Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android ViewPager + TabHost 實現首頁底部導航

編輯:關於 Android 開發

本文記錄一個用 TabHost 結合 ViewPager 實現首頁底部導航的實踐案例。網絡上同類 Demo 很多,這裡重點整理幾個容易踩坑的細節。

先看效果圖:

幾個關鍵注意點

1. 三個 id 屬性必須寫死。TabHost、TabWidget、FrameLayout 一定要使用系統預定義的 id,否則運行時直接報錯:

android:id="@android:id/tabhost"
android:id="@android:id/tabcontent"
android:id="@android:id/tabs"

2. ViewPager 的適配器要繼承 PagerAdapter,別和 FragmentPagerAdapter 的場景搞混。

佈局文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0">

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

        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.0"
            android:visibility="gone" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#0ff0f0" />

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

            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/img_draw_money_fail"
                android:button="@null" />

            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/img_draw_money_fail"
                android:button="@null" />

            <RadioButton
                android:id="@+id/radioButton3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/img_draw_money_fail"
                android:button="@null" />

            <RadioButton
                android:id="@+id/radioButton4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/img_draw_money_fail"
                android:button="@null" />
        </RadioGroup>
    </LinearLayout>
</TabHost>

這裡 TabWidget 被設為 gone,真正的底部按鈕由 RadioGroup 承擔,好處是樣式完全可控。

Activity 代碼

public class MainActivity extends TabActivity {

    private TabHost tabHost = null;
    private ViewPager viewPager = null;
    private RadioGroup radioGroup = null;
    private ArrayList<View> list = null;
    private View view1, view2, view3, view4;
    private RadioButton radioButton1, radioButton2, radioButton3, radioButton4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();

        // 設置默認選中第一項
        radioGroup.check(R.id.radioButton1);
        radioButton1.setBackgroundResource(R.drawable.img_draw_money_success);
        viewPager.setCurrentItem(0);
        tabHost.setCurrentTab(0);

        viewPager.setAdapter(new MyAdapter(list));

        // 左右滑動時同步底部按鈕狀態
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float offset, int offsetPixels) { }

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

            @Override
            public void onPageScrollStateChanged(int state) { }
        });

        // 點擊底部按鈕時切換頁面
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radioButton1:
                        viewPager.setCurrentItem(0);
                        updateRadioState(0);
                        break;
                    case R.id.radioButton2:
                        viewPager.setCurrentItem(1);
                        updateRadioState(1);
                        break;
                    case R.id.radioButton3:
                        viewPager.setCurrentItem(2);
                        updateRadioState(2);
                        break;
                    case R.id.radioButton4:
                        viewPager.setCurrentItem(3);
                        updateRadioState(3);
                        break;
                }
            }
        });
    }

    /** 只讓當前項顯示選中態背景 */
    private void updateRadioState(int position) {
        radioButton1.setBackgroundResource(position == 0
                ? R.drawable.img_draw_money_success : R.drawable.img_draw_money_fail);
        radioButton2.setBackgroundResource(position == 1
                ? R.drawable.img_draw_money_success : R.drawable.img_draw_money_fail);
        radioButton3.setBackgroundResource(position == 2
                ? R.drawable.img_draw_money_success : R.drawable.img_draw_money_fail);
        radioButton4.setBackgroundResource(position == 3
                ? R.drawable.img_draw_money_success : R.drawable.img_draw_money_fail);
    }

    /** 初始化數據源 */
    private void initData() {
        list = new ArrayList<View>();
        list.add(view1);
        list.add(view2);
        list.add(view3);
        list.add(view4);
    }

    /** 初始化控件,並把各頁佈局 inflate 出來 */
    private void initView() {
        tabHost = getTabHost();
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
        radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
        radioButton4 = (RadioButton) findViewById(R.id.radioButton4);

        LayoutInflater inflater = LayoutInflater.from(this);
        view1 = inflater.inflate(R.layout.layout_one, null);
        view2 = inflater.inflate(R.layout.layout_two, null);
        view3 = inflater.inflate(R.layout.layout_three, null);
        view4 = inflater.inflate(R.layout.layout_four, null);
    }
}

原文中四段判斷分支邏輯幾乎完全重複,實踐中抽成 updateRadioState() 這樣的公共方法會清爽很多。

ViewPager 適配器

public class MyAdapter extends PagerAdapter {

    private ArrayList<View> list = null;

    public MyAdapter(ArrayList<View> list) {
        this.list = list;
    }

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

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(list.get(position));
        return list.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));
    }
}

整體思路就是「TabHost 提供骨架 + ViewPager 承擔切換 + RadioGroup 充當自定義底部欄」,三者通過監聽互相同步。有問題歡迎留言交流。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved