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

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

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

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

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


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



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

  1. public class MyListView extends ListView implements NestedScrollingChild{  
  2.   
  3. private final NestedScrollingChildHelper mScrollingChildHelper;  
  4.   
  5. public MyListView(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. 最后在xml中
  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.MyListView  
  7.                     android:id="@+id/listview"  
  8.                     android:layout_width="match_parent"  
  9.                     android:layout_height="wrap_content">  
  10.   
  11.                 </com.dipaitv.widget.MyListView>  
  12.                 </android.support.v4.widget.NestedScrollView>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值