关于Android中Fragment静态和动态加载的方法

时间:2022-09-29 17:19:03

一.静态加载

1.首先创建一个layout布局fragment.xml,里面放要显示和操作的控件

2.创建一个layout布局main1.xml,用来实现页面的跳转(跳转为要实现静态加载的界面)

3.创建一个layout布局main2.xml,用来实现MyFragment类的转换为视图控件的定义

4.创建一个继承自Activity的Main1.java类,载入main1.xml的视图,并且实现点击后页面跳转为Main2.java类

5.创建一个继承自Activity的Main2.java类,载入main2.xml的视图,并且实现点击后fragment.xml中组件的text改变的效果,main2.java中可以直接引用fragment.xml中的控件

6.创建一个继承自ragment的MyFragment.java类,载入fragment.xml的视图,并且设置fragment.xml中组件的text初始的值

7.在AndroidMainFest文件里面定义Activity的Main1.java类和Activity的Main2.java类,并把Main1.java定义为初始的Activity界面

fragment.xml

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变" />

main1.xml

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="静态加载" />

main2.xml

<fragment
android:id="@+id/fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.fragmentjing.MyFragment"
/>

Main1.java

public class Main1 extends Activity {

  private Button btn1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);     btn1=(Button) findViewById(R.id.button1);     btn1.setOnClickListener(new OnClickListener(){     @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      Intent intent = new Intent(Main1.this,Main2.class);
      startActivity(intent);
    }   }); }

Main2.java

public class Main2 extends Activity {
  private TextView text;
  private Button button;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);     button=(Button) findViewById(R.id.button4);
    text=(TextView) findViewById(R.id.textView4);     button.setOnClickListener(new OnClickListener(){     @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      text.setText("TextView的值改变了");
    }
   });
 }
}

MyFragment.java

public class MyFragment extends Fragment{

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  View view=inflater.inflate(R.layout.fragment,container,false);
  TextView textView=(TextView)view.findViewById(R.id.textView4);
  textView.setText("静态加载Fragment");
  return view;
  }
}

二.动态加载

1.首先创建一个layout布局fragment.xml,里面放要显示和操作的控件

2.创建一个layout布局main1.xml,用来实现页面的跳转(跳转为要实现静态加载的界面)

3.创建一个继承自Activity的Main1.java类,载入main1.xml的视图,并且实现点击后创建MyFragment2.java类的事件

4.创建一个继承自ragment的MyFragment2.java类,载入fragment.xml的视图,并且设置fragment.xml中组件的text初始的值

5.在AndroidMainFest文件里面定义Activity的Main1.java类和Activity的Main2.java类,并把Main1.java定义为初始的Activity界面

fragment.xml

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变" />

main1.xml

<LinearLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>

Main1.java

public class Main1 extends Activity {

  private Button btn2;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);
    btn2=(Button) findViewById(R.id.button2);     btn2.setOnClickListener(new OnClickListener(){       @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        MyFragment2 fragment2 = new MyFragment2();
        FragmentManager fragmrentManager = getFragmentManager();
        FragmentTransaction beginTransaction = fragmrentManager.beginTransaction();
        beginTransaction.add(R.id.frame, fragment2);
        beginTransaction.addToBackStack(null);
        beginTransaction.commit();
        }
      });
    }
}

MyFragment2.java

public class MyFragment2 extends Fragment{
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view=inflater.inflate(R.layout.fragment,container,false);
    TextView textView=(TextView)view.findViewById(R.id.textView4);
    textView.setText("动态加载Fragment");
    return view;
  }
}