Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换
viewPager是v4包里的一个组件,可以实现滑动显示多个界面。
android也为viewPager提供了一个adapter,此adapter最少要重写4个方法:
public int getCount()
public boolean isViewFromObject(View view, Object o)
public void destroyItem(ViewGroup container, int position, Object object)
public Object instantiateItem(ViewGroup container, int position)
这些方法的作用我就不说了,在代码里面有详细的解释。
接下来就直接上代码吧!!
MainActivity.java
package com.example.administrator.viewpagerdemo; import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabWidget; import java.util.ArrayList;
import java.util.List; public class MainActivity extends Activity { private ViewPager viewPager = null;
private List<View> viewContainter = new ArrayList<View>(); //存放容器
private ViewPagerAdapter viewPagerAdapter = null; //声明适配器 private TabHost mTabHost = null;
private TabWidget mTabWidget = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMyTabHost(); //初始化TabHost
// 绑定组件
viewPager = (ViewPager) findViewById(R.id.viewpager);
initViewPagerContainter(); //初始viewPager
viewPagerAdapter = new ViewPagerAdapter();
//设置adapter的适配器
viewPager.setAdapter(viewPagerAdapter);
//设置viewPager的监听器
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
//当 滑动 切换时
@Override
public void onPageSelected(int position) {
mTabWidget.setCurrentTab(position);
}
@Override
public void onPageScrollStateChanged(int state) { }
});
//TabHost的监听事件
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if(tabId.equals("tab1")){
viewPager.setCurrentItem(0);
}else if (tabId.equals("tab2")){
viewPager.setCurrentItem(1);
}else if (tabId.equals("tab3")){
viewPager.setCurrentItem(2);
}else{
viewPager.setCurrentItem(3);
}
}
}); //解决开始时不显示viewPager
mTabHost.setCurrentTab(1);
mTabHost.setCurrentTab(0);
} //初始化TabHost
public void initMyTabHost(){
//绑定id
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabWidget = mTabHost.getTabWidget();
/**
* newTabSpec() 就是给每个Tab设置一个ID
* setIndicator() 每个Tab的标题
* setCount() 每个Tab的标签页布局
*/
mTabHost.addTab(mTabHost.newTabSpec("tab1").setContent(R.id.tab1).setIndicator("第一页"));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setContent(R.id.tab2).setIndicator("第二页"));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setContent(R.id.tab3).setIndicator("第三页"));
mTabHost.addTab(mTabHost.newTabSpec("tab4").setContent(R.id.tab4).setIndicator("第四页"));
} //初始化viewPager
public void initViewPagerContainter(){
//建立四个view的样式,并找到他们
View view_1 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.viewpager_view_1,null);
View view_2 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.viewpager_view_2,null);
View view_3 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.viewpager_view_3,null);
View view_4 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.viewpager_view_4,null);
//加入ViewPage的容器
viewContainter.add(view_1);
viewContainter.add(view_2);
viewContainter.add(view_3);
viewContainter.add(view_4);
} //内部类实现viewpager的适配器
private class ViewPagerAdapter extends PagerAdapter{ //该方法 决定 并 返回 viewpager中组件的数量
@Override
public int getCount() {
return viewContainter.size();
} @Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
//滑动切换的时候,消除当前组件
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewContainter.get(position));
}
//每次滑动的时候生成的组件
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewContainter.get(position));
return viewContainter.get(position);
}
}
}
activity_main.xml
在使用TabHost和TabWidget时,指定他们的id时时,android的已经为他定义好了,并且必须为他们指定这个id。而且TabHost里面必须要包含TabWedght和FrameLayout
切他们的id也必须为android已经给定的,就是下面xml代码里的id,否则会报错
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:layout_weight="9">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<TextView
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tab4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout> </LinearLayout>
</TabHost>
我们再为viewPager添加View的时候,需要建立他们的布局文件,在上面的代码,我建立了4个xml,内容是一样的,都放了一个ImageView
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:layout_weight="9">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<TextView
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tab4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout> </LinearLayout>
</TabHost>
这样做出来的Demo,首次启动时会出现无法显示画面的问题,只有当点击一次Tab的时候才会显示。为了解决这个问题,我在MainActivity的onCreate()方法的
最后面加入了这两句话,但是还是不明白,为什么我只 mTabHost.setCurrentTab(0);不起作用,希望知道的的小伙伴各异给我解答一下疑惑,谢谢。
//解决开始时不显示viewPager
mTabHost.setCurrentTab(1);
mTabHost.setCurrentTab(0);
下面是效果图:

Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换的更多相关文章
- iOS导航栏-导航栏透明
设置一张透明图片:nav_bargound.png //导航栏背景 [self.navigationController.navigationBar setBackgroundImage:[ ...
- MTK Android SwitchPreference(设置-智能辅助-导航栏-导航栏可隐藏)
1.界面布局文件 packages/apps/PrizeSettings/res/xml/navigation_bar_prize.xml ------------------------------ ...
- swift导航栏导航按钮添加多个按钮事件
//导航左边返回按钮 let button1 = UIButton(frame:CGRectMake(0, 0, 18, 18)) button1.setImage(Constant.Image.Na ...
- Android中使用ViewPager实现屏幕页面切换和页面切换效果
之前关于如何实现屏幕页面切换,写过一篇博文<Android中使用ViewFlipper实现屏幕切换>,相比ViewFlipper,ViewPager更适用复杂的视图切换,而且Viewpag ...
- Android ScrollView嵌套ViewPager,嵌套的ViewPager无法显示
记录:ScrollView嵌套ViewPager,嵌套的ViewPager无法显示 项目中所需要布局:LinearLayout中包含(orientation="vertical") ...
- iOS系统中导航栏的转场解决方案与最佳实践
背景 目前,开源社区和业界内已经存在一些 iOS 导航栏转场的解决方案,但对于历史包袱沉重的美团 App 而言,这些解决方案并不完美.有的方案不能满足复杂的页面跳转场景,有的方案迁移成本较大,为此我们 ...
- 03 uniapp自定义导航栏的开发
在我眼里自定义导航分2类: 原生基础上 || 非原生基础上 总结:项目当中能原生就原生,提高性能 区别 uni-app 自带原生导航栏,在pages.json里配置. 原生导航的体验更好,渲染新页面时 ...
- Bootstrap<基础十七>导航栏
导航栏是一个很好的功能,是 Bootstrap 网站的一个突出特点.导航栏在您的应用或网站中作为导航页头的响应式基础组件.导航栏在移动设备的视图中是折叠的,随着可用视口宽度的增加,导航栏也会水平展开. ...
- BootStrap导航栏的使用
默认的导航栏 创建一个默认的导航栏的步骤如下: 向 <nav> 标签添加 class .navbar..navbar-default. 向上面的元素添加 role="naviga ...
随机推荐
- nginx+lua+redis初体验
1.下载nginx.lua.redis nginx下载地址 wget http://nginx.org/download/nginx-1.8.0.tar.gz lua下载地址 wget http:/ ...
- linx 实用操作命令二
pcre相关操作pcretest -C #查看pcre是否支持utf8 rpm -q -a #查看软件包列表rpm -ql pcre-7.8-6.el6.x86_64 #查看包安装的位置rpm -e ...
- java服务器
WebLogic BEA公司开发的(被Oracle收购了)收费的 支持JavaEE所有的规范(ejb servlet/jsp规范) java mysql(oracle) 2.WebSphe ...
- PC上安装MAC X Lion
PC上安装MACXLion 网上关于如何在PC下安装MAC的文章已近不少了,但对于一些初学者在实践当中会遇到各种问题,以下视频资料为大家展示两种虚拟机安装MacOS. 1.VmwareWorkstat ...
- 动态链接库(VC_Win32)
目录 动态链接库概述相关函数动态链接库编程dumpbin工具 (本章节中例子都是用 VS2005 编译调试的) 动态链接概述 说明 所谓动态链接,就是把一些经常会共用的代码(静态链接的OBJ程序库)制 ...
- ArcGIS10.2最新全套下载地址
http://www.tuicool.com/articles/VfaMfy 免责声明: 该链接来自于哥伦比亚大学或者牛津大学的网站链接, 下载 软件之前确保有正版的软件授权 ,本博客只是转载了网站链 ...
- 【MySQL】binlog_format以及binlog事务记录分析
MySQL官方对于binlog_format参数的说明: http://dev.mysql.com/doc/refman/5.5/en/binary-log-setting.html binlog_f ...
- ios开发之OC基础-oc小程序
本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...
- PHP 统计中文字符串的长度
中文网站一般会选择两种编码:gbk/gb2312或是utf-8. gbk编码下每个中文字符所占字节为2,例: $zhStr = ‘您好,中国!’; echo strlen($zhStr); // 输出 ...
- 第六章_PHP数组
1.PHP支持两种数组:索引数组(indexed array)和关联数组(associative array),前者使用数字作为键,后者使用字符串作为键. 2.遍历索引数组 2.1 for循环语句 2 ...