先看下效果圖:
GIF.gif
首先,創(chuàng)建left_fragment.xml ,right_fragment.xml 和 another_right_fragment.xml布局:
<Button
android:layout_gravity="center_horizontal"
android:text="Button"
android:textAllCaps="false"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
left布局只有一個(gè)按鈕
<TextView
android:layout_gravity="center_horizontal"
android:text="this is right fragment"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
right布局只有一個(gè)TextView,并給根布局設(shè)置背景為綠色:android:background="#00ff00"
<TextView
android:layout_gravity="center_horizontal"
android:text="this is right fragment"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
another_right_fragment布局也是只有一個(gè)TextView,并給根布局設(shè)置背景為黃色: android:background="#ffff00"
2 . 分別創(chuàng)建 LeftFragment ,RightFragment ,AnotherRightFragment并讓他們繼承Fragment
最好選擇V4包下的Fragment
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.left_farment,container,false);
return view;
} }
LeftFragment 與 RightFragment,AnotherRightFragment 大致一樣這里只給出一個(gè)
3 . 修改 activity_main.xml. 將右側(cè)Fragment放在了一個(gè) FrameLayout 中
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/left_fragment"
android:layout_width="0dp"
android:layout_weight="1"
android:name = "com.example.fragment_4zhang.fragment.LeftFragment"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
<fragment
android:id="@+id/right_fragment"
android:layout_width="match_parent"
android:name = "com.example.fragment_4zhang.fragment.RightFragment"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
4 .修改MainActivity 中的代碼
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
AnotherRightFragment fragment = new AnotherRightFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.commit();
break;
}
} }
。。。
5 .動(dòng)態(tài)添加Fragment步驟
- 創(chuàng)建待添加的Fragment實(shí)例。
- 獲取到 FragmentManager,在Activity中可以直接調(diào)用getFragmentManager()方法得到。
- 開啟一個(gè)事務(wù),通過調(diào)用 beginTransaction()方法開啟。
- 向容器內(nèi)加入Fragment,一般使用 replace()方法實(shí)現(xiàn),需要- 傳入容器的 id 和待添加的Fragment實(shí)例。
- 提交事務(wù),調(diào)用 commit()方法來完成。
代碼已上傳Github:https://github.com/15503737504/DynamicAddFragment