关于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接口的源码
- **
- * 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 {
** * 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 {
- …………..
..............
- }
}看到开头注释中这样一段话:
实现这个接口必须创建NestedScrollingChildHelper,并且将View的方法交给NestedScrollingChildHelper的同名方法来处理,所以我们就在自定义ListView中加入
- 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);
- }
- }
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即可
- <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>
<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
- 关于CoordinatorLayout和ListView滑动冲突的解决
- 关于CoordinatorLayout和ListView滑动冲突的解决
- ScrollView和ListView的滑动冲突解决
- listview的滑动冲突解决
- 解决SpringView与CoordinatorLayout和AppBarLayout嵌套使用的上下滑动冲突问题
- 完美解决ListView和ViewPager的滑动冲突
- ListView 和Scollowview嵌套滑动冲突的解决
- 自定义viewpager解决listview和viewpager滑动冲突的问题
- Fragment中ListView和左右滑动冲突的解决
- 完美解决listView和ScrollView滑动冲突的问题
- Android--关于ListView和ViewPager滑动冲突的处理
- 解决HorizontalScrollView和ListView滑动冲突
- 解决viewpager和listview滑动冲突
- 解决listView和ScrollView滑动冲突
- 解决ScrollView和ListView滑动冲突
- SwipeRefreshLayout 与ListView滑动冲突的解决
- Listview 嵌套 Viewpager 滑动冲突的解决
- 解决ListView与ScrollView的滑动冲突
- Qt Designer一打开就无法工作的解决办法
- Another app is currently holding the yum lock; waiting for it to exit..
- pom文件依赖依赖一直报错?不妨试试这个神器
- win10开机启动项配置
- HDU 4282A very hard mathematic problem(二分)
- 关于CoordinatorLayout和ListView滑动冲突的解决
- PHP Warning: phpinfo(): It is not safe to rely on the system's timezone settings.处理方法
- adbunknown Host service问题修复
- IDC说阿里云又涨了丨专家称信息保护对区块链技术需求巨大丨联想亏惨了,净利润同比下滑80%【软件网每日新闻播报│第11-2期】
- 携AI+安防双基因,博思廷如何推动智能安防产业发展?
- 集合框架
- https://www.5ceo.cn/generator/index.htm [5CEO.cn-代码生成工具,在线快速生成java代码,springmvc框架]
- 引用 网页层叠z-index用法详解(转载)
- mybatis 的一级缓存和二级缓存