Android 中 TabHost與ViewPager結合實現(xiàn)首頁導航效果
今天發(fā)的是TabHost結合ViewPager實現(xiàn)首頁底部導航的效果,雖然說網(wǎng)上有很多這樣的Demo,不過呢,我還是要把自己練習寫的發(fā)出來,沒錯!就是這么任性;
先上效果圖,如下:
代碼里面有注釋,就不過多解釋了,說幾點需要注意的問題
1:TabHost 、TabWidget、FrameLayout一定添加id這個屬性,否則會報錯
android:id=”@android:id/tabhost”
android:id=”@android:id/tabcontent”
android:id=”@android:id/tabs”
這個屬性是固定的。
2:ViewPager的適配器要繼承PagerAdapter,別整錯咯;
布局文件xml:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.wgh.tabhostwithviewpager.MainActivity"> <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"></android.support.v4.view.ViewPager> </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" android:paddingLeft="10dp" /> <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>
Activity:
package com.example.wgh.tabhostwithviewpager; import android.app.TabActivity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TabHost; import java.util.ArrayList; 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 = null; private View view2 = null; private View view3 = null; private View view4 = null; private RadioButton radioButton1 = null; private RadioButton radioButton2 = null; private RadioButton radioButton3 = null; private RadioButton radioButton4 = null; @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); //getViewPager添加適配器 MyAdapter adapter = new MyAdapter(list); viewPager.setAdapter(adapter); /** * viewPager設置滑動監(jiān)聽,根據(jù)viewPager選中頁的position,設置tabHost頁卡選項的樣式 */ viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (position == 0){ radioButton1.setBackgroundResource(R.drawable.img_draw_money_success); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); }else if(position == 1){ radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_success); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); }else if(position == 2){ radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_success); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); }else if(position == 3){ radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_success); } } @Override public void onPageScrollStateChanged(int state) { } }); /** * 給radioGroup設置監(jiān)聽,以此來改變ViewPager的頁面 */ radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { switch (checkedId){ case R.id.radioButton1: viewPager.setCurrentItem(0); radioButton1.setBackgroundResource(R.drawable.img_draw_money_success); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); break; case R.id.radioButton2: viewPager.setCurrentItem(1); radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_success); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); break; case R.id.radioButton3: viewPager.setCurrentItem(2); radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_success); radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail); break; case R.id.radioButton4: viewPager.setCurrentItem(3); radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail); radioButton4.setBackgroundResource(R.drawable.img_draw_money_success); break; } } }); } /** * 初始化數(shù)據(jù)源 */ private void initData() { list = new ArrayList<View>(); list.add(view1); list.add(view2); list.add(view3); list.add(view4); } /** * 初始化控件 */ 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); /** * 將每頁要展示的layout實例出來,添加到list里面,最后通過適配器return回來要展示的相應的layout */ 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); } }
ViewPager適配器MyAdapter:
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)); } }
以上所述是小編給大家介紹的Android 中 TabHost與ViewPager結合實現(xiàn)首頁導航效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Android調(diào)用系統(tǒng)照相機拍照與攝像的方法
這篇文章主要為大家詳細介紹了Android如何調(diào)用系統(tǒng)現(xiàn)有的照相機拍照與攝像,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Kotlin?掛起函數(shù)CPS轉(zhuǎn)換原理解析
這篇文章主要為大家介紹了Kotlin?掛起函數(shù)CPS轉(zhuǎn)換原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12Android自定義Dialog實現(xiàn)加載對話框效果
這篇文章將介紹如何定制當今主流的對話框,通過自定義dialog實現(xiàn)加載對話框效果,具體實現(xiàn)代碼大家通過本文學習吧2018-05-05Android加載大分辨率圖片到手機內(nèi)存中的實例方法
有些圖片的分辨率比較高,把它直接加載到手機內(nèi)存中之后,會導致堆內(nèi)存溢出的問題,下面就講解一下Android的堆內(nèi)存以及如何在Android應用中加載一個高分辨率的圖片的方法2013-11-11Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法
今天小編就為大家分享一篇關于Android中Fragment的分屏顯示處理橫豎屏顯示的實現(xiàn)方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Android中RecyclerView上拉下拉,分割線,多條目的實例代碼
這篇文章主要介紹了Android中RecyclerView上拉下拉,分割線,多條目的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-01-01Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使
這篇文章主要介紹了Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使用的相關資料,需要的朋友可以參考下2016-12-12Android開發(fā)手冊shape屬性和子屬性使用說明
這篇文章主要為大家介紹了Android開發(fā)手冊shape屬性和子屬性使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06