仿网易新闻客户端头条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 主要功能展示和代码实现 差不多花了一周的时间,目前实现的了新闻下的包括头条.体育.娱乐的一系列的新闻展示,以及点击后进入的新闻详情展示. 目前效果 目前效果请访问该网 ...
随机推荐
- jsp中文件下载的实现
jsp中实现文件下载的最简单的方式是在网页上做超级链接,如:<a href="music/abc.mp3">点击下载</a>.但是这样服务器上的目录资源会直 ...
- 2013年山东省第四届ACM大学生程序设计竞赛 Alice and Bob
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very ...
- hdu2012
http://acm.hdu.edu.cn/showproblem.php?pid=2012 数组大小算错了.....郁闷-_- #include<iostream> #include&l ...
- BZOJ 1982 Moving Pebbles
首先我们假设只有两堆, 容易发现当且仅当两堆相等时,先手必败 否则先手必胜 然后我们猜测一下原因: ->当两堆相等时,无论先手怎么做,后手总能使两堆相等,且必败态为0,0 推广一下: 当所有的石 ...
- lintcode: 二叉树的锯齿形层次遍历
题目 二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / \ ...
- 2014--9=17 软工二班 MyEclipse blue==1
package cn.rwkj.test; import java.io.IOException; import java.net.ServerSocket; import java.net.Sock ...
- PCL—综述—三维图像处理
点云模型与三维信息 三维图像是一种特殊的信息表达形式,其特征是表达的空间中三个维度的数据.和二维图像相比,三维图像借助第三个维度的信息,可以实现天然的物体-背景解耦.除此之外,对于视觉测量来说,物体的 ...
- NYOJ-253 凸包
LK的旅行 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 LK最近要去某几个地方旅行,她从地图上计划了几个点,并且用笔点了出来,准备在五一假期去这几个城市旅行.现在 ...
- ios进度条Demo一个
一个很简单的Dmo.就拿出来分享一下. 一个简单的阴影效果 _progressView.frame = CGRectMake(size.width * progress-size.width, H_H ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...