FrameLayout页面切换(事务管理)

我的布局

<?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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值