Android SwipeRefreshLayout webview无法向下滚动

时间:2016-09-08 07:37:55

标签: android

我是android的新手。目前我想在webview中使用SwipeRefreshLayout,但我无法在webview中向下滚动,每次向下滚动都会触发SwipeRefreshLayout事件。任何建议和步骤我都可以避免这种情况?以下是我所参考的样本编码。

这是我目前的布局:

   <android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/swipe">


    <WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


</android.support.v4.widget.SwipeRefreshLayout>

这是我的活动

package com.example.web3;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

       private WebView webView;
        private SwipeRefreshLayout swipeRefresh;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            //SwipeRefresh
            swipeRefresh = (SwipeRefreshLayout)findViewById(R.id.swipe_contain);
            swipeRefresh.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh() {

                    webView.loadUrl(webView.getUrl());
                }
            });
            swipeRefresh.setColorScheme(android.R.color.holo_blue_light, 
                                                 android.R.color.holo_red_light, 
                                                 android.R.color.holo_orange_light, 
                                                 android.R.color.holo_green_light);

            webView = (WebView)findViewById(R.id.web_show);

            webView.loadUrl("http://192.168.0.28/sample/index.html");

            webView.getSettings().setJavaScriptEnabled(true);

            webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

            webView.requestFocus(); 

            webView.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });

            webView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                  @Override
                  public void onScrollChanged() {
                     if (webView.getScrollY() == 0) {
                         swipeRefresh.setEnabled(true);
                      } else {
                          swipeRefresh.setEnabled(false);
                      }
                  }
                });



            webView.setWebChromeClient(new WebChromeClient(){
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    if(newProgress == 100){

                        swipeRefresh.setRefreshing(false);
                    }else if(!swipeRefresh.isRefreshing()){
                        swipeRefresh.setRefreshing(true);

                    }
                }
            });
        }



}

1 个答案:

答案 0 :(得分:2)

试试这个:

webView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            if (webView.getScrollY() == 0) {
                swipeToRefresh.setEnabled(true);
            } else {
                swipeToRefresh.setEnabled(false);
            }
        }
    });