警言:慎终如始!
一,在布局文件中留好Fragment的位置(红色区域),这里可以用别的布局,相对布局,线性布局都行的,只是帮Fragment占个位置而已,这里建议使用FrameLayout,效率会高一些
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="设置"/>
二,主要代码
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) {
// 开启Fragment事务
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) {
// 开启Fragment事务
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;
}
}
});
}
}
注:代码逻辑比较简单,就不再赘述。
希望小伙伴们多多支持(如有错误,希望指出)