【Tab導航】ViewPager+RadioButton輕鬆實現底部Tab導航

1、概述

 實現相似微信哪一種底部tab導航的方式有不少種,這篇文章先介紹實現這種底部導航的一種簡單的方式,即ViewPager+RedioButton實現底部tab導航。java

實現以前,咱們先看一下即將要實現的導航效果圖:android

這裏寫圖片描述

OK,下面咱們經過代碼一步一步實現上圖的底部導航效果。數組

2、編寫代碼

第一步:編寫主界面的佈局,activity_main.xml微信

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </android.support.v4.view.ViewPager>

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

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp" >

        <RadioButton
            android:id="@+id/btn_home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawablePadding="3dp"
            android:drawableTop="@drawable/ic_tab_home_yellow"
            android:gravity="center_horizontal"
            android:text="@string/tab_home"
            android:textColor="@color/yellow" />

        <RadioButton
            android:id="@+id/btn_classify"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawablePadding="3dp"
            android:drawableTop="@drawable/ic_tab_classify_yellow"
            android:gravity="center_horizontal"
            android:text="@string/tab_classify"
            android:textColor="@color/yellow" />

        <RadioButton
            android:id="@+id/btn_discover"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawablePadding="3dp"
            android:drawableTop="@drawable/ic_tab_discover_yellow"
            android:gravity="center_horizontal"
            android:text="@string/tab_discover"
            android:textColor="@color/yellow" />

        <RadioButton
            android:id="@+id/btn_me"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:button="@null"
            android:drawablePadding="3dp"
            android:drawableTop="@drawable/ic_tab_me_yellow"
            android:gravity="center_horizontal"
            android:text="@string/tab_me"
            android:textColor="@color/yellow" />
    </RadioGroup>

</LinearLayout>

 注意:app

  • RadioButton默認是前面帶有圓點的,去掉前面圓點的方法:
    將button屬性設爲null,即:
android:button="@null"
  • 讓RadioButton的文本水平居中,即:
android:gravity="center_horizontal"
  • 能夠添加一個寬度爲屏幕填充父容器,高度爲0.1dp的View,設置顏色背景實現分割線的效果ide

  • 當設置了view的weight屬性後,能夠將寬度或者高度設爲0dp,這樣能夠提升效率佈局

第二步:MainActivity處理邏輯post

一、初始化Fragmentthis

protected void init() {
        Fragment homeFragment = new HomeFragment();
        Fragment classifyFragment = new ClassifyFragment();
        Fragment discoverFragment = new DiscoverFragment();
        Fragment meFragment = new MeFragment();
        fragments.add(homeFragment);
        fragments.add(classifyFragment);
        fragments.add(discoverFragment);
        fragments.add(meFragment);
    }
  • fragments爲一個盛放Fragment的List集合
  • 其中的一個Fragment的代碼(其餘的同樣,只是顯示的文本不一樣):
package com.lt.bottomtabdemo.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

/**
 * Created by lt on 2015/12/1.
 */
public class HomeFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(params);
        textView.setText("首頁");
        return textView;
    }
}

二、初始化View併爲ViewPager設置Adapter.net

mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
TabPageAdapter tabPageAdapter = new TabPageAdapter(
            getSupportFragmentManager(), fragments);
mViewPager.setAdapter(tabPageAdapter);
mViewPager.setOnPageChangeListener(this);

其中 TagPagerAdapter.java

package com.lt.bottomtabdemo.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by lt on 2015/12/1.
 * app導航內容區域適配器
 */
public class TabPageAdapter extends FragmentPagerAdapter{

    private List<Fragment> fragments;
    public TabPageAdapter(FragmentManager fm,List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

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

    /**
     * 重寫,不讓Fragment銷燬
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }
}

這裏重寫destroyItem什麼都不作,這裏只有四個頁面不必讓系統銷燬Fragment。

三、寫一個切換頁面的方法:

/**
     * 選擇某頁
     * @param position 頁面的位置
     */
    private void selectPage(int position) {
        // 將全部的tab的icon變成灰色的
        for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
            Drawable gray = getResources().getDrawable(unselectedIconIds[i]);
            // 不能少,少了不會顯示圖片
            gray.setBounds(0, 0, gray.getMinimumWidth(),
                    gray.getMinimumHeight());
            RadioButton child = (RadioButton) mRadioGroup.getChildAt(i);
            child.setCompoundDrawables(null, gray, null, null);
            child.setTextColor(getResources().getColor(
                    R.color.dark_gray));
        }
        // 切換頁面
        mViewPager.setCurrentItem(position, false);
        // 改變圖標
        Drawable yellow = getResources().getDrawable(selectedIconIds[position]);
        yellow.setBounds(0, 0, yellow.getMinimumWidth(),
                yellow.getMinimumHeight());
        RadioButton select = (RadioButton) mRadioGroup.getChildAt(position);
        select.setCompoundDrawables(null, yellow, null, null);
        select.setTextColor(getResources().getColor(
                R.color.yellow));
    }

 用到的數組資源,分別是按鈕選中與沒有選中狀態顯示的圖片資源:

/**
     * 按鈕的沒選中顯示的圖標
     */
    private int[] unselectedIconIds = { R.drawable.ic_tab_home_gray,
            R.drawable.ic_tab_classify_gray, R.drawable.ic_tab_discover_gray,
            R.drawable.ic_tab_me_gray };
    /**
     * 按鈕的選中顯示的圖標
     */
    private int[] selectedIconIds = { R.drawable.ic_tab_home_yellow,
            R.drawable.ic_tab_classify_yellow, R.drawable.ic_tab_discover_yellow,
            R.drawable.ic_tab_me_yellow };

 selectPage(int postion)方法說明:

  • 遍歷RadioGroup將全部的RadioButton的上面的圖片和文本顏色設爲沒有選中的狀態:

    • 經過RadioButton.setCompoundDrawables(left, top, right, bottom);變換RadioButton的四個方向的圖片,這裏咱們要換掉上面的圖片,因此先獲得上面的圖片Drawable對象(注意要設置圖片顯示的大小):
    Drawable gray = getResources().getDrawable(unselectedIconIds[i]);
              // 不能少,少了不會顯示圖片,設置顯示的範圍,爲一個矩形
              gray.setBounds(0, 0, gray.getMinimumWidth(),
                      gray.getMinimumHeight());
  • 而後在設置選中的那個RadioButton上面的圖片和文本顏色

這裏的position是指當前頁面對應的tab按鈕RadioButton在RadioGroup中的排序。

四、爲了底部RadioButton點擊後能夠切換到相應的頁面,爲RadioGroup設置按鈕選中改變的監聽:

mRadioGroup.setOnCheckedChangeListener(this);

 實現onCheckedChanged方法

@Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.btn_home: // 首頁選中
                selectPage(0);
                break;
            case R.id.btn_classify: // 分類選中
                selectPage(1);
                break;
            case R.id.btn_discover: // 發現選中
                selectPage(2);
                break;
            case R.id.btn_me: // 我的中心選中
                selectPage(3);
                break;
        }
    }

五、爲了讓ViewPager滑動的同時改變底部按鈕選擇狀態,爲ViewPager設置頁面改變監聽:

mViewPager.setOnPageChangeListener(this);

 實現接口的三個方法(只實現其中的onPageSelected,其它的給空實現):

@Override
    public void onPageSelected(int position) {
        selectPage(position);
    }

OK,到這裏咱們 ViewPager+RadioButton 實現底部導航的例子完成了。

總結:ViewPager+RadioButton這種方式輕鬆實現底部導航。

Demo下載

相關文章
相關標籤/搜索