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. Mysql Replace语句的使用

    Mysql Replace语句的语法: REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name [(col_name,...)] VALUES ({expr ...

  2. [bzoj3991][SDOI2015]寻宝游戏_树链的并_倍增lca_平衡树set

    寻宝游戏 bzoj-3991 SDOI-2015 题目大意:题目链接. 注释:略. 想法:我们发现如果给定了一些点有宝物的话那么答案就是树链的并. 树链的并的求法就是把所有点按照$dfs$序排序然后相 ...

  3. [bzoj2194]快速傅立叶之二_FFT

    快速傅立叶之二 bzoj-2194 题目大意:给定两个长度为$n$的序列$a$和$b$.求$c$序列,其中:$c_i=\sum\limits_{j=i}^{n-1} a_j\times b_{j-i} ...

  4. WCF - 自定义绑定

    自定义绑定 当系统提供的某个绑定不符合服务的要求时,可使用 CustomBinding 类.所有绑定都是从绑定元素的有序集构造而来的.自定义绑定可以从一组系统提供的绑定元素生成,也可以包含用户定义的自 ...

  5. (linux shell)第一章--小试牛刀(下)

    文章来源: (linux shell)第一章--小试牛刀(下) 1.6 数组和关联数组 1.6.1 预备知识 Bash同一时候支持普通数组和关联数组.普通数组仅仅能使用整数作为数组索引,而关联数组能够 ...

  6. Android 圆形/圆角图片的方法

    Android 圆形/圆角图片的方法 眼下网上有非常多圆角图片的实例,Github上也有一些成熟的项目.之前做项目,为了稳定高效都是选用Github上的项目直接用.但这样的结束也是Android开发必 ...

  7. mac 查看python路径

    1,terminal : input: which python 2,  terminal: input : python  --->import sys  ----> print sys ...

  8. ubuntu双网卡绑定配置

    1,安装bonding需要的软件 sudo apt-get install ifenslave 2,在/etc/modules中加入: bonding mode= miimon= 3,在/etc/ne ...

  9. UVA1523-Helicopter(暴力+全排列)

    题目链接 题意:有八个乘客坐在直升机上,求重心M最小值. 思路:依据题目所给的公式,我们能够知道要使得M最小.也就是要使得Mv和Mh的和最小,我们能够使用全排列,分别将每一个值放在各个位子上,然后更新 ...

  10. Paypal支付(一)MPL真正的快捷支付

    一.前导 前面讲到了MEC支付,是在Web端集成好的,在手机端仅仅需通过WebView进行载入就可以,不须要不论什么Paypal第三方架包.以下将的是MPL支付.须要架包. 这样的支付的形式能够參考以 ...