2015-12-29 105 views
4

我知道有类似的问题,我已经尝试并实施所有这些解决方案建议。但是,他们都没有和我一起工作。尽管WebView完成加载数据,但ScrollView从不停止滚动到底部。我想用拉来刷新并隐藏操作栏onScrollListener。一切都很好,但我无法阻止从不停滚动到底部的scrolView如何停止滚动浏览到WebView当前加载页面的底部

我这样说的:How to prevent a scrollview from scrolling to a webview after data is loaded?

我读这还有:webview is forcing my scrollview to the bottom

我试图实现一个自定义scrollview它覆盖requestChildFocus方法。

我将android:descendantFocusability="blocksDescendants"添加到我的xml文件中。

我也加了android:focusableInTouchMode="true",但还是不行,没有变化。它仍然强制永久滚动。

这里是我的自定义滚动型:

package com.example.john.cosmicbrowser; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.webkit.WebView; 
import android.widget.ScrollView; 

public class MyScrollView extends ScrollView 
{ 
    public MyScrollView(Context context) 
    { 
     super(context); 
    } 

    public MyScrollView(Context context, AttributeSet attributeSet) 
    { 
     super(context, attributeSet); 
    } 

    public MyScrollView(Context context, AttributeSet attributeSet, int defStyle) 
    { 
     super(context, attributeSet, defStyle); 
    } 

    @Override 
    public void requestChildFocus(View child, View focused) 
    { 
     if (focused instanceof WebView) 
      return; 
     super.requestChildFocus(child, focused); 
    } 
} 

这里是我的xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/swipe" 
    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:descendantFocusability="blocksDescendants" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".WebActivity" 
    tools:showIn="@layout/activity_webview" 
    > 


    <com.example.john.cosmicbrowser.MyScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:focusableInTouchMode="true" 
     android:descendantFocusability="blocksDescendants" 
     > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:descendantFocusability="blocksDescendants" 
      > 

      <WebView 
       android:id="@+id/webView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_centerVertical="true" 
       android:padding="0dp"/> 

      <ProgressBar 
       android:id="@+id/cosmicProgressBar" 
       style="?android:attr/progressBarStyleHorizontal" 
       android:layout_width="match_parent" 
       android:layout_height="4dp" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_alignParentTop="true" 
       android:indeterminate="false"/> 

     </RelativeLayout> 
    </com.example.john.cosmicbrowser.MyScrollView> 


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

例如想象你在此的WebView打开Goog​​le.com。页面加载完成后,它会不可思议地向下滚动到底部,甚至不能在页面底部看到任何信息。我该如何解决这个问题?任何帮助将非常感谢!提前致谢!

通过添加android:descendantFocusability="blocksDescendants"导致问题像曾经Google.com打开,你不能进入任何输入到谷歌的搜索栏,你不能搜索的方式!

回答

0

我得到了同样的问题。我的解决办法是设置的WebView加载URL之后等于其内容的高度:

mWebView.setWebViewClient(new WebViewClient(){ 
       @Override 
       public void onPageFinished(WebView view, String url) { 
        super.onPageFinished(view, url); 
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mWebView.getMeasuredHeight()); 
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
        mWebView.setLayoutParams(params); 

       } 
      }); 

请注意,RelativeLayout的是在我的情况下的WebView的家长,你应该更改为相应的一个。

相关问题