[木木方文安卓学习笔记六]ViewPager+RadioGroup实现底部导航栏

新建一个xml




    

    

    
    


单个button选中新建一个home_selector.xml



    
    

在activity_main中布局



    
    
    
    


在style


在java中

private void initBottomNav() {
    pager = (ViewPager) findViewById(R.id.vPager);
    radioGroup = (RadioGroup) findViewById(R.id.main_nav_radiogroup);
    fragments = new ArrayList();
    homeFragment = new HomeFragment();
    telephoneBookFragment = new TelephoneBookFragment();
    workFragment = new WorkFragment();
    personalFragment = new PersonalFragment();
    fragments.add(homeFragment);
    fragments.add(telephoneBookFragment);
    fragments.add(workFragment);
    fragments.add(personalFragment);
    rb_Home = (RadioButton) findViewById(R.id.main_nav_home);
    rb_telephoneBook = (RadioButton) findViewById(R.id.main_nav_telephone);
    rb_work = (RadioButton) findViewById(R.id.main_nav_work);
    rb_personal = (RadioButton) findViewById(R.id.main_nav_personal);
    findViewById(R.id.main_nav_home).setOnClickListener(this);
    findViewById(R.id.main_nav_telephone).setOnClickListener(this);
    findViewById(R.id.main_nav_work).setOnClickListener(this);
    findViewById(R.id.main_nav_personal).setOnClickListener(this);

    pager.setAdapter(new FragmentPagerAdapter(this.getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    });

    pager.addOnPageChangeListener(this);
}

实现方法

@Override
    public void onClick(View view) {
        // 加这句
//        Toast.makeText(MainActivity.this, "测试!", Toast.LENGTH_SHORT).show();
        switch (view.getId()) {
            case [R.id](http://R.id).main_nav_home:
                pager.setCurrentItem(0);
                break;
            case [R.id](http://R.id).main_nav_telephone:
                pager.setCurrentItem(1);
                break;
            case [R.id](http://R.id).main_nav_work:
                pager.setCurrentItem(2);
                break;
            case [R.id](http://R.id).main_nav_personal:
                pager.setCurrentItem(3);
                break;
            default:
                break;
        }
        setTabSate();
    }

    private void setTabSate() {
        setHomeState();
        setTelephoneBookState();
        setWorkState();
        setPersonalState();
    }

    private void setHomeState() {
        if (rb_Home.isChecked()) {
            rb_Home.setTextColor(ContextCompat.getColor(this, R.color.colorRed));
        }else {
            rb_Home.setTextColor(ContextCompat.getColor(this, R.color.colorTextGray));
        }
    }

    private void setTelephoneBookState() {
        if (rb_telephoneBook.isChecked()) {
            rb_telephoneBook.setTextColor(ContextCompat.getColor(this, R.color.colorRed));
        }else {
            rb_telephoneBook.setTextColor(ContextCompat.getColor(this, R.color.colorTextGray));
        }
    }

    private void setWorkState() {
        if (rb_work.isChecked()) {
            rb_work.setTextColor(ContextCompat.getColor(this, R.color.colorRed));
        }else {
            rb_work.setTextColor(ContextCompat.getColor(this, R.color.colorTextGray));
        }
    }

    private void setPersonalState() {
        if (rb_personal.isChecked()) {
            rb_personal.setTextColor(ContextCompat.getColor(this, R.color.colorRed));
        }else {
            rb_personal.setTextColor(ContextCompat.getColor(this, R.color.colorTextGray));
        }
    }
@Override
public void onPageScrollStateChanged(int state) {

}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    setTabSate();
}

你可能感兴趣的:([木木方文安卓学习笔记六]ViewPager+RadioGroup实现底部导航栏)