[木木方文安卓學習筆記六]ViewPager+RadioGroup實現底部導航欄

weixin_34075551發表於2017-08-16

新建一個xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/app_home" />

    <item
        android:id="@+id/navigation_telephoneBook"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/app_telephoneBook" />

    <item
        android:id="@+id/navigation_work"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/app_work" />
    <item
        android:id="@+id/navigation_personal"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/app_personal"/>

</menu>

單個button選中新建一個home_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)">
    <item android:state_checked="true" android:drawable="@mipmap/home_selected"/>
    <item android:drawable="@mipmap/home_normal"/>
</selector>

在activity_main中佈局

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

    <RadioButton
        android:id="@+id/main_nav_home"
        style="@style/main_navi_radiobutton"
        android:drawableTop="@drawable/home_selector"
        android:textColor="@color/colorRed"
        android:checked="true"
        android:text="@string/app_home"/>
    <RadioButton
        android:id="@+id/main_nav_telephone"
        style="@style/main_navi_radiobutton"
        android:drawableTop="@drawable/telephone_selector"
        android:textColor="@color/colorTextGray"
        android:text="@string/app_telephoneBook"/>
    <RadioButton
        android:id="@+id/main_nav_work"
        style="@style/main_navi_radiobutton"
        android:drawableTop="@drawable/work_selector"
        android:textColor="@color/colorTextGray"
        android:text="@string/app_work"/>
    <RadioButton
        android:id="@+id/main_nav_personal"
        style="@style/main_navi_radiobutton"
        android:drawableTop="@drawable/personal_selector"
        android:textColor="@color/colorTextGray"
        android:text="@string/app_personal"/>

</RadioGroup>

在style

<style name="main_navi_radiobutton">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">50dp</item>
    <item name="android:layout_weight">1</item>
    <item name="android:button">@null</item>
    <item name="android:gravity">center</item>
    <item name="android:drawablePadding">5dp</item>
    <item name="android:textSize">10sp</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:paddingTop">5dp</item>
    <item name="android:paddingBottom">5dp</item>
</style>

在java中

private void initBottomNav() {
    pager = (ViewPager) findViewById(R.id.vPager);
    radioGroup = (RadioGroup) findViewById(R.id.main_nav_radiogroup);
    fragments = new ArrayList<Fragment>();
    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();
}

相關文章