簡體   English   中英

無法在 android WebView 中滾動到頂部

[英]Unable to scroll to top in android WebView

我正在開發我在 android 中的第一個項目,因為我正在創建瀏覽器,所以我添加了進度條並滑動以刷新選項。

當我滾動到頁面底部時,我綁定到滾動到頁面頂部,但是當我嘗試滾動到頂部時觸發了swipe refresh選項。

問題:

  • 無法滾動到頁面頂部。

有人幫我解決這個問題

主.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/mySwipeLayout"
    tools:context=".MainActivity">

   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

       <LinearLayout
           android:id="@+id/myLinearLayout"
           android:orientation="horizontal"
           android:layout_width="match_parent"
           android:layout_height="3dp">


           <ProgressBar
               android:id="@+id/myProgressBar"
               android:layout_weight="0.1"
               style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />

           <ImageView
               android:id="@+id/myImageView"
               android:src="@mipmap/ic_launcher"
               android:layout_weight="0.9"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />

       </LinearLayout>


       <WebView
           android:id="@+id/myWebView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"></WebView>

   </LinearLayout>

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

主要活動.java

package com.sanoj.goproxy;

import android.graphics.Bitmap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ProgressBar superProgressBar;
    ImageView superImageView;
    WebView superWebView;
    LinearLayout superLinearLayout;
    SwipeRefreshLayout superSwipeLayout;

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

        superSwipeLayout = findViewById(R.id.mySwipeLayout);
        superLinearLayout = findViewById(R.id.myLinearLayout);
        superProgressBar = findViewById(R.id.myProgressBar);
        superImageView = findViewById(R.id.myImageView);
        superWebView = findViewById(R.id.myWebView);

        superProgressBar.setMax(100);

        superWebView.loadUrl("http://safebrowser.tk");
        superWebView.getSettings().setJavaScriptEnabled(true);
        superWebView.setWebViewClient(new WebViewClient(){

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

            @Override
            public void onPageFinished(WebView view, String url) {
                superLinearLayout.setVisibility(View.GONE);
                superSwipeLayout.setRefreshing(false);
                super.onPageFinished(view, url);
            }
        });

        superWebView.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                superProgressBar.setProgress(newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                getSupportActionBar().setTitle(title);
            }

            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
                superImageView.setImageBitmap(icon);
            }
        });
        superSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                superWebView.reload();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.super_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){

            case R.id.menu_back:
            onBackPressed();
            break;

            case R.id.menu_forward:
                onForwardPressed();
                break;

            case R.id.menu_refresh:
                superWebView.reload();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void onForwardPressed(){
        if (superWebView.canGoForward()){
            superWebView.goForward();
        } else {
            Toast.makeText(getApplicationContext(), "Can't Go Further..!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onBackPressed() {
        if (superWebView.canGoBack()){
            superWebView.goBack();
        }else {
            finish();
        }

    }
}

我用mScrollView.fullScroll(mScrollView.FOCUS_DOWN);<\/code> 滾動到頂部,

並感謝https:\/\/stackoverflow.com\/a\/8684666\/3836908<\/a>對答案的信任

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM