美文网首页
Android中使用RadioGroup实现Fragment的切

Android中使用RadioGroup实现Fragment的切

作者: Funny灬 | 来源:发表于2017-11-20 17:04 被阅读0次

    现在的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上面,但是它们互相之间并没有什么影响。代码很简单,希望能帮到你。

    相关文章

      网友评论

          本文标题:Android中使用RadioGroup实现Fragment的切

          本文链接:https://www.haomeiwen.com/subject/lxgvvxtx.html