刷新Webview的“ SwipeRefreshLayout”仅一次

时间:2019-12-17 15:33:05

标签: android webview

SwipeRefreshLayout仅工作一次,如何解决此问题:

 <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

 <RelativeLayout
            android:id="@+id/nonVideoLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:windowSoftInputMode="adjustResize">

            <ProgressBar
                android:id="@+id/progressbar"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="4dp"
                android:scaleY="4.0" />
          <WebView
                android:id="@+id/webid"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:windowSoftInputMode="adjustResize"
                app:layout_constraintDimensionRatio="1" />

        </RelativeLayout>



    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


</RelativeLayout>

Java:

 public class Eboardresult extends AppCompatActivity {

    WebView webViewid;
    TextView mywebtitle;
    ProgressBar supperprogressBar;
    String url = "https://google.com/";
    private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
    SwipeRefreshLayout mySwipeRefreshLayout;



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

        mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
        mywebtitle = findViewById(R.id.websitetitle);
        webViewid = (WebView) findViewById(R.id.webid);
        // FloatingActionButton fab = findViewById(R.id.fab);
        supperprogressBar = findViewById(R.id.progressbar);



        webViewid.loadUrl(url);
        webViewid.setWebViewClient( new WebViewClient());
        WebSettings webSettings= webViewid.getSettings();
        webViewid.getSettings().setJavaScriptEnabled(true);
        WebSettings settings =webViewid.getSettings();
        settings.setDisplayZoomControls(false);
        settings.setSupportZoom(true);
        settings.supportZoom();
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setBuiltInZoomControls(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webViewid.clearCache(false);
        String userAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0" +
                            "(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
        webViewid.getSettings().setUserAgentString(userAgent);
        webViewid.clearHistory();
        settings.getSaveFormData();
        settings.getSaveFormData();
        webViewid.setWebChromeClient( new WebChromeClient());


        webViewid.setWebChromeClient( new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                supperprogressBar.setProgress(newProgress);
                if (newProgress==100){
                    supperprogressBar.setVisibility(View.VISIBLE);
                }else {
                    supperprogressBar.setVisibility(View.VISIBLE);
                }
            }
        });








        mySwipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        webViewid.reload();
                    }
                }
        );

        mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);

        mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
                new ViewTreeObserver.OnScrollChangedListener() {
                    @Override
                    public void onScrollChanged() {
                        if (webViewid.getScrollY() == 0)
                            mySwipeRefreshLayout.setEnabled(true);
                        else
                            mySwipeRefreshLayout.setEnabled(false);

                    }
                });


    }  

1 个答案:

答案 0 :(得分:0)

使用mySwipeRefreshLayout.setEnabled();代替使用mySwipeRefreshLayout来停止mySwipeRefreshLayout.setRefreshing(false) Link转换为Android文档