Android Fragment 巧妙利用 show和hide。这里咱们分两种情况来做相应的处理。
第一种:switch 是以break;结束
Java代码片段
public class ContextFragment extends Fragment {
@BindView(R.id.frame_layout)
FrameLayout frameLayout;
@BindView(R.id.navigation)
BottomNavigationView navigation;
Unbinder unbinder;
private View mView;
private BlankFragment1 mABlankFragment;
private BlankFragment2 mBBlankFragment;
private BlankFragment3 mCBlankFragment;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//事务
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
// 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
hideFragments(transaction);
switch (item.getItemId()) {
case R.id.navigation_home:
//显示、加载Fragment
if (mABlankFragment == null) {
// 如果fragment为空,则创建一个并添加到界面上
mABlankFragment = new BlankFragment1();
transaction.add(R.id.frame_layout, mABlankFragment);
} else {
// 如果Fragment不为空,则直接将它显示出来
transaction.show(mABlankFragment);
}
// return true;
break;
case R.id.navigation_dashboard:
//显示、加载Fragment
if (mBBlankFragment == null) {
// 如果fragment为空,则创建一个并添加到界面上
mBBlankFragment = new BlankFragment2();
transaction.add(R.id.frame_layout, mBBlankFragment);
} else {
// 如果Fragment不为空,则直接将它显示出来
transaction.show(mBBlankFragment);
}
// return true;
break;
case R.id.navigation_notifications:
//显示、加载Fragment
if (mCBlankFragment == null) {
// 如果fragment为空,则创建一个并添加到界面上
mCBlankFragment = new BlankFragment3();
transaction.add(R.id.frame_layout, mCBlankFragment);
} else {
// 如果Fragment不为空,则直接将它显示出来
transaction.show(mCBlankFragment);
}
// return true;
break;
default:
}
transaction.commit();
return false;
}
};
private void hideFragments(FragmentTransaction transaction) {
if (mABlankFragment != null) {
transaction.hide(mABlankFragment);
}
if (mBBlankFragment != null) {
transaction.hide(mBBlankFragment);
}
if (mCBlankFragment != null) {
transaction.hide(mCBlankFragment);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.view_page_context, container, false);
initView();
unbinder = ButterKnife.bind(this, mView);
return mView;
}
private void initView() {
BottomNavigationView navigation = mView.findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
第二种:switch 是以return;结束
public class MainActivity extends BaseActivity {
private PrimaryFragment primaryFragment;
private JuniorHighFragment juniorHighFragment;
private HighFragment highFragment;
private boolean isOne = true;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
//隐藏所有fragment
hideAllFragment(transaction);
switch (item.getItemId()) {
case R.id.navigation_home:
if (highFragment == null) {
Log.e("空空如也", "onNavigationItemSelected: ");
//这里这样写,是因为我们在布局中默认静态加载该碎片(这里显示出来就好)
//静态和动态好像不能一起使用,会出问题,不注释就会出现空白,
//highFragment = new HighFragment();
//transaction.add(R.id.move_fragment, highFragment);
} else {
transaction.show(highFragment);
}
//提交事务这个骚操作不能写在外层了,会没反应
transaction.commit();
return true;
case R.id.navigation_dashboard:
if (juniorHighFragment == null) {
juniorHighFragment = new JuniorHighFragment();
transaction.add(R.id.move_fragment, juniorHighFragment);
} else {
transaction.show(juniorHighFragment);
}
transaction.commit();
return true;
case R.id.navigation_notifications:
if (primaryFragment == null) {
primaryFragment = new PrimaryFragment();
transaction.add(R.id.move_fragment, primaryFragment);
} else {
transaction.show(primaryFragment);
}
transaction.commit();
return true;
default:
}
return false;
}
};
/**
* 隐藏所有fragment
*/
private void hideAllFragment(FragmentTransaction transaction) {
if (primaryFragment != null) {
transaction.hide(primaryFragment);
}
if (juniorHighFragment != null) {
transaction.hide(juniorHighFragment);
}
if (highFragment != null) {
transaction.hide(highFragment);
}
}
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected void initView() {
// if (isOne) {
// FragmentManager fragmentManager = getSupportFragmentManager();
// FragmentTransaction transaction = fragmentManager.beginTransaction();
// HighFragment highFragment = new HighFragment();
// transaction.add(R.id.move_fragment, highFragment);
// transaction.commit();
// }
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
两种情况的xml代码片段是一样的
?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.liaichuan.top.ui.fragment.BlankFragment1"/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>