要点: 
1、重写组件public boolean onInterceptTouchEvent(MotionEvent event)方法 
2、正确使用requestDisallowInterceptTouchEvent(boolean flag)方法 
关于以上两个方法,请大家多看看相关介绍,这里就不在叙述了^_^

接下来上例子: 
1、外层ViewPager布局 (假定文件名为viewpager_layout.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:orientation="vertical" > 
<android.support.v4.view.ViewPager 
android:id="@+id/viewpager" 
android:layout_width="fill_parent" 
android:layout_height="300dp" > 
</android.support.v4.view.ViewPager> 
</LinearLayout> 

2、里层ViewPager布局(假定文件名为child_viewpager_layout.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:gravity="center" 
android:orientation="vertical" > 
<!--com.android.mylistview.view.MyLayout为自定义的布局,主要是为了重写public boolean onInterceptTouchEvent(MotionEvent event)方法--> 
<com.android.mylistview.view.MyLayout 
android:id="@+id/mylayout" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" > 
<android.support.v4.view.ViewPager 
android:id="@+id/child_viewpager" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_above="@+id/testtextview" > 
</android.support.v4.view.ViewPager> 
<TextView 
android:id="@+id/testtextview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="10dp" 
android:background="#999999" 
android:padding="20dp" 
android:textColor="@android:color/black" /> 
</com.android.mylistview.view.MyLayout> 
</LinearLayout> 

3、child_viewpager每一页中的内容(假定文件名为child_viewpager_item.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:gravity="center" 
android:orientation="vertical" > 
<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" > 
<ImageView 
android:id="@+id/imageview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
android:background="@drawable/ic_launcher" /> 
<TextView 
android:id="@+id/textview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" /> 
</RelativeLayout> 
</LinearLayout> 

4、以上为全部布局文件,接下来自定义MyLayout布局

复制代码代码如下:
public class MyLayout extends RelativeLayout 

ViewPager child_viewpager; 
float startX; 
/** 
* @param context 
* @param attrs 
*/ 
public MyLayout(Context context, AttributeSet attrs) 

super(context, attrs); 

//这里是关键 
public boolean onInterceptTouchEvent(MotionEvent event) 

int action = event.getAction(); 
switch (action) 

case MotionEvent.ACTION_DOWN://按下 
startX = event.getX(); 
getParent().requestDisallowInterceptTouchEvent(true); 
break; 
//滑动,在此对里层viewpager的第一页和最后一页滑动做处理 
case MotionEvent.ACTION_MOVE: 
if (startX == event.getX()) 

if (0 == child_viewpager.getCurrentItem() 
|| child_viewpager.getCurrentItem() == child_viewpager 
.getAdapter().getCount() - 1) 

getParent().requestDisallowInterceptTouchEvent(false); 


//里层viewpager已经是最后一页,此时继续向右滑(手指从右往左滑) 
else if (startX > event.getX()) 

if (child_viewpager.getCurrentItem() == child_viewpager 
.getAdapter().getCount() - 1) 

getParent().requestDisallowInterceptTouchEvent(false); 


//里层viewpager已经是第一页,此时继续向左滑(手指从左往右滑) 
else if (startX < event.getX()) 

if (child_viewpager.getCurrentItem() == 0) 

getParent().requestDisallowInterceptTouchEvent(false); 

} else 

getParent().requestDisallowInterceptTouchEvent(true); 

break; 
case MotionEvent.ACTION_UP://抬起 
case MotionEvent.ACTION_CANCEL: 
getParent().requestDisallowInterceptTouchEvent(false); 
break; 

return false; 

//注入里层viewpager 
public void setChild_viewpager(ViewPager child_viewpager) 

this.child_viewpager = child_viewpager; 

5、最后是主activity

复制代码代码如下:
public class TestViewpager extends Activity 

private ViewPager viewpager; 
@Override 
protected void onCreate(Bundle savedInstanceState) 

super.onCreate(savedInstanceState); 
setContentView(R.layout.viewpager_layout); 
viewpager = (ViewPager) findViewById(R.id.viewpager); 
LayoutInflater inflater = LayoutInflater.from(this); 
List<View> list = new ArrayList<View>(); 
View view = null, childView = null; 
ViewPager child_viewpager; 
TextView textview, testtextview; 
List<View> childlist = null; 
MyLayout mylayout; 
for (int i = 0; i < 3; i++) 

view = inflater.inflate(R.layout.child_viewpager_layout, null); 
mylayout = (MyLayout) view.findViewById(R.id.mylayout); 
testtextview = (TextView) view.findViewById(R.id.testtextview); 
testtextview.setText("viewpager:" + i); 
list.add(view); 
child_viewpager = (ViewPager) view 
.findViewById(R.id.child_viewpager); 
//注入里层viewpager 
mylayout.setChild_viewpager(child_viewpager); 
childlist = new ArrayList<View>(); 
for (int j = 0; j < 3; j++) 

childView = inflater.inflate(R.layout.child_viewpager_item, 
null); 
textview = (TextView) childView.findViewById(R.id.textview); 
textview.setText("view" + i + ":" + j); 
childlist.add(childView); 
child_viewpager.setAdapter(new ViewPagerAdapter(childlist)); 


viewpager.setAdapter(new ViewPagerAdapter(list)); 

 
注:来自
 

仿网易新闻客户端头条ViewPager嵌套实例的更多相关文章

  1. 类似掌盟的Tab页 Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签 (转)

    原博客地址  :http://blog.csdn.net/xiaanming/article/details/10766053 本文转载,记录学习用,如有需要,请到原作者网站查看(上面这个网址) 之前 ...

  2. Android 开源框架ActionBarSherlock 和 ViewPager 仿网易新闻客户端

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/9971721 大家都知道Android的ActionBar是在3.0以上才有的,那么在3 ...

  3. Android Studio精彩案例(一)《ActionBar和 ViewPager版仿网易新闻客户端》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 为了能更好的分享高质量的文章,所以开设了此专栏.文章代码都以Android Studio亲测运行,读者朋友可在后面直接下载源码.该专栏 ...

  4. Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/10766053 之前用JakeWharton的开源框架ActionBarSherlock ...

  5. Android SlidingMenu 仿网易新闻客户端布局

    前面两篇文章中的SlidingMenu都出现在左侧,今天来模仿一下网易新闻客户端左右两边都有SlidingMenu的效果,以下是网易新闻客户端效果: 不扯闲话了,直接进入正题吧 frame_conte ...

  6. 分享一个仿网易新闻客户端iPhone版的标签式导航ViewController

    该Controller是一个容器,用于容纳其他的controller.效果与网易新闻客户端的标签式导航基本一样: (1)点击上面的标签,可以切换到对应的controller,标签下面的红色提示条的长度 ...

  7. Android Studio精彩案例(四)《DrawerLayout使用详解仿网易新闻客户端侧边栏 》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 为了提高兴趣,咱们开头先看看最终要实现什么样的效果: 侧拉菜单在Android应用中非常常见,它的实现方式太多了,今天我们就说说使用G ...

  8. Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻clientTab标签

    之前用JakeWharton的开源框架ActionBarSherlock和ViewPager实现了对网易新闻clientTab标签的功能,ActionBarSherlock是在3.0下面的机器支持Ac ...

  9. 仿Android网易新闻客户端,并增加水平图片滑动,改进阅读体验

    仿网易新闻Android端APP 主要功能展示和代码实现 差不多花了一周的时间,目前实现的了新闻下的包括头条.体育.娱乐的一系列的新闻展示,以及点击后进入的新闻详情展示. 目前效果 目前效果请访问该网 ...

随机推荐

  1. poj 3358 Period of an Infinite Binary Expansion

    由乘2取整得到分数的小数位,可以找到规律!!! 例如:1/10,2/10,4/10,8/10,16/10,32/10,64/10…… 取整后:1/10,2/10,4/10,8/10,6/10,2/10 ...

  2. DIV+CSS列表式布局(同意图片的应用)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. lintcode: 翻转链表

    题目: 翻转链表 翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null 挑战 在原地一次翻转完成 解题: 递归还 ...

  4. 用JUnit4进行单元测试

    转载:http://tonl.iteye.com/blog/1948869 参考: http://thihy.iteye.com/blog/1771826 http://developer.51cto ...

  5. Android Cursor空指针的问题

    最近几天无聊自己动手写个音乐播放器,用到Cursor来取得数据库中音乐文件的信息,但是当用到Cursor的时候总是报空指针错误,后来发现是模拟器上没有音乐文件,使用Cursor的时候 ,若Cursor ...

  6. git全局配置

    使用git的童鞋都知道,git是非常好的版本管理工具,工具再好要想用的得心应手还是要下凡功夫的,比如可以通过对git的全局配置文件.gitconfig进行适当的配置,可以在日常项目开发中节省很多的时间 ...

  7. MySQL5.7表空间加密

    MySQL5.7开始支持表空间加密了,增强了MySQL的数据文件的安全性,这是一个很不错的一个功能,这个特性默认是没有启用的,要使用这个功能要安装插件keyring_file. 下面就来看看怎么安装, ...

  8. struts2中token防止重复提交表单

    struts2中token防止重复提交表单 >>>>>>>>>>>>>>>>>>>&g ...

  9. 23.allegro中自动布线[原创]

    1. --- 方法①:选择网络自动布线 -- --- 已经步好: --- 方法②: ---- ---- 布线: --- 方法③: -- ----

  10. POJ 3185 The Water Bowls(高斯消元-枚举变元个数)

    题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...