您的位置:首页 > 移动开发

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

2017-12-24 22:30 429 查看
在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) {
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 (null != mChildViewGroup) {
if (mChildViewGroup.getScrollY() > 0) {
return true;
}
return false;
}
return super.canChildScrollUp();
}
}


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

mRefreshLayout.setViewGroup(webView);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  webview