国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android 中 TabHost與ViewPager結(jié)合實(shí)現(xiàn)首頁(yè)導(dǎo)航效果

2019-12-12 05:12:26
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

今天發(fā)的是TabHost結(jié)合ViewPager實(shí)現(xiàn)首頁(yè)底部導(dǎo)航的效果,雖然說(shuō)網(wǎng)上有很多這樣的Demo,不過(guò)呢,我還是要把自己練習(xí)寫(xiě)的發(fā)出來(lái),沒(méi)錯(cuò)!就是這么任性;

先上效果圖,如下:

代碼里面有注釋?zhuān)筒贿^(guò)多解釋了,說(shuō)幾點(diǎn)需要注意的問(wèn)題

1:TabHost 、TabWidget、FrameLayout一定添加id這個(gè)屬性,否則會(huì)報(bào)錯(cuò)

android:id=”@android:id/tabhost”

android:id=”@android:id/tabcontent”

android:id=”@android:id/tabs”

這個(gè)屬性是固定的。

2:ViewPager的適配器要繼承PagerAdapter,別整錯(cuò)咯;

布局文件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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1.0"><android.support.v4.view.ViewPagerandroid:id="@+id/viewPager"android:layout_width="match_parent"android:layout_height="match_parent"></android.support.v4.view.ViewPager></FrameLayout><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="0.0"android:visibility="gone" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#0ff0f0" /><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid: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" /><RadioButtonandroid: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" /><RadioButtonandroid: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" /><RadioButtonandroid: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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initData();//設(shè)置初始化默認(rèn)選項(xiàng)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設(shè)置滑動(dòng)監(jiān)聽(tīng),根據(jù)viewPager選中頁(yè)的position,設(shè)置tabHost頁(yè)卡選項(xiàng)的樣式*/viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic 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);}}@Overridepublic void onPageScrollStateChanged(int state) {}});/*** 給radioGroup設(shè)置監(jiān)聽(tīng),以此來(lái)改變ViewPager的頁(yè)面*/radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic 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);/*** 將每頁(yè)要展示的layout實(shí)例出來(lái),添加到list里面,最后通過(guò)適配器return回來(lái)要展示的相應(yīng)的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;}@Overridepublic int getCount() {return list.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {container.addView(list.get(position));return list.get(position);}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView(list.get(position));}}

以上所述是小編給大家介紹的Android 中 TabHost與ViewPager結(jié)合實(shí)現(xiàn)首頁(yè)導(dǎo)航效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 皮山县| 拜泉县| 集贤县| 永州市| 湖州市| 黄龙县| 乐昌市| 武汉市| 清镇市| 嘉兴市| 山阳县| 离岛区| 定州市| 姜堰市| 安丘市| 泽州县| 墨脱县| 饶阳县| 信阳市| 嘉黎县| 温宿县| 运城市| 天气| 三明市| 嫩江县| 集贤县| 邵阳市| 那曲县| 荥经县| 东兴市| 梁河县| 山阳县| 淄博市| 祥云县| 准格尔旗| 阜宁县| 宝鸡市| 阿城市| 惠州市| 明星| 灵丘县|