How to prevent a scrollview from scrolling to a webview after data is loaded?
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" >
Related videos on Youtube
Navarr
PHP Developer, Material Design enthusiast, Semantic HTML Evangelist. Magento Developer @ swift Otter
Updated on July 08, 2022Comments
-
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 about 12 yearswhy exactly do you need the ScrollView as a WebView is scrollable?
-
Navarr about 12 yearsTo keep some aspects out of the webview instead of rendering them as HTML. For speed and quality.
-
AndrewS over 10 yearsmjp66 has the best answer - may be you should accept his answer
-
-
Erik B over 11 yearsAlso 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 about 10 yearsDoes this have any negative impact on the accessibility of these controls?
-
djhworld about 10 yearsNote that if there are any child views like EditText, this method will cause them to become uneditable.
-
Colin M. almost 10 yearsThis 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 about 8 yearsGood answer. It would be good if you can explain View priorities for taking focus in android
-
Vaibhav Jani almost 8 yearsWorks without side effects!
-
YumeYume over 7 yearsWorking 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 about 7 yearsThe very same issue happens for Ads in a
RecyclerView
as well. Your solution works in that case as well :) -
Justin Liu about 7 yearsI 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 almost 7 yearsI 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