動(dòng)態(tài)添加Fragment

先看下效果圖:

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()方法得到。
  • 開(kāi)啟一個(gè)事務(wù),通過(guò)調(diào)用 beginTransaction()方法開(kāi)啟。
  • 向容器內(nèi)加入Fragment,一般使用 replace()方法實(shí)現(xiàn),需要- 傳入容器的 id 和待添加的Fragment實(shí)例。
  • 提交事務(wù),調(diào)用 commit()方法來(lái)完成。

代碼已上傳Github:https://github.com/15503737504/DynamicAddFragment

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,557評(píng)論 25 708
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 6,554評(píng)論 0 17
  • 1.創(chuàng)建一個(gè)類繼承Fragment,復(fù)寫onCreateView方法。例如: 2.在MainActivity中創(chuàng)建...
    前端develop閱讀 6,538評(píng)論 0 0
  • “讓王爺一人服蠱?...對(duì)王爺?shù)纳眢w可有傷害?” 司徒靜猶豫著問(wèn)道。 “稟娘娘,除了形體上的虛弱枯槁外全無(wú)傷害。”...
    兮云閱讀 473評(píng)論 0 6
  • 自安古城位于通化市的治安村,古城始建于南北朝,高句麗族在此建立一個(gè)王國(guó)。目前流經(jīng)身旁的是哈泥河,哈泥河是渾江的支流...
    自安古城先生閱讀 589評(píng)論 0 0