SwipeRefreshLayout 嵌套WebView滑动冲突问题

webView手势上拉没问题,下拉会执行SwipeRefreshLayout 的刷新操作,

解决办法:自定义Webview重写手势

package com.yinuo.mancai.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.webkit.WebView;

/**
 * Created by yangfan
 * nrainyseason@163.com
 */

public class MyWebView extends WebView{
    private ViewGroup viewGroup;

    public MyWebView(Context context) {
        super(context);
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ViewGroup getViewGroup() {
        return viewGroup;
    }

    public void setViewGroup(ViewGroup viewGroup) {
        this.viewGroup = viewGroup;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(this.getScrollY() <= 0)
                    this.scrollTo(0,1);
                break;
        }
        return super.onTouchEvent(event);
    }
}

然后在SwipeRefreshLayout 的setOnChildScrollUpCallback监听中判断,setOnChildScrollUpCallback这个监听要24以上的包下才有

mWebView.setViewGroup(SwipeRefreshLayout);
private void initListener() {
    SwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mWebView.reload();
            SwipeRefreshLayout.setRefreshing(false);
        }
    });
    SwipeRefreshLayout.setOnChildScrollUpCallback(new SwipeRefreshLayout.OnChildScrollUpCallback() {
        @Override
        public boolean canChildScrollUp(SwipeRefreshLayout parent, @Nullable View child) {
            return mWebView.getScrollY()>0;
        }
    });
}
这样就能解决问题了
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

SwipeRefreshLayout 嵌套WebView滑动冲突问题

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