今天发的是TabHost结合ViewPager实现首页底部导航的效果,虽然说网上有很多这样的Demo,不过呢,我还是要把自己练习写的发出来,没错!就是这么任性;
先上效果图,如下:

代码里面有注释,就不过多解释了,说几点需要注意的问题
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=""
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=""
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=""
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=""
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();
tabHost.setCurrentTab(); //getViewPager添加适配器
MyAdapter adapter = new MyAdapter(list);
viewPager.setAdapter(adapter); /**
* viewPager设置滑动监听,根据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 == ){
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 == ){
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 == ){
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 == ){
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设置监听,以此来改变ViewPager的页面
*/
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch (checkedId){
case R.id.radioButton1:
viewPager.setCurrentItem();
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();
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();
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();
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;
}
}
});
} /**
* 初始化数据源
*/
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 ViewPager+TabHost实现首页导航的更多相关文章

  1. ViewPager+GridView实现首页导航栏布局分页效果

    如图是效果图用ViewPager+GridView实现首页导航栏布局分页效果来实现的效果 Demo下载地址:http://download.csdn.net/detail/qq_29774291/96 ...

  2. Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换

    viewPager是v4包里的一个组件,可以实现滑动显示多个界面. android也为viewPager提供了一个adapter,此adapter最少要重写4个方法: public int getCo ...

  3. android使用tabhost实现导航

    参考 http://blog.csdn.net/xixinyan/article/details/6771341 http://blog.sina.com.cn/s/blog_6b04c8eb0101 ...

  4. Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡

     <Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡> 之前基于github上的第三方开源控件ViewPagerIndicator的UnderlinePa ...

  5. Android底部TabHost API

    今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...

  6. Android选项卡TabHost方式实现

    1.布局XML: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android= ...

  7. Android ViewPager 打造炫酷欢迎页

    Android ViewPager 打造炫酷欢迎页 ViewPager是Android扩展v4包中的类,这个类可以让用户切换当前的View.对于这个类的应用场景,稍加修改就可以应用到多个环境下.比如: ...

  8. Android 仿QQ微信开场导航以及登陆界面

    相信大家对于微信等社交应用的UI界面已经都很熟悉了,该UI最值得借鉴的莫过于第一次使用的时候一些列产品介绍的图片,可以左右滑动浏览,最后进入应 用,这一效果适用于多种项目中,相信今后开发应用一定会用得 ...

  9. Android项目--tabhost

    所有牵扯到自定义布局的layout中尽量用RelativeLayout 在通讯录中如果像小米手机的UI那就是viewpager,在这里,我们做成静态的.通过tabhost来做. 1.布局 a) 直接用 ...

随机推荐

  1. 洛谷 P1503 鬼子进村

    题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战. 题目描述 描述 县城里 ...

  2. REST模式中HTTP请求方法(GET,POST,PUT,DELETE)

    一直在测试REST模式的WEB SERVICE接口,客户端的HTTP的请求方式一般分为四种:GET.POST.PUT.DELETE,这四种请求方式有什么不同呢.简单的说,GET就是获取资源,POST就 ...

  3. 一些英文表达-youtube

    culinary tradition 烹饪传统 crunchy 松脆的 boutique  精品店 migraine  偏头痛 colon 冒号 towel 毛巾 ecstatic  狂喜的 bok ...

  4. [转] 完全卸载删除gitlab

    [From] https://yq.aliyun.com/articles/114619 完全卸载删除gitlab 1.停止gitlab gitlab-ctl stop 2.卸载gitlab(注意这里 ...

  5. JAVA 序列化_基础

    JAVA序列化 实现 Serializable 接口的对象,可以序列化为本地文件 简单示例: //序列化类 public class Test implements Serializable { pr ...

  6. C++ GUI Qt4编程(13)-6.2preferencedialog

    1. 主要介绍了QStackedLayout.QListWidget.QDialogButtonBox的简单用法.2. QStackedLayout:   要使某个特定的子窗口部件可见,可以用setC ...

  7. vue filters过滤器的使用

    说的很详细 https://www.w3cplus.com/vue/how-to-create-filters-in-vuejs.html

  8. Python+Selenium定位元素的方法

    Python+Selenium有以下八种定位元素的方法: 1. find_element_by_id() eg: find_element_by_id("kw") 2. find_ ...

  9. oracle 笔记---(五)__内存管理

    ###查看连接池的信息 select connection_pool,status,maxsize from dba_cpool_info            

  10. 【3dsMax安装失败,如何卸载、安装3dMax 2012?】

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...