首先寫好每個Fragment:
1.在第一個Fragment寫一個按鈕,使其加載下一個Fragment
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加載"/> </LinearLayout>
java代碼:
public class LeftFragment extends Fragment{ OnClickButton mCallback; //定義一個接口 public interface OnClickButton{ //並實現一個方法,用來傳值並在(onAttach()中綁定activity) public void onClickB(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //綁定布局文件並獲取到里面的控件,特別 注意里面的 view View view = inflater.inflate(R.layout.fragment_left,null); Button button = (Button) view.findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCallback.onClickB(); } }); return view; } /** * 綁定到activity * @param activity */ public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (OnClickButton) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } }
加載顯示出來的布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新聞內容" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
java文件:
public class RightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_right, null); Button button = (Button) view.findViewById(R.id.button2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //getActivity() 獲取父類的activity Toast.makeText(getActivity(), "我是fragment", Toast.LENGTH_SHORT).show(); } }); return view; } }
主類:
布局
給Fragment創建一個容器activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
注意:一定要寫明id。
然后就在activity中實現Fragment add進去就行了!
//實現LeftFragment中定義的接口,主要用來傳值或者按鈕點擊事件 public class MainActivity extends Activity implements LeftFragment.OnClickButton { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化第一個Fragment if (findViewById(R.id.fragment_container) != null){ if (savedInstanceState != null) { return; } LeftFragment leftFragment = new LeftFragment(); leftFragment.setArguments(getIntent().getExtras()); getFragmentManager().beginTransaction().add(R.id.fragment_container,leftFragment).commit(); } } /** * 實現接口中的方法和點擊按鈕后加載的fragment */ @Override public void onClickB() { FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); RightFragment rightFragment = new RightFragment(); transaction.replace(R.id.fragment_container, rightFragment); transaction.addToBackStack(null); transaction.commit(); } }
這樣就實現了一個很小的demo!
動態添加Fragment
首先新建兩個fragment的布局文件
fragment1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is fragment 1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
fragment2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000ff" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is fragment 2" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
新建兩個Fragment類繼承Fragment
Fragment1
public class Fragmet1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1,container,false); } }
Fragment2
public class Fragmet2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2,container,false); } }
然后定義一個顯示fragment的mainactivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:baselineAligned="false" > <Button android:id="@+id/btn_show_fragment1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="顯示Fragment1"/> <Button android:id="@+id/btn_show_fragment2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="顯示Fragment2"/> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
其中FrameLayout是用來顯示Fragment的,在MainActivity中實現
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); Button button1 = (Button) findViewById(R.id.btn_show_fragment1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //動態添加Fragment FragmentManager manager = getSupportFragmentManager(); FragmentTransaction ft = manager.beginTransaction(); Fragmet1 fragmet1 = new Fragmet1(); ft.add(R.id.fragment_container,fragmet1); ft.commit(); } }); Button button2 = (Button) findViewById(R.id.btn_show_fragment2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction ft = manager.beginTransaction(); Fragmet2 fragmet2 = new Fragmet2(); ft.add(R.id.fragment_container,fragmet2); ft.commit(); } }); } }
ok 基本就完成了。
注意:
動態添加Fragment主要分為4步:
- 1.獲取到FragmentManager,在V4包中通過getSupportFragmentManager,在系統中原生的Fragment是通過getFragmentManager獲得的。
- 2.開啟一個事務,通過調用beginTransaction方法開啟。
- 3.向容器內加入Fragment,一般使用add或者replace方法實現,需要傳入容器的id和Fragment的實例。
- 4.提交事務,調用commit方法提交。