仿網(wǎng)易新聞客戶端頭條ViewPager嵌套實(shí)例
更新時(shí)間:2013年06月14日 14:36:30 作者:
正確使用requestDisallowInterceptTouchEvent(boolean flag)方法,下面為大家介紹下外層ViewPager布局的實(shí)例,感興趣的朋友可以參考下哈
要點(diǎn):
1、重寫組件public boolean onInterceptTouchEvent(MotionEvent event)方法
2、正確使用requestDisallowInterceptTouchEvent(boolean flag)方法
關(guān)于以上兩個(gè)方法,請(qǐng)大家多看看相關(guān)介紹,這里就不在敘述了^_^
接下來上例子:
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每一頁中的內(nèi)容(假定文件名為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);
}
//這里是關(guān)鍵
public boolean onInterceptTouchEvent(MotionEvent event)
{
int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN://按下
startX = event.getX();
getParent().requestDisallowInterceptTouchEvent(true);
break;
//滑動(dòng),在此對(duì)里層viewpager的第一頁和最后一頁滑動(dòng)做處理
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已經(jīng)是最后一頁,此時(shí)繼續(xù)向右滑(手指從右往左滑)
else if (startX > event.getX())
{
if (child_viewpager.getCurrentItem() == child_viewpager
.getAdapter().getCount() - 1)
{
getParent().requestDisallowInterceptTouchEvent(false);
}
}
//里層viewpager已經(jīng)是第一頁,此時(shí)繼續(xù)向左滑(手指從左往右滑)
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)方法
關(guān)于以上兩個(gè)方法,請(qǐng)大家多看看相關(guān)介紹,這里就不在敘述了^_^
接下來上例子:
1、外層ViewPager布局 (假定文件名為viewpager_layout.xml)
復(fù)制代碼 代碼如下:
<?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)
復(fù)制代碼 代碼如下:
<?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每一頁中的內(nèi)容(假定文件名為child_viewpager_item.xml)
復(fù)制代碼 代碼如下:
<?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布局
復(fù)制代碼 代碼如下:
public class MyLayout extends RelativeLayout
{
ViewPager child_viewpager;
float startX;
/**
* @param context
* @param attrs
*/
public MyLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
//這里是關(guān)鍵
public boolean onInterceptTouchEvent(MotionEvent event)
{
int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN://按下
startX = event.getX();
getParent().requestDisallowInterceptTouchEvent(true);
break;
//滑動(dòng),在此對(duì)里層viewpager的第一頁和最后一頁滑動(dòng)做處理
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已經(jīng)是最后一頁,此時(shí)繼續(xù)向右滑(手指從右往左滑)
else if (startX > event.getX())
{
if (child_viewpager.getCurrentItem() == child_viewpager
.getAdapter().getCount() - 1)
{
getParent().requestDisallowInterceptTouchEvent(false);
}
}
//里層viewpager已經(jīng)是第一頁,此時(shí)繼續(xù)向左滑(手指從左往右滑)
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
復(fù)制代碼 代碼如下:
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));
}
}
您可能感興趣的文章:
- Android使用ViewPager加載圖片和輪播視頻
- Android開發(fā)之使用ViewPager實(shí)現(xiàn)圖片左右滑動(dòng)切換效果
- android 解決ViewPager加載大量圖片內(nèi)存溢出問題
- Android ViewPager實(shí)現(xiàn)圖片輪播效果
- Android 利用ViewPager實(shí)現(xiàn)圖片可以左右循環(huán)滑動(dòng)效果附代碼下載
- 使用ViewPager實(shí)現(xiàn)左右循環(huán)滑動(dòng)及滑動(dòng)跳轉(zhuǎn)
- Android利用ViewPager實(shí)現(xiàn)滑動(dòng)廣告板實(shí)例源碼
- Android App中ViewPager所帶來的滑動(dòng)沖突問題解決方法
- Android ViewPager中顯示圖片與播放視頻的填坑記錄
相關(guān)文章
Android手機(jī)通過藍(lán)牙連接佳博打印機(jī)的實(shí)例代碼
這篇文章主要介紹了Android手機(jī)通過藍(lán)牙連接佳博打印機(jī)的實(shí)例代碼,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Flutter使用AnimationController實(shí)現(xiàn)控制動(dòng)畫
這篇文章主要想帶大家來嘗試一下Flutter如何使用AnimationController實(shí)現(xiàn)一個(gè)拖拽圖片,然后返回原點(diǎn)的動(dòng)畫,感興趣的可以了解一下2023-05-05詳解關(guān)于MIUI 9沉浸式狀態(tài)欄的最新適配
由于各系統(tǒng)版本的限制,沉浸式狀態(tài)欄對(duì)系統(tǒng)有要求,本篇文章主要介紹了詳解關(guān)于MIUI 9沉浸式狀態(tài)欄的最新適配,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-05-05Android開發(fā)之Button事件實(shí)現(xiàn)與監(jiān)聽方法總結(jié)
這篇文章主要介紹了Android開發(fā)之Button事件實(shí)現(xiàn)與監(jiān)聽方法,結(jié)合實(shí)例形式總結(jié)分析了Android開發(fā)中Button事件的兩種實(shí)現(xiàn)方法以及針對(duì)Button控件的幾種常用監(jiān)聽方法,需要的朋友可以參考下2016-01-01Android編程設(shè)計(jì)模式之抽象工廠模式詳解
這篇文章主要介紹了Android編程設(shè)計(jì)模式之抽象工廠模式,結(jié)合實(shí)例形式詳細(xì)分析了Android抽象工廠模式的概念、原理、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12Android開發(fā)中一個(gè)簡單實(shí)用的調(diào)試應(yīng)用技巧分享
這篇文章主要跟大家分享了一個(gè)簡單實(shí)用的Android調(diào)試應(yīng)用技巧,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來一起看看吧。2017-05-05Flutter?+?Idea?環(huán)境搭建及配置教程
本文主要總結(jié)我實(shí)際搭建的過程,最后發(fā)現(xiàn)不一定按網(wǎng)上那些博客或者官方文檔寫的來也可以搭建成功,在這里小編給大家分享下Flutter?+?Idea?環(huán)境搭建及配置教程,感興趣的朋友參考下吧2021-12-12