仿网易新闻客户端头条ViewPager嵌套实例
程序员文章站
2023-11-04 14:33:04
要点: 1、重写组件public boolean onintercepttouchevent(motionevent event)方法 2、正确使用requestdisal...
要点:
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));
}
}
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));
}
}