1.示例

2.代码

2.1 TabViewPagerMain.java

 import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView; import com.txw.e.viewpager.R; public class TabViewPagerMain extends Fragment { //1,在layout.xml中添加 ViewPagerFragment,它可以是顶级布局,如下:
/*
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPagerFragment
android:id="@+id/state_view_pager"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
*/ //2,准备变量,ViewPager和 PagerAdapter
ViewPager pager;
TabViewPagerAdapter pagerAdapter; //3,初始化pager
void initPager(View v){
//从layout.xml中初始化pager
pager = (ViewPager) v.findViewById(R.id.tab_view_pager); //初始化page adapter
pagerAdapter = new TabViewPagerAdapter(getFragmentManager()); //设置adapter
pager.setAdapter(pagerAdapter); //设置page切换监听者
pager.addOnPageChangeListener(pageChangeListener); //设置pager切换动画
// pager.setPageTransformer(true, new DepthPageTransformer());
} //4,处理 pager 切换事件 /**
* This method will be invoked when the current page is scrolled, either as
* part of a programmatically initiated smooth scroll or a user initiated
* touch scroll.
*
* @param position
* Position index of the first page currently being displayed.
* Page position+1 will be visible if positionOffset is nonzero.
* 当positionOffset不为0时,就说明有划动,这时屏幕会同时显示两个page,各显示一部分。
* 这时position就是第一个page的下标。
* @param positionOffset
* Value from [0, 1) indicating the offset from the page at position.
* 是当前页面滑动比例,如果页面向左翻动,这个值不断变大,最后在趋近1的情况后突变为0。
* 如果页面向右翻动,这个值不断变小,最后变为0。
* 当前屏幕上显示的两个page中第一个page相对于屏幕中间偏移量,如果越小说明越近中心,说明第1个page向右移动。
* 越大说明第1个page正在远离中心。就是向左移动。
* @param positionOffsetPixels
* Value in pixels indicating the offset from position.
*/
/* */
ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
System.out.print("onPageScrolled : ");
System.out.print("position " + position);
System.out.print("\tpositionOffset " + positionOffset);
System.out.println("\tpositionOffsetPixels " + positionOffsetPixels);
if (positionOffset != ){
View lTab = tabWidget.getChildAt(position);
View rTab = tabWidget.getChildAt(position + ); lTab.setSelected(true);
rTab.setSelected(true); lTab.setAlpha(1.35f - positionOffset);
rTab.setAlpha(positionOffset + 0.35f);
}else{
for (int i = ; i < tabWidget.getChildCount() ; ++i){
View tab = tabWidget.getChildAt(i);
tab.setAlpha(1.0f);
if (i == position){
tab.setSelected(true);
}else{
tab.setSelected(false);
}
}
}
}
/**
* This method will be invoked when a new page becomes selected. Animation
* is not necessarily complete.
*
* @param position
* Position index of the new selected page.
*/
// 一个新页被调用时执行,仍为原来的page时,该方法不被调用
@Override
public void onPageSelected(int position) {
System.out.println("onPageSelected " + position); } /**
* Called when the scroll state changes. Useful for discovering when the
* user begins dragging, when the pager is automatically settling to the
* current page, or when it is fully stopped/idle.
*
* @param state
* The new scroll state.
* @see ViewPager#SCROLL_STATE_IDLE
* @see ViewPager#SCROLL_STATE_DRAGGING
* @see ViewPager#SCROLL_STATE_SETTLING
*/
/*
* SCROLL_STATE_IDLE: pager处于空闲状态
* SCROLL_STATE_DRAGGING: pager处于正在拖拽中
* SCROLL_STATE_SETTLING: pager正在自动沉降,相当于松手后,pager恢复到一个完整pager的过程
*/
@Override
public void onPageScrollStateChanged(int state) {
System.out.println("onPageScrollStateChanged " + state);
}
}; //5,重写切换动画类,可以把这个类放到外面去。
public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f; public void transformPage(View view, float position) {
int pageWidth = view.getWidth(); if (position < -) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(); } else if (position <= ) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha();
view.setTranslationX();
view.setScaleX();
view.setScaleY(); } else if (position <= ) { // (0,1]
// Fade the page out.
view.setAlpha( - position); // Counteract the default slide transition
view.setTranslationX(pageWidth * -position); // Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ ( - MIN_SCALE) * ( - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor); } else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha();
}
}
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_tab_pager_main, container, false); initPager(v); initTabWidget(v); return v;
} //初始化tab栏
void initTabWidget(View v){
tabWidget = (LinearLayout) v.findViewById(R.id.tab_widget);
tab0 = (TextView) tabWidget.findViewById(R.id.tab0);
tab1 = (TextView) tabWidget.findViewById(R.id.tab1);
tab2 = (TextView) tabWidget.findViewById(R.id.tab2);
tab3 = (TextView) tabWidget.findViewById(R.id.tab3); Drawable top ;
top = tab0.getResources().getDrawable(R.drawable.tab_session_state);
top.setBounds(, , , );
tab0.setCompoundDrawables(null,top,null,null); top = tab0.getResources().getDrawable(R.drawable.tab_contacts_state);
top.setBounds(, , , );
tab1.setCompoundDrawables(null,top,null,null); top = tab0.getResources().getDrawable(R.drawable.tab_discovery_state);
top.setBounds(,,,);
tab2.setCompoundDrawables(null,top,null,null); top = tab0.getResources().getDrawable(R.drawable.tab_personal_state);
top.setBounds(,,,);
tab3.setCompoundDrawables(null,top,null,null); tab0.setOnClickListener(clickListener);
tab1.setOnClickListener(clickListener);
tab2.setOnClickListener(clickListener);
tab3.setOnClickListener(clickListener);
} //下面是tab栏及它的事件
LinearLayout tabWidget;
TextView tab1,tab2,tab3,tab0; //tab 栏事件
View.OnClickListener clickListener = new View.OnClickListener() { @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tab0:
pager.setCurrentItem(, false);
break;
case R.id.tab1:
pager.setCurrentItem(, false);
break;
case R.id.tab2:
pager.setCurrentItem(, false);
break;
case R.id.tab3:
pager.setCurrentItem(, false);
break;
}
}
}; }

