Android listview viewpager解決衝突 滑動

Android listview viewpager滑動 跳動 衝突解決 

 

ListView中嵌套ViewPage有或者滑動手勢衝突解決

 

 

在listview 上使用 addHeaderView 在第一欄添加 viewpager 當作header html

如:java

 

 

 

當觸發 滑動事件 的時候容易引發 滑動衝突    (好比斜着滑動viewpager  的時候 listview會跳動)android

特別是在  下拉刷新或者上拉加載 的時候 , 組件可能會傳遞到viewpager當中ide

 

查閱了不少的帖子  發現修改起來都很是麻煩 this

 

(1)解決方案

1. 針對viewpager 作了些修改  

替換掉support.v4當中的viewpager便可:spa

 

 

[java]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
 
  1. package com.example.bz_viewpager;  
  2.   
  3. import android.content.Context;  
  4. import android.support.v4.view.ViewPager;  
  5. import android.util.AttributeSet;  
  6. import android.view.MotionEvent;  
  7. import android.view.ViewGroup;  
  8.   
  9. /** 
  10.  * viewpage 和listview 相互衝突 將父view 傳遞到viewpage 裏面 
  11.  *  
  12.  * 使用父類的方法 parent.requestDisallowInterceptTouchEvent(true); 
  13.  *  
  14.  * 當 requestDisallowInterceptTouchEvent 若是爲true的時候 表示:父view 不攔截子view的touch 事件 
  15.  *  
  16.  * 這個方法只是改變flag   
  17.  *  
  18.  * @author baozi 
  19.  *  
  20.  */  
  21. public class DecoratorViewPager extends ViewPager {  
  22.     private ViewGroup parent;  
  23.   
  24.     public DecoratorViewPager(Context context) {  
  25.         super(context);  
  26.         // TODO Auto-generated constructor stub  
  27.     }  
  28.   
  29.     public DecoratorViewPager(Context context, AttributeSet attrs) {  
  30.         super(context, attrs);  
  31.     }  
  32.   
  33.     public void setNestedpParent(ViewGroup parent) {  
  34.         this.parent = parent;  
  35.     }  
  36.   
  37.     @Override  
  38.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  39.         if (parent != null) {  
  40.             parent.requestDisallowInterceptTouchEvent(true);  
  41.         }  
  42.         return super.dispatchTouchEvent(ev);  
  43.     }  
  44.   
  45.     @Override  
  46.     public boolean onInterceptTouchEvent(MotionEvent arg0) {  
  47.         if (parent != null) {  
  48.             parent.requestDisallowInterceptTouchEvent(true);  
  49.         }  
  50.         return super.onInterceptTouchEvent(arg0);  
  51.     }  
  52.   
  53.     @Override  
  54.     public boolean onTouchEvent(MotionEvent arg0) {  
  55.         if (parent != null) {  
  56.             parent.requestDisallowInterceptTouchEvent(true);  
  57.         }  
  58.         return super.onTouchEvent(arg0);  
  59.     }  
  60.   
  61. }  

 

 

2 . 在xml裏面:

 

[html]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#f1f1f1" >  
  6.   
  7.     <com.example.bz_viewpager.DecoratorViewPager  
  8.         android:id="@+id/vp"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="200dp"  
  11.         android:fadingEdge="none" />  
  12.   
  13. </RelativeLayout>  



 

3. 在代碼裏使用

 

將 viewpager 的父view傳遞到viewpager裏面  .net

調用:       vp.setNestedpParent((ViewGroup)vp.getParent()); 方法code

以下:xml

 

[java]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
 
  1. lv = (ListView) findViewById(R.id.lv);  
  2.   
  3. View header = LayoutInflater.from(MainActivity.this).inflate(R.layout.viewpage_layout, null);  
  4. DecoratorViewPager vp = (DecoratorViewPager) header.findViewById(R.id.vp);  
  5. vp.setNestedpParent((ViewGroup)vp.getParent());  
  6.   
  7. MyPagapter myPagapter = new MyPagapter(MainActivity.this);  
  8. vp.setAdapter(myPagapter);  
  9. lv.addHeaderView(header);  



 

(2)解析:

viewgroup 當中有 一個 requestDisallowInterceptTouchEvent方法htm

這個方法只改變flag  當 view.requestDisallowInterceptTouchEvent 參數爲true的時候  

view 不會攔截其子控件的 觸摸事件

 

 

[java]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
 
  1. /** 
  2.  * Called when a child does not want this parent and its ancestors to 
  3.  * intercept touch events with 
  4.  * {@link ViewGroup#onInterceptTouchEvent(MotionEvent)}. 
  5.  * 
  6.  * <p>This parent should pass this call onto its parents. This parent must obey 
  7.  * this request for the duration of the touch (that is, only clear the flag 
  8.  * after this parent has received an up or a cancel.</p> 
  9.  *  
  10.  * @param disallowIntercept True if the child does not want the parent to 
  11.  *            intercept touch events. 
  12.  */  
  13. public void requestDisallowInterceptTouchEvent(boolean disallowIntercept);  


貼上源碼:

 

 

[java]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
 
    1. public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {  
    2.   
    3.     if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {  
    4.         // We're already in this state, assume our ancestors are too  
    5.         return;  
    6.     }  
    7.   
    8.     if (disallowIntercept) {  
    9.         mGroupFlags |= FLAG_DISALLOW_INTERCEPT;  
    10.     } else {  
    11.         mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;  
    12.     }  
    13.   
    14.     // Pass it up to our parent  
    15.     if (mParent != null) {  
    16.         mParent.requestDisallowInterceptTouchEvent(disallowIntercept);  
    17.     }  
    18. }  
相關文章
相關標籤/搜索