import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
/**
-
viewpage 和listview 相互冲突 将父view 传递到viewpage 里面
-
使用父类的方法 parent.requestDisallowInterceptTouchEvent(true);
-
当 requestDisallowInterceptTouchEvent 如果为true的时候 表示:父view 不拦截子view的touch 事件
-
这个方法只是改变flag
-
@author baozi
*/
public class DecoratorViewPager extends ViewPager {
private ViewGroup parent;
public DecoratorViewPager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public DecoratorViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setNestedpParent(ViewGroup parent) {
this.parent = parent;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
return super.onInterceptTouchEvent(arg0);
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
return super.onTouchEvent(arg0);
}
}
2 . 在xml里面:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:background="#f1f1f1" >
<com.example.bz_viewpager.DecoratorViewPager
android:id="@+id/vp"
android:layout_width=“match_parent”
android:layout_height=“200dp”
android:fadingEdge=“none” />
3. 在代码里使用
将 viewpager 的父view传递到viewpager里面
调用: vp.setNestedpParent((ViewGroup)vp.getParent()); 方法
如下:
lv = (ListView) findViewById(R.id.lv);
View header = LayoutInflater.from(MainActivity.this).inflate(R.layout.viewpage_layout, null);
DecoratorViewPager vp = (DecoratorViewPager) header.findViewById(R.id.vp);
vp.setNestedpParent((ViewGroup)vp.getParent());
MyPagapter myPagapter = new MyPagapter(MainActivity.this);
vp.setAdapter(myPagapter);
lv.addHeaderView(header);
(2)解析:
viewgroup 当中有 一个 requestDisallowInterceptTouchEvent方法
这个方法只改变flag 当 view.requestDisallowInterceptTouchEvent 参数为true的时候
view 不会拦截其子控件的 触摸事件
/**
-
Called when a child does not want this parent and its ancestors to
-
intercept touch events with
-
{@link ViewGroup#onInterceptTouchEvent(MotionEvent)}.
-
This parent should pass this call onto its parents. This parent must obey
-
this request for the duration of the touch (that is, only clear the flag
-
after this parent has received an up or a cancel.
-
@param disallowIntercept True if the child does not want the parent to
-
intercept touch events.
*/
public void requestDisa
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
llowInterceptTouchEvent(boolean disallowIntercept);
贴上源码:
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
// We’re already in this state, assume our ancestors are too
return;
}
本文介绍了一种解决Android中ViewPager与ListView触控事件冲突的方法。通过自定义DecoratorViewPager类,并利用requestDisallowInterceptTouchEvent方法确保父视图不拦截子视图的触摸事件,从而避免了两者间的冲突。文中提供了具体的实现代码及使用步骤。

891

被折叠的 条评论
为什么被折叠?



