4

I know there are similar questions and I already tried and implemented all those solution suggestions. However, none of them worked with me.Even Though WebView Finished Loading Data, ScrollView Never Stops Scrolling to the Bottom. I want to use pull to refresh and hide the action bar with onScrollListener. Everything is good but I can't prevent scrolView from non-stop scrolling to the bottom.

I read this: How to prevent a scrollview from scrolling to a webview after data is loaded?

I read this as well: webview is forcing my scrollview to the bottom

I tried to implement a custom scrollview which overrides requestChildFocus method.

I added android:descendantFocusability="blocksDescendants" to my xml file.

I added android:focusableInTouchMode="true" too but still no good, no change. It still forces to scroll forever.

Here is my Custom scrollview:

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);
    }
}

Here is my xml file:

<?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>

For example imagine you opened Google.com on this webview. After page loaded it incredibly scrolls down to the bottom you can't even see any information at the bottom of the page. How can I fix this problem? Any help would be extremely appreciated! Thanks in advance!

By the way adding android:descendantFocusability="blocksDescendants" causing problems like you can't search once Google.com opened you can't enter any input to the Google's search bar!

Community
  • 1
  • 1
Les Paul
  • 219
  • 2
  • 5
  • 15

1 Answers1

0

I got the same issues. My solution is to set the height of the webview equal to its content right after loading 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);

                }
            });

Please note that RelativeLayout is the parent of the WebView in my case, you should change to your corresponding one.

Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21