webview和swiperefreshLayout 的下拉冲突问题
由于webview比较特殊,如果不进行特殊的处理的话,下拉和webview的滚动就会发生冲突,这个解决办法也很简单,只要把重写一下swiperefreshLayout的 onTouchEvent 方法就可以了
代码:
package com.spring.swiperefreshlayout.view;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
/**
* 为了不让webview的滚动事件和下拉更新的事件冲突,重写onTouchEvent事件,当webview滚动到最顶端的时候才有下拉刷新
* @author yd
*
*/
public class ScrollSwipeRefreshLayout extends SwipeRefreshLayout {
private ViewGroup viewGroup ;
public ScrollSwipeRefreshLayout(Context context) {
super(context);
}
public ScrollSwipeRefreshLayout(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 arg0) {
if(null!=viewGroup){
if(viewGroup.getScrollY()> 1){
//直接截断时间传播
return false;
}else{
return super.onTouchEvent(arg0);
}
}
return super.onTouchEvent(arg0);
}
}
main里面的代码使用跟之前的没有什么区别,只是多了设置一个子view给它。
eg:
package com.spring.swiperefreshlayout;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.spring.swiperefreshlayout.view.ScrollSwipeRefreshLayout;
/**
* 浏览器的下拉更新
*
* @author yd
*
*/
public class WebviewActivity extends Activity implements OnRefreshListener {
private WebView webView = null;
private ScrollSwipeRefreshLayout refreshLayout = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_layout);
webView = (WebView) this.findViewById(R.id.webview);
refreshLayout = (ScrollSwipeRefreshLayout) this.findViewById(R.id.refresh_layout);
refreshLayout.setViewGroup(webView);//设置监听滚动的子view
refreshLayout.setOnRefreshListener(this);
//设置颜色
refreshLayout.setColorScheme(R.color.green, R.color.gray, R.color.blue_50, R.color.light_white);
webView.loadUrl("http://blog.csdn.net/spring_he/article/details/19359099");
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress == 100) {
//设置加载完成后结束动画
refreshLayout.setRefreshing(false);
}
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
@Override
public void onRefresh() {
//下拉重新加载
webView.reload();
}
}