Android WebView下拉刷新与SwipeRefreshLayout事件冲突解决
来源:互联网 发布:阿里云邮箱不免费了 编辑:程序博客网 时间:2024/03/29 17:38
简介
本篇介绍WebView下拉刷新方法,另外解决SwipeRefreshLayout与WebView嵌套布局时滑动事件冲突的解决办法。
效果
下拉刷新
SwipeRefreshLayout控件可以优雅的完成下拉事件监听。
1、布局文件:
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_fresh" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null" android:scrollbars="none" /></android.support.v4.widget.SwipeRefreshLayout>
2、Activity
@BindView(R.id.swipe_fresh)SwipeRefreshLayout refreshLayout;@BindView(R.id.webview)WebView webView;...private void initView() { refreshLayout.setColorSchemeResources(R.color.colorPrimary); refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { webView.loadUrl(url); } });}...
即在SwipeRefreshLayout控件的refresh事件中重新加载WebView的内容就可以了。
事件冲突
完成了上述功能后,会发现网页无法上拉,往上滑动就会触发下拉刷新控件的refresh事件。演示:
解决冲突
关于两个控件的冲突问题,网络上有不少解决办法,有的是自定义SwipeRefreshLayout重写onTouchEvent方法;有的是重写WebView的scroll监听。
最后我找到一个比较好的解决办法。
将Activity代码修改为
@BindView(R.id.swipe_fresh)SwipeRefreshLayout refreshLayout;@BindView(R.id.webview)WebView webView;...private void initView() { refreshLayout.setColorSchemeResources(R.color.colorPrimary); refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { webView.loadUrl(url); } }); // 设置子视图是否允许滚动到顶部 refreshLayout.setOnChildScrollUpCallback(new SwipeRefreshLayout.OnChildScrollUpCallback() { @Override public boolean canChildScrollUp(SwipeRefreshLayout parent, @Nullable View child) { return webView.getScrollY() > 0; } });}...
说明:canChildScrollUp方法返回的true/false表示子视图是否可返回顶部,我们改成webView.getScrollY() > 0后表示webView到顶部时返回false,refreshLayout可接收到下拉动作,触发refresh事件。
此时上拉、下拉都没问题了,看下效果
源码分析
/** * Set a callback to override {@link SwipeRefreshLayout#canChildScrollUp()} method. Non-null * callback will return the value provided by the callback and ignore all internal logic. * @param callback Callback that should be called when canChildScrollUp() is called. */public void setOnChildScrollUpCallback(@Nullable OnChildScrollUpCallback callback) { mChildScrollUpCallback = callback;}
setOnChildScrollUpCallback方法可以设置一定监听接口,返回子视图是否可以滑动到顶部。
/** * Classes that wish to override {@link SwipeRefreshLayout#canChildScrollUp()} method * behavior should implement this interface. */ public interface OnChildScrollUpCallback { /** * Callback that will be called when {@link SwipeRefreshLayout#canChildScrollUp()} method * is called to allow the implementer to override its behavior. * * @param parent SwipeRefreshLayout that this callback is overriding. * @param child The child view of SwipeRefreshLayout. * * @return Whether it is possible for the child view of parent layout to scroll up. */ boolean canChildScrollUp(SwipeRefreshLayout parent, @Nullable View child); }
OnChildScrollUpCallback 接口就一个canChildScrollUp方法,返回是否可滚动。
在上述实现过程中,我们判断WebView的状态:
- 在顶部时,返回false,表示子视图不可滚动,refreshLayout接收到滑动事件,引出滑动视图和调用滑动刷新方法;
- 不在顶部时,webView.getScrollY() > 0,返回true,表示子视图可滚动,refreshLayout中canChildScrollUp()返回true,刷新控件不再处理滑动问题,所以没有调用滑动刷新方法。
参考资料
http://blog.csdn.net/u012461368/article/details/51037573
阅读全文
0 0
- Android WebView下拉刷新与SwipeRefreshLayout事件冲突解决
- Android SwipeRefreshLayout 嵌套 CoordinatorLayout 下拉刷新 事件冲突解决
- 解决SwipeRefreshLayout和webview下拉刷新冲突问题
- 解决SwipeRefreshLayout和webview下拉刷新冲突问题
- SwipeRefreshLayout下拉刷新冲突解决
- SwipeRefreshLayout下拉刷新冲突解决
- 解决listview下拉与SwipeRefreshLayout下拉刷新冲突
- 解决SwipeRefreshLayout下拉刷新与ListView下拉的冲突
- SwipeRefreshLayout下拉刷新与下拉控件冲突解决
- 解决SwipeRefreshLayout下拉刷新与SwipeMenuListView的冲突
- SwipeRefreshLayout 嵌套PullToRefreshListView下拉刷新冲突解决
- viewpager和SwipeRefreshLayout下拉刷新冲突解决
- Android SwipeRefreshLayout 、RecyclerView 下拉刷新冲突
- 【Android】Webview 如何使用SwipeRefreshLayout下拉刷新
- 完美解决SwipeRefreshLayout与WebView冲突
- 解决使用`SwipeRefreshLayout`下拉刷新和左右滑动事件冲突的问题
- SwipeRefreshLayout下拉刷新跟listview下拉滑动冲突解决
- 两句代码解决ViewPager滑动与SwipeRefreshLayout下拉刷新冲突
- Elasticsearch Centos7 安装
- 【android学习】四大组件-Content Provider(内容提供者)
- 对接支付宝接口时,不使用SDK,调用成功但是返回的数据一直验签失败问题解决方案
- multisim仿真时为什么改一个vcc值,却全部变了
- Python程序的错误:变量未定义
- Android WebView下拉刷新与SwipeRefreshLayout事件冲突解决
- 167. Two Sum II
- java递归删除文件以及子文件
- 隐马尔科夫(HMM)的Matlab实现
- 去除serchview的默认黑色背景提示框
- JS封装(循环遍历添加)
- 二进制与十进制
- UnityEditor.AsyncHTTPClient:Done(State, Int32)错误解决方法
- 排序算法代码总结