android FrameLayout+RadioGroup底部导航栏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Alangao420983/article/details/88304057

在项目开发中,市场上大大小小的app很多就是采用底部几个按钮的导航栏。其实有很多种方式去实现。这里是采用FrameLayout+RadioGroup来实现的。

1.主界面的xml文件`

<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">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/color_gay_guide" />

        <RadioGroup
            android:id="@+id/radio_main"
            android:layout_width="match_parent"
            android:layout_height="54dp"
            android:layout_marginTop="5sp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_home"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/tab_home_deal"
                android:gravity="center"
                android:text="11"
                android:textColor="@drawable/tab_home_deal_text" />

            <RadioButton
                android:id="@+id/rb_wealth"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/tab_wealth_deal"
                android:gravity="center"
                android:text="22"
                android:textColor="@drawable/tab_home_deal_text" />

            <RadioButton
                android:id="@+id/rb_find"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/tab_find_deal"
                android:gravity="center"
                android:text="33"
                android:textColor="@drawable/tab_home_deal_text" />

            <RadioButton
                android:id="@+id/rb_mine"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/tab_mine_deal"
                android:gravity="center"
                android:text="44"
                android:textColor="@drawable/tab_home_deal_text" />
        </RadioGroup>
    </LinearLayout>


</LinearLayout>

主要是RadioButton的点击切换,这个是不能滑动界面。
图片的切换在drawable中配置:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/pic_home_page_one" android:state_checked="true"/>
    <item android:drawable="@mipmap/pic_home_page_tow" android:state_checked="false"/>
</selector>

其他的3个依次,注意的是state_checked。
字体的颜色的改变:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color_main" android:state_checked="true"/>
    <item android:color="@color/color_home_fragment_text" android:state_checked="false"/>
</selector>

主界面的代码

public class HomeActivity extends BaseActivity<HomePresenter> implements HomeView,View.OnClickListener {

    private HomeFragment homeFragment;
    private WealthFragment wealthFragment;
    private FindFragment findFragment;
    private MineFragment mineFragment;

    private RadioButton mRadioButtonHome;
    private FrameLayout mFrameLayout;
    private RadioGroup mRadioGroup;
    private FragmentTransaction transaction;

    @Override
    protected HomePresenter createPresenter() {
        return new HomePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void initView() {
        super.initView();
        mFrameLayout = (FrameLayout) findViewById(R.id.fragment_main);
        mRadioGroup = (RadioGroup) findViewById(R.id.radio_main);
        mRadioButtonHome = (RadioButton) findViewById(R.id.rb_home);
        //默认首页选中
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        transaction = supportFragmentManager.beginTransaction();
        if (homeFragment == null){
            homeFragment=new HomeFragment();
            transaction.add(R.id.fragment_main,homeFragment);
        }
        transaction.show(homeFragment);
        transaction.commit();
        //初始选中的状态
        mRadioButtonHome.setChecked(true);
    }

    @Override
    public void setListener() {
        super.setListener();
    }

    @Override
    public void initData() {
        super.initData();
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                transaction = supportFragmentManager.beginTransaction();
                hideFragment();
                switch (checkedId){
                    case R.id.rb_home:
                        if (homeFragment == null){
                            homeFragment=new HomeFragment();
                            transaction.add(R.id.fragment_main,homeFragment);
                        }
                        transaction.show(homeFragment);
                        break;
                    case R.id.rb_wealth:
                        if (wealthFragment == null){
                            wealthFragment = new WealthFragment();
                            transaction.add(R.id.fragment_main,wealthFragment);
                        }
                        transaction.show(wealthFragment);
                        break;
                    case R.id.rb_find:
                        if (findFragment == null){
                            findFragment = new FindFragment();
                            transaction.add(R.id.fragment_main,findFragment);
                        }
                        transaction.show(findFragment);
                        break;
                    case R.id.rb_mine:
                        if (mineFragment == null){
                            mineFragment = new MineFragment();
                            transaction.add(R.id.fragment_main,mineFragment);
                        }
                        transaction.show(mineFragment);
                        break;
                }
                transaction.commit();
            }
        });
    }

    private void hideFragment() {
        if (homeFragment!=null){
            transaction.hide(homeFragment);
        }
        if (wealthFragment!=null){
            transaction.hide(wealthFragment);
        }
        if (findFragment!=null){
            transaction.hide(findFragment);
        }
        if (mineFragment!=null){
            transaction.hide(mineFragment);
        }
    }

}

fragment中就不用去描述了。

生活的点点滴滴虽小,积累就成多,没有一步登天,只有一步一步的走下去。虽然简单但是还是去做,坚定自己。
坚持写,做笔记。

猜你喜欢

转载自blog.csdn.net/Alangao420983/article/details/88304057