解决SwipeRefreshLayout和webview下拉刷新冲突问题

来源:互联网 发布:苏联国歌 知乎 编辑:程序博客网 时间:2024/04/19 23:05

参考:点击打开链接  但我按这篇修改时会出现下拉后不能响应上拉事件的问题


在SwipeRefreshLayout中嵌套webview时,默认无论在网页任何位置下拉都会触发刷新,实际需求是只在顶部需要,解决方法是继承SwipeRefreshLayout,复写其中的canScrollUp方法:

import android.content.Context;import android.support.v4.widget.SwipeRefreshLayout;import android.util.AttributeSet;import android.view.ViewGroup;public class ScrollSwipeRefreshLayout extends SwipeRefreshLayout {    //子布局 这里为webview    private ViewGroup mChildViewGroup;    public ScrollSwipeRefreshLayout(Context context) {        super(context);    }    public ScrollSwipeRefreshLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    public ViewGroup getViewGroup() {        return mChildViewGroup;    }    public void setViewGroup(ViewGroup viewGroup) {        this.mChildViewGroup = viewGroup;    }    /**     * @return Whether it is possible for the child view of this layout to     *         scroll up. Override this if the child view is a custom view.     */    @Override    public boolean canChildScrollUp() {        if(mChildViewGroup != null){            //子布局在非顶部才可以向上滚动            if(mChildViewGroup.getScrollY() > 0){                return true;            }        }        return false;    }}

然后在使用时传入webview即可:

refreshLayout.setViewGroup(webView);//设置监听滚动的子view


阅读全文
0 0