Android 解决WebView和SmartRefreshLayout滑动冲突

最近遇到个需求需要在网页中实现下拉刷新功能,遇到一个坑,SmartRefreshLayout嵌套WebView产生滑动冲突,加载自研H5界面时nubia 6.0和xiaomi 8.0上拉加载更多正常,下拉时无法回到顶部,会直接刷新。

布局文件



            

            

                

                
            

在代码中将DWebView 添加到FrameLayout中,这里没有使用系统的WebView

    private void initWebView() {
        DsWebView dWebView = new DsWebView(this);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        dWebView.setLayoutParams(layoutParams);
        flContainer.addView(dWebView);
        DWebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG);
        dWebView.getSettings().setDomStorageEnabled(true);
        // 启动缓存
        dWebView.getSettings().setAppCacheEnabled(true);
        // 设置缓存模式
        dWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (TextUtils.isEmpty(title)) {
            dWebView.clearCache(true);
        }
        dWebView.getSettings().setSavePassword(false);
        dWebView.addJavascriptObject(new JsApi(), null);
        dWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                if (!TextUtils.isEmpty(message)) {
                    ToastUtil.getInstance().showToast(message);
                }
                return true;
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                progressBar.setProgress(newProgress);
            }
        });
        dWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
                progressBar.setProgress(100);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setProgress(0);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                progressBar.setVisibility(View.GONE);
            }
        });
        LogUtil.e("LessonListActivity url:" + url);
        dWebView.loadUrl(url);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                dWebView.reload();
                refreshLayout.finishRefresh(500);
            }
        });
    }

找到两种解决方案,我是两种解决方案都写上了才解决的这个问题

都需要重写DWebView

方案一:https://www.jianshu.com/p/dd27f91cd2f4

思路:通过监听WebView滚动位置来控制SmartRefreshLayout是否可以刷新

方案二:https://blog.csdn.net/hhoo3344521/article/details/79959063

最后重写的DWebView是这样的

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

import wendu.dsbridge.DWebView;

public class DsWebView extends DWebView {
    private ViewGroup viewGroup;
    public DsWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

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

    public ViewGroup getViewGroup() {
        return viewGroup;
    }

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

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mScrollListener != null) {
            mScrollListener.onScrollChanged(t);
        }
    }

    public interface IScrollListener {
        void onScrollChanged(int scrollY);
    }

    private IScrollListener mScrollListener;

    public void setOnScrollListener(IScrollListener listener) {
        mScrollListener = listener;
    }
}

代码中initWebView最后添加

        dWebView.setViewGroup(refreshLayout);
        dWebView.setOnScrollListener(new DsWebView.IScrollListener() {
            @Override
            public void onScrollChanged(int scrollY) {
                if (scrollY == 0) {
                    //开启下拉刷新
                    refreshLayout.setEnableRefresh(true);
                } else {
                    //关闭下拉刷新
                    refreshLayout.setEnableRefresh(false);
                }
            }
        });

然后就可以啦,完美解决我遇到的问题啦,希望你也能顺利解决问题呀!

你可能感兴趣的:(Android开发,android,webview)