2.2 TabViewPagerAdapter.java

 import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; public class TabViewPagerAdapter extends FragmentPagerAdapter{ SessionTab sessionTab;
ContactsTab contactsTab;
DiscoveryTab discoveryTab;
PersonalTab personalTab;
public TabViewPagerAdapter(FragmentManager fm) {
super(fm);
} @Override
public Fragment getItem(int position) { switch (position){
case :if (sessionTab == null) sessionTab = new SessionTab(); return sessionTab;
case :if (contactsTab == null) contactsTab = new ContactsTab(); return contactsTab;
case :if (discoveryTab == null) discoveryTab = new DiscoveryTab(); return discoveryTab;
case :if (personalTab == null) personalTab = new PersonalTab(); return personalTab;
}
return null;
} @Override
public int getCount() {
return ;
}
}

2.4 SessionTab.java

 import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView; import com.txw.e.viewpager.R; import java.util.ArrayList; public class SessionTab extends Fragment { ListView listView;
ArrayAdapter<String> adapter;
ArrayList<String> items; void init(){
items = new ArrayList<>();
for (int i = ; i < ; ++i){
items.add("session " + i);
}
adapter = new ArrayAdapter<>(listView.getContext(),android.R.layout.simple_list_item_1,android.R.id.text1,items); listView.setAdapter(adapter);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_session_tab, container, false);
listView = (ListView) v.findViewById(R.id.session_list); init(); return v;
} }

2.5 ContactsTab.java

 import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView; import com.txw.e.viewpager.R; import java.util.ArrayList; public class ContactsTab extends Fragment { ListView listView;
ArrayAdapter<String> adapter;
ArrayList<String> items; void init(){
items = new ArrayList<>();
for (int i = ; i < ; ++i){
items.add("contact " + i);
}
adapter = new ArrayAdapter<>(listView.getContext(),android.R.layout.simple_list_item_1,android.R.id.text1,items); listView.setAdapter(adapter);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_contacts_tab, container, false);
listView = (ListView) v.findViewById(R.id.contact_list); init(); return v;
} }

2.6 DiscoveryTab.java

 import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.txw.e.viewpager.R; public class DiscoveryTab extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_discovery_tab, container, false);
} }

2.7 PersonalTab.java

 import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.txw.e.viewpager.R; public class PersonalTab extends Fragment { public PersonalTab() {
// Required empty public constructor
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_personal_tab, container, false);
} }

3.xml

3.1 fragment_tab_pager_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
android:id="@+id/pager_layout"
> <android.support.v4.view.ViewPager
android:id="@+id/tab_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/> <LinearLayout
android:id="@+id/tab_widget"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:background="#d1d1d1"
android:padding="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="微信"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal|center_vertical"
android:id="@+id/tab0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="联系人"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal|center_vertical"
android:id="@+id/tab1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="发现"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal|center_vertical"
android:id="@+id/tab2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="我"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal|center_vertical"
android:id="@+id/tab3"/> </LinearLayout> </LinearLayout>

3.2 fragment_session_tab.xml

 <LinearLayout 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:orientation="vertical"
tools:context=".TabViewPager.SessionTab"
android:background="#FFFFFF">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="session"/>
<ListView
android:id="@+id/session_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>

3.3 fragment_contacts_tab.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:background="#FFFFFF"
tools:context="com.txw.e.viewpager.TabViewPager.ContactsTab"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="contacts"/>
<ListView
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>

3.4 fragment_discovery_tab.xml

 <LinearLayout 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:orientation="vertical"
android:background="#FFFFFF"
tools:context="com.txw.e.viewpager.TabViewPager.DiscoveryTab"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="discovery"/> <AnalogClock
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/analogClock"
android:layout_gravity="center_horizontal|center_vertical"/> <Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chronometer"
android:layout_gravity="center_horizontal"/> </LinearLayout>

