前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一行代码搞定SwipeRefreshLayout拦截事件

一行代码搞定SwipeRefreshLayout拦截事件

作者头像
企鹅号小编
发布2018-01-31 17:28:47
1.2K0
发布2018-01-31 17:28:47
举报
文章被收录于专栏:应用案例应用案例

SwipeRefreshLayout这个控件大家可能几百年前就已经在熟练使用了,相关的博客也多不胜数,方法也许不同,但实质都是一样的,写这个的目的也只是为了先把公众号和星球转起来。

SwipeRefreshLayout是Android自己支持库的下拉刷新控件,官方文档中提示,只有其包裹的孩子是RecyclerView、ListView、ScrollView等可滑动控件才能正常执行下拉刷新完整逻辑,显示下拉刷新图标以及回收图标。如果是非滑动控件,比如我们会常用到Material Design设计风格中的CoordinatorLayout控件AppBarLayout结合RecyclerView的使用,下拉刷新就会出现拦截问题导致无法滑动列表。

很多博客对SwipeRefreshLayout都介绍得很详细,包括源码的分析,以及分析并解决遇到各种问题。对于列表拦截冲突的解决方法,大致都是根据查看onInterceptTouchEvent方法里面的拦截机制,根据判断逻辑继承SwipeRefreshLayout类重写canChildScrollUp()来解决,其实有个更简单粗暴的方法,直接按照自己的想要的滑动逻辑来设置是否拦截就可以了,上代码

public classAdvanceSwipeRefreshLayoutextendsSwipeRefreshLayout {

privateOnPreInterceptTouchEventDelegatemOnPreInterceptTouchEventDelegate;

publicAdvanceSwipeRefreshLayout(Context context) {

super(context);

}

publicAdvanceSwipeRefreshLayout(Context context,AttributeSet attrs) {

super(context,attrs);

mConfiguration= ViewConfiguration.get(context);

}

@Override

public booleanonInterceptTouchEvent(MotionEvent ev) {

booleandisallowIntercept =false;

if(mOnPreInterceptTouchEventDelegate!=null)

disallowIntercept =mOnPreInterceptTouchEventDelegate.shouldDisallowInterceptTouchEvent(ev);

if(disallowIntercept) {

return false;

}

return super.onInterceptTouchEvent(ev);

}

public voidsetOnPreInterceptTouchEventDelegate(OnPreInterceptTouchEventDelegate listener) {

mOnPreInterceptTouchEventDelegate= listener;

}

public interfaceOnPreInterceptTouchEventDelegate {

booleanshouldDisallowInterceptTouchEvent(MotionEvent ev);

}

}

使用的时候根据自己的需要设置就可以了。

View appBarLayout =v_findView(mContentSection,R.id.appBarLayout);

mRefreshLayout.setOnPreInterceptTouchEventDelegate(ev ->appBarLayout.getTop()

是不是很快,如果有什么问题希望大家指正,谢谢!

参考

https://github.com/hanks-zyh/SwipeRefreshLayout

http://www.jianshu.com/p/a366e9ecb7b8

本文来自企鹅号 - Android开发者部落媒体

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文来自企鹅号 - Android开发者部落媒体

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档