最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年09月21日 14:22:07   投稿:mrr  
這篇文章主要介紹了Android 中 TabHost與ViewPager結(jié)合實(shí)現(xiàn)首頁導(dǎo)航效果,代碼簡單易懂,感興趣的朋友參考下吧

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

先上效果圖,如下:

代碼里面有注釋,就不過多解釋了,說幾點(diǎ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">
<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();
//設(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)聽,根據(jù)viewPager選中頁的position,設(shè)置tabHost頁卡選項(xiàng)的樣式
*/
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設(shè)置監(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實(shí)例出來,添加到list里面,最后通過適配器return回來要展示的相應(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;
}
@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結(jié)合實(shí)現(xiàn)首頁導(dǎo)航效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • EditText監(jiān)聽方法,實(shí)時(shí)的判斷輸入多少字符

    EditText監(jiān)聽方法,實(shí)時(shí)的判斷輸入多少字符

    在EditText提供了一個(gè)方法addTextChangedListener實(shí)現(xiàn)對(duì)輸入文本的監(jiān)控。本文分享了EditText監(jiān)聽方法案例,需要的朋友一起來看下吧
    2016-12-12
  • 淺析Android的啟動(dòng)原理

    淺析Android的啟動(dòng)原理

    當(dāng)談到Android啟動(dòng)原理時(shí),我們進(jìn)入了Android操作系統(tǒng)的核心,理解Android系統(tǒng)啟動(dòng)的原理對(duì)于開發(fā)者來說非常重要,因?yàn)檫@有助于優(yōu)化應(yīng)用程序性能并提供更好的用戶體驗(yàn),本文給大家講講Android啟動(dòng)原理,需要的朋友可以參考下
    2023-10-10
  • Android RecyclerView使用方法詳解

    Android RecyclerView使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android RecyclerView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Flutter集成高德地圖并添加自定義Maker的實(shí)踐

    Flutter集成高德地圖并添加自定義Maker的實(shí)踐

    本文主要介紹了Flutter集成高德地圖并添加自定義Maker的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Flutter實(shí)現(xiàn)簡單的內(nèi)容高亮效果

    Flutter實(shí)現(xiàn)簡單的內(nèi)容高亮效果

    內(nèi)容高亮并不陌生,特別是在搜索內(nèi)容頁面,可以說四處可見,這篇文章主要為大家介紹了如何使用Flutter實(shí)現(xiàn)簡單的內(nèi)容高亮效果,需要的可以參考下
    2023-08-08
  • Android自定義View實(shí)現(xiàn)五子棋游戲

    Android自定義View實(shí)現(xiàn)五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android SQLite數(shù)據(jù)庫版本升級(jí)的管理實(shí)現(xiàn)

    Android SQLite數(shù)據(jù)庫版本升級(jí)的管理實(shí)現(xiàn)

    這篇文章主要介紹了Android SQLite數(shù)據(jù)庫版本升級(jí)的管理實(shí)現(xiàn)的相關(guān)資料,這里提供實(shí)現(xiàn)代碼幫助大家掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Android?Choreographer源碼詳細(xì)分析

    Android?Choreographer源碼詳細(xì)分析

    Choreographer的作用主要是配合Vsync,給上層App的渲染提供一個(gè)穩(wěn)定的Message處理的時(shí)機(jī),也就是Vsync到來的時(shí)候,系統(tǒng)通過對(duì)Vsync信號(hào)周期的調(diào)整,來控制每一幀繪制操作的時(shí)機(jī)
    2022-08-08
  • Android Studio導(dǎo)入so文件到項(xiàng)目中的實(shí)例詳解

    Android Studio導(dǎo)入so文件到項(xiàng)目中的實(shí)例詳解

    這篇文章主要介紹了Android Studio導(dǎo)入so文件到項(xiàng)目中的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • android仿微信好友列表功能

    android仿微信好友列表功能

    這篇文章主要介紹了android仿微信好友列表功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2018-04-04

最新評(píng)論

忻州市| 无锡市| 曲水县| 河东区| 孟津县| 大邑县| 信丰县| 沐川县| 甘孜| 东乡县| 新竹市| 迁西县| 临沂市| 屏山县| 韶关市| 平阳县| 广昌县| 故城县| 双牌县| 句容市| 彭山县| 淮滨县| 连城县| 虞城县| 绥阳县| 金堂县| 绥中县| 中超| 平乐县| 会理县| 大宁县| 景东| 托克逊县| 宝应县| 汤阴县| 周至县| 康乐县| 漠河县| 兴和县| 蚌埠市| 甘肃省|