3.5 fragment_personal_tab.xml

 <ScrollView 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:background="#FFFFFF"
> <LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.txw.e.viewpager.TabViewPager.PersonalTab"> <!-- TODO: Update blank fragment layout --> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="personal"/> <DatePicker
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/datePicker"/> </LinearLayout>
</ScrollView>

ViewPager(3)用viewpager实现tabhost的更多相关文章

  1. Android ViewPager 里有子ViewPager的事件冲突

    在Android应用中有时候要用到类似网易新闻左右滑动页面且页面里又有左右滑动的图片功能,我不知道网易是怎么实现的,本人的做法是外面的BaseFragmentActivity布局就是TabViewPa ...

  2. 【Android开发日记】之入门篇(十五)——ViewPager+自定义无限ViewPager

    ViewPager 在 Android 控件中,ViewPager 一直算是使用率比较高的控件,包括首页的banner,tab页的切换都能见到ViewPager的身影. viewpager 来源自 v ...

  3. andorid自己定义ViewPager之——子ViewPager滑到边缘后直接滑动父ViewPager

    近期的项目中,有一个需求要用ViewPager中嵌套ViewPager去实现整个效果.没做不论什么处理做出来后,仅仅能不停的滑动子ViewPager,父ViewPager就无法滑动了,这样肯定是不满足 ...

  4. 【原创】【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析   1.使用场景 ViewPager+Fragment实现界面切换,界面数量>=3   2.Fragment生命周期以及与Activ ...

  5. 自定义的带tab的可左右滑动的viewpager之二viewpager与fragment不兼容

    总的来说,这个TAB用起来还算方便吧 不过随着用的地方多起来,发现了一些问题,比如下面这个界面: TAB1和TAB2都是表单,保存按钮对两个TAB都有效:若当前显示TAB1,点击保存则保存TAB1的f ...

  6. 转载【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析  原文链接 http://www.cnblogs.com/monodin/p/3866441.html 1.使用场景 ViewPager+ ...

  7. 解决Fragment中使用ViewPager时,ViewPager里的Fragment错位和空白问题

    这两天开始在改OSChina的开源android客户端,打算用Fragment来分离Main这个Activity里的功能.用Fragment嵌套ViewPager+Fragment的时候发现问题. 红 ...

  8. ViewPager嵌套ViewPager后子ViewPager滑动不正常问题

    ViewPager嵌套ViewPager后,滑动事件没法在子ViewPager里面响应. 解决办法是自定义子ViewPager. 以下代码是转载的,经本人测试,可以用!!! 转载地址:http://b ...

  9. ViewPager(4)用viewpager实现splash view

    1,示例 2,代码 SplashMain.java import android.os.Bundle; import android.support.v4.app.Fragment; import a ...

随机推荐

  1. POJ1276 Cash Machine

    Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu Description A Bank pla ...

  2. 【ZJOI2017 Round1练习&BZOJ4773】D3T1 cycle(最小负环,倍增)

    题意:给定一个带权有向图,求点数最小的负环. 2 ⩽ n ⩽ 3000 ⩽ m ⩽ n(n - 1)1 ⩽ ui,vi ⩽ nabs(w[j])<= 10^4 思路:倍增思想 设d[i,j,k] ...

  3. Redis 配置【十】

    参考:http://www.runoob.com/redis/redis-conf.html Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONF ...

  4. MongoDB小结22 - id生成规则

    MongoDB的文档必须有一个_id键. 目的是为了确认在集合里的每个文档都能被唯一标识. ObjectId 是 _id 的默认类型. ObjectId 采用12字节的存储空间,每个字节两位16进制数 ...

  5. Java使用Memcached和Redis简单示例

    package xmq.study.memcached; import java.io.IOException; import java.net.InetSocketAddress; import n ...

  6. pkill有的时候并不能杀死进程?

    pkill的用法:http://man.linuxde.net/pkill 根据进程命令行,杀死进程 如下intellij.go代码为一个代理服务器,把本地请求转向一个代理 package main ...

  7. debug 和release 的区别

    http://blog.csdn.net/h_wlyfw/article/details/26688677

  8. Guava ---- EventBus事件驱动模型

    在软件开发过程中, 难免有信息的共享或者对象间的协作. 怎样让对象间信息共享高效, 而且耦合性低. 这是一个难题. 而耦合性高将带来编码改动牵一发而动全身的连锁效应. Spring的风靡正是由于攻克了 ...

  9. iOS开发——基础篇——get和post请求的区别

    HTTP 定义了与服务器交互的不同方法,最常用的有4种,Get.Post.Put.Delete,如果我换一下顺序就好记了,Put(增),Delete(删),Post(改),Get(查),即增删改查,下 ...

  10. Android开发之接收系统广播消息

    BroadcastReceiver除了接收用户所发送的广播消息之外.另一个重要的用途:接收系统广播. 假设应用须要在系统特定时刻运行某些操作,就能够通过监听系统广播来实现.Android的大量系统事件 ...