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?
setScrollContainer(false); Don't forget to add the webview. setOnTouchListener(...) code above to disable all scrolling in the webview.
In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.
Scroll view supports vertical scrolling only. For horizontal scrolling, use HorizontalScrollView instead. Never add a RecyclerView or ListView to a scroll view.
ScrollView is used to put different or same child views or layouts and the all can be scrolled. ListView is used to put same child view or layout as multiple items. All these items are also scrollable. Simply ScrollView is for both homogeneous and heterogeneous collection.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With