FrameLayout页面切换(事务管理)

    xiaoxiao2022-07-07  141

    我的布局

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".HomeActivity"> <FrameLayout android:id="@+id/home_frameLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9.5"></FrameLayout> <RadioGroup android:id="@+id/rg_RadioGroup" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@mipmap/bg_homepage_bottom" android:gravity="center" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_home" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:checked="true" android:drawableBottom="@drawable/selector_home" android:gravity="center" /> <RadioButton android:id="@+id/rb_circle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableBottom="@drawable/selector_circle" android:gravity="center"/> <RadioButton android:id="@+id/rb_shoppingCart" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableBottom="@mipmap/tab_gwc" android:gravity="center" /> <RadioButton android:id="@+id/rb_orderForm" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableBottom="@drawable/selector_orderform" android:gravity="center" /> <RadioButton android:id="@+id/rb_mySelf" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableBottom="@drawable/selector_myself" android:gravity="center" /> </RadioGroup> </LinearLayout>

    主页面:

    public class HomeActivity extends AppCompatActivity { @BindView(R.id.home_frameLayout) FrameLayout frameLayout; @BindView(R.id.rb_home) RadioButton rbHome; @BindView(R.id.rb_circle) RadioButton rbCircle; @BindView(R.id.rb_shoppingCart) RadioButton rbShoppingCart; @BindView(R.id.rb_orderForm) RadioButton rbOrderForm; @BindView(R.id.rb_mySelf) RadioButton rbMySelf; private FragmentManager supportFragmentManager; private Unbinder unbinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); //注册ButterKnife unbinder = ButterKnife.bind(this); //开启事务管理器 supportFragmentManager = getSupportFragmentManager(); //添加Fragment FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.add(R.id.home_frameLayout, new HomeFragment()); //默认显示的页面 fragmentTransaction.commit(); } @OnClick({R.id.rb_home, R.id.rb_circle, R.id.rb_shoppingCart, R.id.rb_orderForm, R.id.rb_mySelf}) public void OnClick(View view) { switch (view.getId()) { case R.id.rb_home: //添加Fragment FragmentTransaction homeTransaction = supportFragmentManager.beginTransaction(); homeTransaction.replace(R.id.home_frameLayout, new HomeFragment()); homeTransaction.commit(); break; case R.id.rb_circle: //替换Fragment FragmentTransaction circleTransaction = supportFragmentManager.beginTransaction(); circleTransaction.replace(R.id.home_frameLayout, new CircleFragment()); circleTransaction.commit(); break; case R.id.rb_shoppingCart: //替换Fragment FragmentTransaction shoppingCartTransaction = supportFragmentManager.beginTransaction(); shoppingCartTransaction.replace(R.id.home_frameLayout, new ShoppingCartFragment()); shoppingCartTransaction.commit(); break; case R.id.rb_orderForm: //替换Fragment FragmentTransaction orderFormTransaction = supportFragmentManager.beginTransaction(); orderFormTransaction.replace(R.id.home_frameLayout, new OrderFormFragment()); orderFormTransaction.commit(); break; case R.id.rb_mySelf: //替换Fragment FragmentTransaction mySelfTransaction = supportFragmentManager.beginTransaction(); mySelfTransaction.replace(R.id.home_frameLayout, new MySelfFragment()); mySelfTransaction.commit(); break; default: break; } } @Override protected void onDestroy() { super.onDestroy(); unbinder.unbind(); } }
    最新回复(0)