仿网易新闻客户端头条ViewPager嵌套实例
要点:
1、重写组件public boolean onInterceptTouchEvent(MotionEvent event)方法
2、正确使用requestDisallowInterceptTouchEvent(boolean flag)方法
关于以上两个方法,请大家多看看相关介绍,这里就不在叙述了^_^
接下来上例子:
1、外层ViewPager布局 (假定文件名为viewpager_layout.xml)
<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)
<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)
<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布局
{
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
{
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嵌套实例的更多相关文章
- 类似掌盟的Tab页 Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签 (转)
原博客地址 :http://blog.csdn.net/xiaanming/article/details/10766053 本文转载,记录学习用,如有需要,请到原作者网站查看(上面这个网址) 之前 ...
- Android 开源框架ActionBarSherlock 和 ViewPager 仿网易新闻客户端
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/9971721 大家都知道Android的ActionBar是在3.0以上才有的,那么在3 ...
- Android Studio精彩案例(一)《ActionBar和 ViewPager版仿网易新闻客户端》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 为了能更好的分享高质量的文章,所以开设了此专栏.文章代码都以Android Studio亲测运行,读者朋友可在后面直接下载源码.该专栏 ...
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/10766053 之前用JakeWharton的开源框架ActionBarSherlock ...
- Android SlidingMenu 仿网易新闻客户端布局
前面两篇文章中的SlidingMenu都出现在左侧,今天来模仿一下网易新闻客户端左右两边都有SlidingMenu的效果,以下是网易新闻客户端效果: 不扯闲话了,直接进入正题吧 frame_conte ...
- 分享一个仿网易新闻客户端iPhone版的标签式导航ViewController
该Controller是一个容器,用于容纳其他的controller.效果与网易新闻客户端的标签式导航基本一样: (1)点击上面的标签,可以切换到对应的controller,标签下面的红色提示条的长度 ...
- Android Studio精彩案例(四)《DrawerLayout使用详解仿网易新闻客户端侧边栏 》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 为了提高兴趣,咱们开头先看看最终要实现什么样的效果: 侧拉菜单在Android应用中非常常见,它的实现方式太多了,今天我们就说说使用G ...
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻clientTab标签
之前用JakeWharton的开源框架ActionBarSherlock和ViewPager实现了对网易新闻clientTab标签的功能,ActionBarSherlock是在3.0下面的机器支持Ac ...
- 仿Android网易新闻客户端,并增加水平图片滑动,改进阅读体验
仿网易新闻Android端APP 主要功能展示和代码实现 差不多花了一周的时间,目前实现的了新闻下的包括头条.体育.娱乐的一系列的新闻展示,以及点击后进入的新闻详情展示. 目前效果 目前效果请访问该网 ...
随机推荐
- How do you design object oriented projects?
what are things you do during the high level design phase (before you begin programming) to determin ...
- POJ 1417 True Liars(种类并查集+dp背包问题)
题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...
- SDUT1500 Message Flood
以前做过的用的字典树,可是貌似现在再用超内存....求解释... 问了LYN用的map函数做的,又去小小的学了map函数.... http://wenku.baidu.com/view/0b08cec ...
- 彷徨中的成长-记一个文科生的IT成长过程
纠结了许久,要不要写这篇文章,然而最终还是写了.就权当总结与呻吟吧..当然,呻吟最开始还是发在自己的站点的,忍不住手贱,还是想发博客园. 1 剧透 人算不如天算:时隔多年,我竟然搞起了前端. 2 发端 ...
- APAC Practice
2016 round A A. Googol String small && large LL a[]; int dfs(LL pos, int id, bool f) { || po ...
- MIT算法导论——第五讲.Linear Time Sort
本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...
- iOS 用命令实现简单的打包过程
`xcode-select --print-path`/Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication // 获得打包 ...
- Maven那点事儿(Eclipse版)
Maven那点事儿(Eclipse版) 前言: 由于最近工作学习,总是能碰到Maven的源码.虽然平时工作并不使用Maven,但是为了学习一些源码,还是必须要了解下.这篇文章不是一个全面的Mave ...
- Spring Boot实现一个监听用户请求的拦截器
项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承W ...
- 人脸识别必读的N篇文章
一,人脸检测/跟踪 人脸检测/跟踪的目的是在图像/视频中找到各个人脸所在的位置和大小:对于跟踪而言,还需要确定帧间不同人脸间的对应关系. 1, Robust Real-time Object Dete ...