fragment使用add方法進(jìn)行切換

使用add與hide切換fragment

首先,在activity中添加方法(參數(shù)fragment是將要切換的fragment,變量mcContent是用來(lái)區(qū)分當(dāng)前fragment和將要切換的fragment的)

public void switchFm(BaseFragment fragment) {
    if (fragment != mContent) {
        FragmentTransaction ft = fm.beginTransaction();
        if (fragment.isAdded()) {
            ft.hide(mContent).show(fragment).commit();
        } else {
            if (mContent == null) {
                ft.add(R.id.fl_content, fragment).commit();
            } else {
                ft.hide(mContent).add(R.id.fl_content, fragment).commit();
            }
        }
        mContent = fragment;
    }
}

在activity中調(diào)用:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fm = getSupportFragmentManager();
    RadioGroup rgHelloworld = (RadioGroup) findViewById(R.id.rg_helloworld);
    rgHelloworld.setOnCheckedChangeListener(this);
    rgHelloworld.check(R.id.hello);
}

切換方法(實(shí)現(xiàn)RadioGroup的OnCheckedChangeListener監(jiān)聽(tīng)):

@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedID) {
    switch (checkedID) {
        case R.id.hello:
            switchFm(new HelloFragment());
            break;
        case R.id.world:
            switchFm(new WorldFragment());
            break;
    }
}

由此就可以自如的切換fragment

附xml文件:

       <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fl_top_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kang.fragmentdemo.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#4250a7">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textColor="#ffffff"
                android:textSize="22sp" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RadioGroup
            android:id="@+id/rg_helloworld"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_gravity="bottom"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/hello"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/bg_radiobutton"
                android:button="@null"
                android:gravity="center"
                android:text="hello"
                android:textColor="#ffffff" />

            <RadioButton
                android:id="@+id/world"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/bg_radiobutton"
                android:button="@null"
                android:gravity="center"
                android:text="world"
                android:textColor="#ffffff" />

        </RadioGroup>

    </FrameLayout>
</LinearLayout>
</FrameLayout>

附效果圖:


1484642160(1).png
1484642183(1).png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容