Android中使用RadioGroup实现Fragment的切换

现在的App主页设计,一般采用的是几个按钮加上不同的Fragment切换。这样看起来层次很清晰,功能明确,用户一目了然。

实现这种效果的方法有很多种,网上第三方的库也有很多很多。但是当我们使用第三方库时,往往会受到它或多或少的限制,其实我们用原生的Android控件就可以实现这种效果,Google已经帮我们封装得很好了。我使用的是RadioGroup加上Fragment的切换,话不多说,直接上代码。

 <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_44"
        android:layout_margin="@dimen/dp_10"
        android:orientation="horizontal"
        android:weightSum="3">

        <RadioButton
            android:id="@+id/btn_product_barcode"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/putaway_operation_left_selector"
            android:button="@null"
            android:gravity="center"
            android:text="@string/wms_col_product_barcode"
            android:textColor="@drawable/putaway_operation_text_selector"/>

        <RadioButton
            android:id="@+id/btn_box"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/putaway_operation_middle_selector"
            android:button="@null"
            android:gravity="center"
            android:text="@string/wms_col_container_no"
            android:textColor="@drawable/putaway_operation_text_selector"/>

        <RadioButton
            android:id="@+id/btn_tray"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/putaway_operation_right_selector"
            android:button="@null"
            android:gravity="center"
            android:text="@string/wms_col_pallet_id"
            android:textColor="@drawable/putaway_operation_text_selector"/>
    </RadioGroup>

    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

在布局文件中,上面是一个RadioGroup,里面有三个RadioButton,对应就是三个切换按钮。下面的FrameLayout是切换的Fragment。

在Activity中,需要用代码来实现Fragment的切换。必须先设置一个默认的Fragment,然后再根据RadioGroup的check事件来实现切换。

//设置默认选中的Fragment
mBtnProductBarcode.setChecked(true);
final FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fl_content, new ProductBarcodeFragment());
transaction.commit();

//RadioGroup的check事件,来实现Fragment的切换
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                //根据RadioButton不同的Id来选中不同的Fragment。
                if (checkedId == R.id.btn_product_barcode) {
                    transaction.replace(R.id.fl_content, new ProductBarcodeFragment());
                } else if (checkedId == R.id.btn_box) {
                    transaction.replace(R.id.fl_content, new BoxFragment());
                } else if (checkedId == R.id.btn_tray) {
                    transaction.replace(R.id.fl_content, new TrayFragment());
                }
                transaction.commit();
            }
        });

接着我们可以在各自的Fragment中写自己的布局和逻辑代码,它们虽然都是挂载在一个Activity上面,但是它们互相之间并没有什么影响。代码很简单,希望能帮到你。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值