【问题标题】:How to scroll to the top of a scrollview when a new page in a webview is loaded加载 web 视图中的新页面时如何滚动到滚动视图的顶部
【发布时间】:2013-06-10 19:30:32
【问题描述】:

我在滚动视图中设置了一个 webview。当我最初向下滚动到屏幕底部并单击 web 视图内的链接时,加载的页面在页面中间可见。如何自动滚动到滚动视图的顶部。我用谷歌搜索但没有答案。 iOS 会自动执行此操作,但据我所知,android 不会。任何见解将不胜感激。我设置了一个 smoothscroll(0,0) 但它在移动到下一页之前会滚动到顶部。也许这就是我所能做的。任何其他解决方案将不胜感激。

if(isConnectingToInternet()){
        WebProducts.setVisibility(View.VISIBLE);
        TextNoInternet.setVisibility(View.GONE);
        WebProducts.getSettings().setLoadWithOverviewMode(true);
        WebProducts.getSettings().setUseWideViewPort(true);
        WebProducts.getSettings().setSupportZoom(false);
        WebProducts.loadUrl("http://www.healthfitnessenergy.com/productpages/products.php");
    }
    else{
        WebProducts.setVisibility(View.GONE);
        TextNoInternet.setVisibility(View.VISIBLE);
    }
    WebProducts.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            ScrollView1.smoothScrollTo(0,0);
            return false;
        }
    });

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="30dp"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/products_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="8dp"
        android:gravity="center_vertical"
        android:text="@string/product_qs2_products"
        style="@style/Bold_Black_20" />
    <WebView
        android:id="@+id/wvProducts"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:background="@drawable/gradient_background"
        android:orientation="vertical" >
    </WebView>
    <TextView
        android:id="@+id/ifnointernet"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingLeft="25dp"
        android:gravity="center_vertical"
        android:text="Product Menu will be available when cell/wifi service is available"
        android:textColor="#333333"
        android:textSize="18sp"
        android:textStyle="bold"
        android:visibility="gone" />
</LinearLayout>

【问题讨论】:

    标签: android webview


    【解决方案1】:

    尝试在OnPageStarted()onPageFinished() 中使用setVisibility() 方法来解决问题。基本上删除现有的视图。

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

    【讨论】:

      【解决方案2】:

      将“scrollview.pageScroll(View.FOCUS_UP);”在一些听众帮助你?

      【讨论】:

      • 上面列出的代码是我必须处理的 webview 以保持应用程序内的链接。不知道如何为网页上的按钮建立侦听器以在应用内工作。
      • 如果在您的 WebViewClient 的“onPageFinished”中制作它怎么办?当页面完成加载时,这将滚动。还是我没有达到你的目标?
      【解决方案3】:

      @Kevin M 您的问题可以通过覆盖@Htet Will 回答的两个方法 onPageStarted 和 onPageFinished 来解决。 而您的另一个问题“不确定如何为网页上的按钮建立侦听器以在应用程序内部工作”可以通过 JS Bridge 解决。在 ios 中您必须知道 js 桥接的概念,在 android 中也类似。

      第一个 - 你必须为你的 javascript 界面启用 webview。

      webview.getSettings().setJavaScriptEnabled(true);
      

      2nd - 制作一个 javascript 界面

      @Keep
          private class JavaScriptInterface {
      
              private JavaScriptInterface() {
              }
              @JavascriptInterface
              public void methodName(){
      
                  getActivity().runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          //do what you want in your java class
                      }
                  });
              }
          }
      

      3rd - 将 JavaScriptInterface 分配给 webview

      webView.addJavascriptInterface(new JavaScriptInterface(), "jsSolutionInterface");
      

      4th - 在 html 按钮中添加一个点击方法让我们说 open()。在名为 open 的方法中,您可以调用 javascript 接口方法。js 方法的示例:

      function open()
      {jsSolutionInterface.methodName();}
      

      通过这种方式可以桥接js和java方法。希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        相关资源
        最近更新 更多