Fragment,在平板應用中較為參見,把視圖分為兩個甚至多個模塊。
一,一個簡單的fragment
1.創建兩個局部文件,用於等待被調用
(1)left_fragment
(2)right_fragment
2.分別創建兩個繼承於fragment類的類
(1)leftFragment(加載xml文件)
public class LeftleftFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment,container,false);//動態加載xml return view; } }
(2)rightFragment
我是代碼(同上);
3.在activity所調用的xml中添加<fragment>控件
<?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"> <fragment android:id="@+id/left_fragment" android:name="com.cenzhongman.myapplication.fragment.LeftleftFragment" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"/> <fragment android:id="@+id/right_fragment" android:name="com.cenzhongman.myapplication.fragment.RightFragment" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout>
二,動態切換fragment
4.在原基礎之上創建AnotherRightFragment(指向要調用的layout)
我是代碼(同上,較少重復加載)
5.在MainActivity所調用xml中更改布局方式
用兩FrameLayout(強制左上角對齊的布局),來存放fragment,動態更新時候將會更換FrameLayout中的內容,也就是旗下的FrameLayout
<?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"> <FrameLayout android:id="@+id/left_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <fragment android:name="com.cenzhongman.myapplication.fragment.LeftleftFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment" /> </FrameLayout> <FrameLayout android:id="@+id/right_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <fragment android:name="com.cenzhongman.myapplication.fragment.RightFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> </LinearLayout>
6.在MainActivity中更換fragment
public void onClick(View v) {
AnotherRightFragment anotherRightFragment = new AnotherRightFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_fragment, anotherRightFragment);
transaction.commit();//必須調用commit()方法,否則顯示不出來
}
1. 創建待添加的碎片實例。
2. 獲取到 FragmentManager,在活動中可以直接調用 getFragmentManager()方法得到。
3. 開啟一個事務,通過調用 beginTransaction()方法開啟。
4. 向容器內加入碎片,一般使用 replace()方法實現,需要傳入容器的 id 和待添加的碎
片實例。
5. 提交事務,調用 commit()方法來完成。
三,把fragment添加到返回棧
transaction.addToBackStack(null);//接收一個名字用於描述返回棧的狀態,一般傳入null即可
用fragment來做導航
1.在MainActivity的xml留出用於裝fragment的容器
2.寫好Fragment的xml描述文件
3.寫四個Fragment的實現類,繼承於Fragment
public class IdeaFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_idea,container,false); return view; } }
4.MainActivity應繼承於FragmentActivity,並創建Fragment對象
//定義fragment對象,基於v4包 private Fragment study_fragment; private Fragment app_fragment; private Fragment idea_fragment; private Fragment personal_fragment; //定義fragmentManager對象 FragmentManager mFragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFragmentManager = getSupportFragmentManager();//基於v4包
initView(); }
5.得到FragmentTransaction對象,並根據情況選擇添加或者顯示fragment
!!!一定要commit()!!!
FragmentTransaction transaction = mFragmentManager.beginTransaction(); idea_Button.setImageResource(R.drawable.idea_blue); idea_TextView.setTextColor(getResources().getColor(R.color.sky_blue)); if (idea_fragment == null) { idea_fragment = new IdeaFragment(); transaction.add(R.id.content, idea_fragment); transaction.commit(); } else { transaction.show(idea_fragment); transaction.commit(); }
參考資料:Fragment之底部導航欄的實現