How to prevent a scrollview from scrolling to a webview after data is loaded?

36,862

Solution 1

You can simply add this to your LinearLayout: android:focusableInTouchMode="true". It works for me.

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

Solution 2

I had the same problem, after hours of trying several ideas, what finally worked for me was simply adding the descendantFocusability attribute to the ScrollView's containing LinearLayout, with the value blocksDescendants. In your case:

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

Haven't had the problem reoccur since.

Solution 3

You should create new class extend ScrollView, then Override requestChildFocus:

public class MyScrollView extends ScrollView {

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

Then in your xml layout, using:

<MyScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background" >

That works for me. The ScrollView will not auto scroll to the WebView anymore.

Solution 4

Adding these line in main layout solves the problem

android:descendantFocusability="blocksDescendants"

Solution 5

Like this:

<com.ya.test.view.MyScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="beforeDescendants"
        android:focusable="true"
        android:focusableInTouchMode="true" >
Share:
36,862

Related videos on Youtube

Navarr
Author by

Navarr

PHP Developer, Material Design enthusiast, Semantic HTML Evangelist. Magento Developer @ swift Otter

Updated on July 08, 2022

Comments

  • Navarr
    Navarr almost 2 years

    So I have a fascinating problem. Despite the fact that I'm not manually or programmatically scrolling my view, my WebView is being automatically scrolled to after the data inside it loads.

    I've got a fragment in a viewpager. When I first load the pager, it works as expected and everything is shown. But once I "flip the page" the data loads and the WebView pops up to the top of the page, hiding the views above it, which is undesirable.

    Does anyone know how to prevent this from happening?

    My layout looks like such:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/article_title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="2dp"
                android:text="Some Title"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/article_title"
                android:textStyle="bold" />
    
            <LinearLayout
                android:id="@+id/LL_Seperator"
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:background="@color/text"
                android:orientation="horizontal" >
            </LinearLayout>
    
            <WebView
                android:id="@+id/article_content"
                android:layout_width="match_parent"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_height="wrap_content" />
    
            <TextView
                android:id="@+id/article_link"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:text="View Full Article"
                android:textColor="@color/article_title"
                android:textStyle="bold" />
        </LinearLayout>
    
    </ScrollView>
    

    I'm also not giving focus to anything. By default, it seems to automatically scroll to the WebView after it has loaded. How do I prevent this?

    • znat
      znat about 12 years
      why exactly do you need the ScrollView as a WebView is scrollable?
    • Navarr
      Navarr about 12 years
      To keep some aspects out of the webview instead of rendering them as HTML. For speed and quality.
    • AndrewS
      AndrewS over 10 years
      mjp66 has the best answer - may be you should accept his answer
  • Erik B
    Erik B over 11 years
    Also needed constructors: public ExtendedScrollView(Context context) { super(context); } public ExtendedScrollView( Context context, AttributeSet attributeSet) { super(context, attributeSet); } public ExtendedScrollView( Context context, AttributeSet attributeSet, int defStyle) { super(context, attributeSet, defStyle); }
  • Cory Trese
    Cory Trese about 10 years
    Does this have any negative impact on the accessibility of these controls?
  • djhworld
    djhworld about 10 years
    Note that if there are any child views like EditText, this method will cause them to become uneditable.
  • Colin M.
    Colin M. almost 10 years
    This question/answer doesn't come up when you search for the words ScrollView Webview autoscroll, and it should. Maybe this comment will help! Had the same issue with using a WebViewFragment and didn't find this question even after lots of searching until I posted my own question (now deleted).
  • VVB
    VVB about 8 years
    Good answer. It would be good if you can explain View priorities for taking focus in android
  • Vaibhav Jani
    Vaibhav Jani almost 8 years
    Works without side effects!
  • YumeYume
    YumeYume over 7 years
    Working on a Xamarin.Android project, had auto scroll trouble in a scrollview with multiple gridviews, populated asynchronously with downloaded images. This solution worked so perfectly I had trouble to believe it at first sight and had to test it again.
  • Minas Mina
    Minas Mina about 7 years
    The very same issue happens for Ads in a RecyclerView as well. Your solution works in that case as well :)
  • Justin Liu
    Justin Liu about 7 years
    I think it's because when onLayout() is called in any ScrollView, it checks to see if any child has focus. If they do, the ScrollView will automatically scroll to that child. When new things load inside the ScrollView, I believe it somehow takes the focus, and that's what causes the problem.
  • Killesk
    Killesk almost 7 years
    I had a recycler view (A), inside a recycler view (B) and the page was jumping when the data was put into recycler view (B). I tried overriding the laymanager for recycler view (A) and disabling vertical scrolling and it was a solution, but I needed to enable it after the data was entered into recycler view (B) but I got it working. Messy fix. This fix has saved me so much hassle. Cheers