Fragment + RadioGroup实现底部导航

Fragment 和 RadioGroup实现底部导航

首先布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_mainmain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ht.testxyapp.com.testapp.activity.MainmainActivity">

    <FrameLayout
        android:id="@+id/fragmentContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

    <RadioGroup
        android:id="@+id/rg"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="首页1" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="首页2" />

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="首页3" />
    </RadioGroup>
</RelativeLayout>

然后Java代码:

public class MainmainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {
    private View fragmentContent;
    private RadioButton rb1, rb2, rb3;
    private RadioGroup rg;
    private RbFragment1 fg1;
    private RbFragment2 fg2;
    private RbFragment3 fg3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainmain);
        fragmentContent = findViewById(R.id.fragmentContent);
        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(this);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rb1.setChecked(true);
    }

    private void hideFragments(FragmentTransaction action) {
        if (fg1 != null) {
            action.hide(fg1);
        }
        if (fg2 != null) {
            action.hide(fg2);
        }
        if (fg3 != null) {
            action.hide(fg3);
        }
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction sd = getSupportFragmentManager().beginTransaction();
        hideFragments(sd);
        switch (checkedId) {
            case R.id.rb1:
                if(fg1==null){
                    fg1 = new RbFragment1();
                    sd.add(R.id.fragmentContent,fg1);
                }else{
                    sd.show(fg1);
                }
                break;
            case R.id.rb2:
                if (fg2 == null){
                    fg2 = new RbFragment2();
                    sd.add(R.id.fragmentContent,fg2);
                }else{
                    sd.show(fg2);
                }
                break;
            case R.id.rb3:
                if (fg3 == null){
                    fg3 = new RbFragment3();
                    sd.add(R.id.fragmentContent,fg3);
                }else{
                    sd.show(fg3);
                }
                break;
        }
        sd.commit();
    }
}

再然后是这几个fragment代码


public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one,container,false);
    oneListView = (ListView) view.findViewById(R.id.oneListView);
    precessScroll();
    initAnimation();
    return view;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值