底部菜单实现(二):RadioGroup和fragment

一布局文件

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.radiogroup.MainActivity"<FrameLayout
android:id="@+id/framlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>

<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:button="@null"
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn1"/>

<RadioButton
android:button="@null"
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn2"/>

<RadioButton
android:button="@null"
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn3"/>
</RadioGroup>
</LinearLayout>

二.与上一遍一样,创建三个fragment和缓存处理

三代码

    public class MainActivity extends AppCompatActivity {


    @Bind(R.id.framlayout)
    FrameLayout mFramlayout;
    @Bind(R.id.btn1)
    Button mBtn1;
    @Bind(R.id.btn2)
    Button mBtn2;
    @Bind(R.id.btn3)
    Button mBtn3;
    @Bind(R.id.radio_group)
    RadioGroup mRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        iniView();
        iniEvent();

    }

    private void iniEvent() {

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                FragmentTransaction transaction = getSupportFragmentManager()
                        .beginTransaction();
                BaseFragment fragment=null;
                switch (checkedId) {
                    case  R.id.btn1:

                        fragment=FragmentFractory.getInstance().getBaseFragment(0);
                         break;
                    case R.id.btn2:

                        fragment=FragmentFractory.getInstance().getBaseFragment(1);
                         break;
                    case R.id.btn3:
                        fragment=FragmentFractory.getInstance().getBaseFragment(2);
                         break;

                }
                transaction.replace(R.id.framlayout,fragment).commit();
            }
        });
    }

    private void iniView() {
        getSupportFragmentManager().
                beginTransaction().
                replace(R.id.framlayout, FragmentFractory.getInstance().getBaseFragment(0))
                .commit();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值