一段漂亮的Fragment hide,show相關(guān)的代碼

警言:慎終如始!

一,在布局文件中留好Fragment的位置(紅色區(qū)域),這里可以用別的布局,相對(duì)布局,線性布局都行的,只是幫Fragment占個(gè)位置而已,這里建議使用FrameLayout,效率會(huì)高一些


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"

android:orientation="vertical"

tools:context="com.jiuzheyange.fragmentdemo.HomeActivity">

android:id="@+id/fragment_layout"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1">

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="#997755"/>

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="horizontal">

android:id="@+id/wx_bt"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:textSize="18sp"

android:gravity="center"

android:text="微信"/>

android:id="@+id/friend_bt"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:textSize="18sp"

android:gravity="center"

android:text="朋友"/>

android:id="@+id/tongxun_bt"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:textSize="18sp"

android:gravity="center"

android:text="通訊錄"/>

android:id="@+id/setting_bt"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:textSize="18sp"

android:gravity="center"

android:text="設(shè)置"/>

二,主要代碼

packagecom.jiuzheyange.fragmentdemo;

importandroid.os.Bundle;

importandroid.support.v4.app.Fragment;

importandroid.support.v4.app.FragmentManager;

importandroid.support.v4.app.FragmentTransaction;

importandroid.support.v7.app.AppCompatActivity;

importandroid.view.View;

importandroid.widget.TextView;

public classHomeActivityextendsAppCompatActivity {

TextViewwxBt;

TextViewfriendBt;

TextViewtongxinBt;

TextViewsettingBt;

FragmentManagermanager;

FragmentTransactiontransaction;

FragmentAfragmentA;

FragmentBfragmentB;

FragmentCfragmentC;

FragmentDfragmentD;

FragmentcurrentFragment;

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_home);

initView(savedInstanceState);

initData();

initEvent();

}

private voidinitView(Bundle savedInstanceState) {

wxBt= (TextView) findViewById(R.id.wx_bt);

friendBt= (TextView) findViewById(R.id.friend_bt);

tongxinBt= (TextView) findViewById(R.id.tongxun_bt);

settingBt= (TextView) findViewById(R.id.setting_bt);

fragmentA=newFragmentA();

fragmentB=newFragmentB();

fragmentC=newFragmentC();

fragmentD=newFragmentD();

manager=getSupportFragmentManager();

transaction=manager.beginTransaction();

if(savedInstanceState==null) {

transaction.add(R.id.fragment_layout,fragmentA,"fragmentA");

//transaction.replace(R.id.fragment_layout,fragmentA);

transaction.commit();

currentFragment=fragmentA;

}

}

private voidinitData() {

}

private voidinitEvent() {

finalFragmentManager fm1 = getSupportFragmentManager();

wxBt.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View view) {

028661

}

});

friendBt.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View view) {

// 開(kāi)啟Fragment事務(wù)

transaction=manager.beginTransaction();

if(fragmentB.isAdded())

{

if(currentFragment!=fragmentB) {

transaction.show(fragmentB).hide(currentFragment).commit();

currentFragment=fragmentB;

}

}

else

{

transaction.add(R.id.fragment_layout,fragmentB,"fragmentB").hide(currentFragment).commit();

//transaction.replace(R.id.fragment_layout,fragmentA);

currentFragment=fragmentB;

}

}

});

tongxinBt.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View view) {

// 開(kāi)啟Fragment事務(wù)

transaction=manager.beginTransaction();

if(fragmentC.isAdded())

{

if(currentFragment!=fragmentC) {

transaction.show(fragmentC).hide(currentFragment).commit();

currentFragment=fragmentC;

}

}

else

{

transaction.add(R.id.fragment_layout,fragmentC,"fragmentC").hide(currentFragment).commit();

//transaction.replace(R.id.fragment_layout,fragmentA);

currentFragment=fragmentC;

}

}

});

settingBt.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View view) {

transaction=manager.beginTransaction();

if(fragmentD.isAdded())

{

if(currentFragment!=fragmentD) {

transaction.show(fragmentD).hide(currentFragment).commit();

currentFragment=fragmentD;

}

}

else

{

transaction.add(R.id.fragment_layout,fragmentD,"fragmentD").hide(currentFragment).commit();

//transaction.replace(R.id.fragment_layout,fragmentA);

currentFragment=fragmentD;

}

}

});

}

}


注:代碼邏輯比較簡(jiǎn)單,就不再贅述。

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 希望小伙伴們多多支持(如有錯(cuò)誤,希望指出)

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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