zoukankan      html  css  js  c++  java
  • Android listview viewpager解决冲突 滑动

    Android listview viewpager滑动 跳动 冲突解决 

    ListView中嵌套ViewPage有或者滑动手势冲突解决

     

    在listview 上使用 addHeaderView 在第一栏添加 viewpager 当做header 

    如:

    当触发 滑动事件 的时候容易引起 滑动冲突    (比如斜着滑动viewpager  的时候 listview会跳动)

    特别是在  下拉刷新或者上拉加载 的时候 , 组件可能会传递到viewpager当中

    查阅了很多的帖子  发现修改起来都非常麻烦 

    (1)解决方案

    1. 针对viewpager 做了些修改  

    替换掉support.v4当中的viewpager即可:

    [java] view plaincopy在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 plaincopy在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里面  

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

    如下:

    [java] view plaincopy在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方法

    这个方法只改变flag  当 view.requestDisallowInterceptTouchEvent 参数为true的时候  

    view 不会拦截其子控件的 触摸事件

    [java] view plaincopy在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 plaincopy在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. }  
  • 相关阅读:
    css 实现div内显示两行或三行,超出部分用省略号显示
    vue组件中的样式属性:scoped,解决在父组件中无法修改子组件样式问题
    HBuilder打包app(vue项目)
    vue动态路由传值以及get传值及编程式导航
    vue路由vue-router的安装和使用
    vue组件传值之父传子
    vue生命周期钩子函数
    vue定义组件
    vue定义自定义事件方法、事件传值及事件对象
    vue中操作Dom节点的方法
  • 原文地址:https://www.cnblogs.com/youngforlife/p/4933682.html
Copyright © 2011-2022 走看看