关于CoordinatorLayout和ListView滑动冲突的解决

来源:互联网 发布:eia数据公布 编辑:程序博客网 时间:2024/06/05 06:40

最近项目中使用到了CoordinatorLayout这种布局方式,搭配RecycleView,实现起来比较简单,而且不用自己处理滑动事件,但是改为了ListView后发生了滑动冲突.

所以想到了以下解决方案:

1.使用事件分发,当ListView在Y轴滑动时,将事件交给CoordinatorLayout处理,无效!!!

2.取消ListView在Y轴的滑动,计算手指移动的距离,交给CoordinatorLayout来scroll,无效!!!


以上都不行,google了下,发现由于由于CoordinatorLayout实现NestedScrollingParent接口,RecycleView实现了NestedScrollingChild接口,所以就可以在NestedScrollingChildHelper的帮助下实现滑动联动,知道了原因这就简单了,让我们的LIstView实现NestedScrollingChild接口,查看下NestedScrollingChild接口的源码

[java] view plain copy
print?
  1. **  
  2.  * This interface should be implemented by {@link android.view.View View} subclasses that wish  
  3.  * to support dispatching nested scrolling operations to a cooperating parent  
  4.  * {@link android.view.ViewGroup ViewGroup}.  
  5.  *  
  6.  * <p>Classes implementing this interface should create a final instance of a  
  7.  * {@link NestedScrollingChildHelper} as a field and delegate any View methods to the  
  8.  * <code>NestedScrollingChildHelper</code> methods of the same signature.</p>  
  9.  *  
  10.  * <p>Views invoking nested scrolling functionality should always do so from the relevant  
  11.  * {@link ViewCompat}, {@link ViewGroupCompat} or {@link ViewParentCompat} compatibility  
  12.  * shim static methods. This ensures interoperability with nested scrolling views on Android  
  13.  * 5.0 Lollipop and newer.</p>  
  14.  */  
  15. public interface NestedScrollingChild {  
** * This interface should be implemented by {@link android.view.View View} subclasses that wish * to support dispatching nested scrolling operations to a cooperating parent * {@link android.view.ViewGroup ViewGroup}. * * <p>Classes implementing this interface should create a final instance of a * {@link NestedScrollingChildHelper} as a field and delegate any View methods to the * <code>NestedScrollingChildHelper</code> methods of the same signature.</p> * * <p>Views invoking nested scrolling functionality should always do so from the relevant * {@link ViewCompat}, {@link ViewGroupCompat} or {@link ViewParentCompat} compatibility * shim static methods. This ensures interoperability with nested scrolling views on Android * 5.0 Lollipop and newer.</p> */public interface NestedScrollingChild {
[java] view plain copy
print?
  1. …………..  
..............
[java] view plain copy
print?
  1. }  
}
看到开头注释中这样一段话:

实现这个接口必须创建NestedScrollingChildHelper,并且将View的方法交给NestedScrollingChildHelper的同名方法来处理,所以我们就在自定义ListView中加入

[java] view plain copy
print?
  1. public class MyListView extends ListView implements NestedScrollingChild{  
  2.   
  3. private final NestedScrollingChildHelper mScrollingChildHelper;  
  4.   
  5. public DPListView(Context context, AttributeSet attrs) {  
  6.         super(context, attrs);  
  7.         mScrollingChildHelper = new NestedScrollingChildHelper(this);  
  8.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
  9.             setNestedScrollingEnabled(true);  
  10.         }  
  11.   
  12.   
  13.     }  
  14.   
  15.   
  16.     @Override  
  17.     public void setNestedScrollingEnabled(boolean enabled) {  
  18.         mScrollingChildHelper.setNestedScrollingEnabled(enabled);  
  19.     }  
  20.   
  21.     @Override  
  22.     public boolean isNestedScrollingEnabled() {  
  23.         return mScrollingChildHelper.isNestedScrollingEnabled();  
  24.     }  
  25.   
  26.     @Override  
  27.     public boolean startNestedScroll(int axes) {  
  28.         return mScrollingChildHelper.startNestedScroll(axes);  
  29.     }  
  30.   
  31.     @Override  
  32.     public void stopNestedScroll() {  
  33.         mScrollingChildHelper.stopNestedScroll();  
  34.     }  
  35.   
  36.     @Override  
  37.     public boolean hasNestedScrollingParent() {  
  38.         return mScrollingChildHelper.hasNestedScrollingParent();  
  39.     }  
  40.   
  41.     @Override  
  42.     public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,  
  43.                                         int dyUnconsumed, int[] offsetInWindow) {  
  44.         return mScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed,  
  45.                 dxUnconsumed, dyUnconsumed, offsetInWindow);  
  46.     }  
  47.   
  48.     @Override  
  49.     public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {  
  50.         return mScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);  
  51.     }  
  52.   
  53.     @Override  
  54.     public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {  
  55.         return mScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);  
  56.     }  
  57.   
  58.     @Override  
  59.     public boolean dispatchNestedPreFling(float velocityX, float velocityY) {  
  60.         return mScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);  
  61.     }  
  62.   
  63. }  
public class MyListView extends ListView implements NestedScrollingChild{private final NestedScrollingChildHelper mScrollingChildHelper;public DPListView(Context context, AttributeSet attrs) {        super(context, attrs);        mScrollingChildHelper = new NestedScrollingChildHelper(this);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            setNestedScrollingEnabled(true);        }    }    @Override    public void setNestedScrollingEnabled(boolean enabled) {        mScrollingChildHelper.setNestedScrollingEnabled(enabled);    }    @Override    public boolean isNestedScrollingEnabled() {        return mScrollingChildHelper.isNestedScrollingEnabled();    }    @Override    public boolean startNestedScroll(int axes) {        return mScrollingChildHelper.startNestedScroll(axes);    }    @Override    public void stopNestedScroll() {        mScrollingChildHelper.stopNestedScroll();    }    @Override    public boolean hasNestedScrollingParent() {        return mScrollingChildHelper.hasNestedScrollingParent();    }    @Override    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,                                        int dyUnconsumed, int[] offsetInWindow) {        return mScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed,                dxUnconsumed, dyUnconsumed, offsetInWindow);    }    @Override    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {        return mScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);    }    @Override    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {        return mScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);    }    @Override    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {        return mScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);    }}

最后在xml布局文件中,在MyListView外层嵌套一层NestedScrollView即可

[html] view plain copy
print?
  1. <android.support.v4.widget.NestedScrollView  
  2.                     android:layout_width=“match_parent”  
  3.                     android:layout_height=“match_parent”  
  4.                     android:fillViewport=“true”  
  5.                     app:layout_behavior=“@string/appbar_scrolling_view_behavior”>  
  6.                 <com.dipaitv.widget.DPListView  
  7.                     android:id=“@+id/listview”  
  8.                     android:layout_width=“match_parent”  
  9.                     android:layout_height=“wrap_content”>  
  10.   
  11.                 </com.dipaitv.widget.DPListView>  
  12.                 </android.support.v4.widget.NestedScrollView>  
<android.support.v4.widget.NestedScrollView                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:fillViewport="true"                    app:layout_behavior="@string/appbar_scrolling_view_behavior">                <com.dipaitv.widget.DPListView                    android:id="@+id/listview"                    android:layout_width="match_parent"                    android:layout_height="wrap_content">                </com.dipaitv.widget.DPListView>                </android.support.v4.widget.NestedScrollView>







阅读全文
0 0