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;
        }
    });
}


这样就能解决问题了
阅读更多

扫码向博主提问

你微笑时很美_

Android系统,编程,无所不能
  • 擅长领域:
  • Android编码
  • Android控件
  • 操作系统
  • diy硬件
去开通我的Chat快问
想对作者说点什么?

博主推荐

换一批

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