简单使用Fragment实现页面切换

xiaoxiao2021-02-28  0

1.在activity_main的布局文件中添加

<FrameLayout android:id="@+id/frag" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"></FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_alignParentBottom="true"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton style="@style/AppBottom" android:id="@+id/shou" android:drawableTop="@drawable/selector_bar_1" android:checked="true" android:text="首页" android:textColor="@drawable/selector_bar_text" /> <RadioButton style="@style/AppBottom" android:id="@+id/fenclass" android:drawableTop="@drawable/selector_bar_2" android:text="分类" android:textColor="@drawable/selector_bar_text" /> <RadioButton style="@style/AppBottom" android:id="@+id/car" android:drawableTop="@drawable/selector_bar_3" android:text="购物车" android:textColor="@drawable/selector_bar_text" /> <RadioButton style="@style/AppBottom" android:id="@+id/user" android:drawableTop="@drawable/selector_bar_4" android:text="我的" android:textColor="@drawable/selector_bar_text" /> </RadioGroup> </LinearLayout>

2.MainAcitivity:

//定义方法  在onCreate中调用

private void init() { fm = getSupportFragmentManager(); f1 = new Home(); f2 = new FenClass(); f3 = new Car(); f4 = new User(); //开启事务 beginTransaction = fm.beginTransaction(); // beginTransaction.replace(R.id.frag,f1); beginTransaction.add(R.id.frag,f1); beginTransaction.add(R.id.frag,f2); beginTransaction.add(R.id.frag,f3); beginTransaction.add(R.id.frag,f4); beginTransaction.show(f1); beginTransaction.hide(f2); beginTransaction.hide(f3); beginTransaction.hide(f4); beginTransaction.commit(); } @OnClick({R.id.shou, R.id.fenclass, R.id.car, R.id.user}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.shou:   beginTransaction = fm.beginTransaction(); beginTransaction.show(f1); beginTransaction.hide(f2); beginTransaction.hide(f3); beginTransaction.hide(f4); beginTransaction.commit(); break; case R.id.fenclass: beginTransaction = fm.beginTransaction(); beginTransaction.show(f2); beginTransaction.hide(f1); beginTransaction.hide(f3); beginTransaction.hide(f4); beginTransaction.commit(); break; case R.id.car: beginTransaction = fm.beginTransaction(); beginTransaction.show(f3); beginTransaction.hide(f2); beginTransaction.hide(f1); beginTransaction.hide(f4); beginTransaction.commit(); break; case R.id.user: beginTransaction = fm.beginTransaction(); beginTransaction.show(f4); beginTransaction.hide(f2); beginTransaction.hide(f3); beginTransaction.hide(f1); beginTransaction.commit(); if (isLogin==false){ Intent intent = new Intent(this, LoginAcitivity.class); startActivity(intent); } } } /*去除(隐藏)所有的Fragment的方法*/ private void hideFragment(FragmentTransaction transaction) { if (f1 != null) { //transaction.hide(f1);隐藏方法也可以实现同样的效果,一般使用去除 transaction.remove(f1); } if (f2 != null) { //transaction.hide(f2); transaction.remove(f2); } if (f3 != null) { //transaction.hide(f3); transaction.remove(f3); } if (f4 != null) { //transaction.hide(f3); transaction.remove(f4); } }

转载请注明原文地址: https://www.6miu.com/read-1750149.html

最新回复(0)