要點: 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));
}
}