編輯:關於 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 承擔,好處是樣式完全可控。
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() 這樣的公共方法會清爽很多。
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 充當自定義底部欄」,三者通過監聽互相同步。有問題歡迎留言交流。
Android 開發環境配置:Eclipse 關聯源碼與源碼/JAR 查看工具
想更深入理解 Android 底層運作,關聯系統源碼、選對查看工具是必備技能。本文介紹三項實用配置。 1、Eclipse 關聯源碼 步驟一:自行下載對應版本的源碼,本文以
IntelliJ/Android Studio技巧:生成getter、setter時自動去除變量前綴
IntelliJ/Android Studio技巧:生成getter、setter時自動去除變量前綴 當我們定義的變量名帶有前綴(例如m),卻不希望生成它的getter(
Unity Shaders and Effects Cookbook(4-1、4-2):靜態立方體貼圖的建立與使用
Unity Shaders and Effects Cookbook(4-1、4-2):靜態 Cubemap 的建立與使用 開始學習第 4 章「著色器的反射」。看完前兩節
新浪微博客戶端開發實錄:啟動載入頁的 UI 實現與屏幕適配
這篇記錄的是自己動手做新浪微博客戶端的過程,本篇聚焦第一個用戶接觸到的界面——啟動載入頁。 這個頁面對用戶而言只是 1、2 秒的等待,但對程序來說是一段寶貴的准備時間:在