Android ViewPager+TabHost实现首页导航
今天发的是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实现首页导航的更多相关文章
- ViewPager+GridView实现首页导航栏布局分页效果
如图是效果图用ViewPager+GridView实现首页导航栏布局分页效果来实现的效果 Demo下载地址:http://download.csdn.net/detail/qq_29774291/96 ...
- Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换
viewPager是v4包里的一个组件,可以实现滑动显示多个界面. android也为viewPager提供了一个adapter,此adapter最少要重写4个方法: public int getCo ...
- android使用tabhost实现导航
参考 http://blog.csdn.net/xixinyan/article/details/6771341 http://blog.sina.com.cn/s/blog_6b04c8eb0101 ...
- Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡
<Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡> 之前基于github上的第三方开源控件ViewPagerIndicator的UnderlinePa ...
- Android底部TabHost API
今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...
- Android选项卡TabHost方式实现
1.布局XML: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android= ...
- Android ViewPager 打造炫酷欢迎页
Android ViewPager 打造炫酷欢迎页 ViewPager是Android扩展v4包中的类,这个类可以让用户切换当前的View.对于这个类的应用场景,稍加修改就可以应用到多个环境下.比如: ...
- Android 仿QQ微信开场导航以及登陆界面
相信大家对于微信等社交应用的UI界面已经都很熟悉了,该UI最值得借鉴的莫过于第一次使用的时候一些列产品介绍的图片,可以左右滑动浏览,最后进入应 用,这一效果适用于多种项目中,相信今后开发应用一定会用得 ...
- Android项目--tabhost
所有牵扯到自定义布局的layout中尽量用RelativeLayout 在通讯录中如果像小米手机的UI那就是viewpager,在这里,我们做成静态的.通过tabhost来做. 1.布局 a) 直接用 ...
随机推荐
- C++_类入门4-String类
很多应用程序都需要处理字符串.C语言在string.h(C++中为cstring)中提供了一系列的字符串函数,很多早期的C++实现为处理字符串提供了自己的类. string类是由头文件string支持 ...
- 一款不错的Linux终端颜色设置
PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[36;40m\]\w\[\e[0m\]]\\$ " #步骤# vi ...
- 洛谷 P2680 运输计划(NOIP2015提高组)(BZOJ4326)
题目背景 公元 \(2044\) 年,人类进入了宇宙纪元. 题目描述 公元\(2044\) 年,人类进入了宇宙纪元. L 国有 \(n\) 个星球,还有 \(n-1\) 条双向航道,每条航道建立在两个 ...
- UESTC - 1610 递推方程+矩阵快速幂
感觉像是HDU Keyboard的加强版,先推出3张牌时的所有组合,然后递推出n张牌 看到n=1e18时吓尿了 最后24那里还是推错了.. (5行1列 dp[1][n],dp[2][n],dp[3][ ...
- 利用touchslide实现tab滑动切换
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- [转] 完全卸载删除gitlab
[From] https://yq.aliyun.com/articles/114619 完全卸载删除gitlab 1.停止gitlab gitlab-ctl stop 2.卸载gitlab(注意这里 ...
- hdu-1702-栈和队列
ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- Promise 多重链式调用
Promise对象是用于异步操作的. Promise的真正强大之处在于它的多重链式调用,可以避免层层嵌套回调.如果我们在第一次ajax请求后,还要用它返回的结果再次请求呢? 使用Promise,我们就 ...
- SQL Server Reporting Service(SSRS) 第七篇 常见错误汇总
1. The current action cannot be completed. The user data source credentials do not meet the requirem ...
- stream4
import java.util.Comparator; import java.util.function.BinaryOperator; public class BinaryOperatorTe ...