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

后端 未结 7 1659
梦如初夏
梦如初夏 2020-11-29 16:22

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

相关标签:
7条回答
  • 2020-11-29 16:43

    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.

    0 讨论(0)
  • 2020-11-29 16:43

    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.

    0 讨论(0)
  • 2020-11-29 16:47

    Probably there are people who have the same problem I was having, so I'll help out.

    I was trying to put android:descendantFocusability="beforeDescendants" in my main ScrollView as following:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="beforeDescendants">
        /*my linearlayout or whatever to hold the views */
    

    and it wasn't working, so I had to make a RelativeLayout the parent of the ScrollView, and place the android:descendantFocusability="beforeDescendants" in the parent aswell.

    So I solved it doing the following:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants">
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    /*my linearlayout or whatever to hold the views */
    
    0 讨论(0)
  • 2020-11-29 16:51

    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" >
    
    0 讨论(0)
  • 2020-11-29 16:56

    I had to use the fully qualified name for MyScrollView, otherwise I got an inflate exception.

    <com.mypackagename.MyScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background" >
    
    0 讨论(0)
  • 2020-11-29 16:59

    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" >
    
    0 讨论(0)
提交回复
热议问题