android FrameLayout+RadioGroup底部导航栏

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

1.主界面的xml文件`



    

    

        

        

            

            

            

            
        
    



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


    
    

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


    
    

主界面的代码

public class HomeActivity extends BaseActivity 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中就不用去描述了。

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

你可能感兴趣的:(android)