【问题标题】:Android ScrollView automatically scrolls to the bottom of the viewAndroid ScrollView 自动滚动到视图底部
【发布时间】:2016-04-26 08:09:11
【问题描述】:

我创建了一个包含一个地图、图像和另一个文本视图的活动,并为它添加了“scrollview”标签。但是在活动开始后,它会自动滚动到页面的末尾。请告诉我为什么会发生这种情况以及如何阻止它移动到结尾,以便我自己滚动它。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.radikallab.earch.DetailsActivity"
android:background="@drawable/background10">


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


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="Title"
        android:textStyle="italic"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/background4"
        android:id="@+id/iv"/>


    <TextView
        android:id="@+id/rat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Title"
        android:textStyle="italic"/>

    <TextView
        android:id="@+id/addr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:text="Title"
        android:textStyle="italic"/>

    <WebView

        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:layout_marginTop="10dp"
        android:id="@+id/webView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

【问题讨论】:

    标签: android


    【解决方案1】:

    我也遇到过同样的情况,但是将 descendantFocusability 属性添加到包含 LinearLayout 的 ScrollView,解决了我的问题。

    android:descendantFocusability="blocksDescendants"
    

    您可以将其用作:

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

    【讨论】:

    • 有没有其他方法可以阻止任何不禁用焦点的自动滚动?我在 LinearLayout 中有具有选择功能的 TextView。使用此方法禁用文本选择。
    【解决方案2】:

    在你的线性布局中设置android:focusableInTouchMode="true"

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

    【讨论】:

      【解决方案3】:

      如果您确实想使用 Java 手动设置; 试试这个 - 它确实有效: 使用 FOCUS_UP 滚动到顶部,使用 FOCUS_DOWN 滚动到底部。

      scrollView.post(new Runnable() {
                              public void run() {
                                  scrollView.fullScroll(View.FOCUS_UP);
                              }
      

      【讨论】:

        【解决方案4】:

        对我有用的是 Punit Sharma 和 FAЯAƸ 的答案的结合。

        我的问题是另一个可滚动视图 (NestedScrollView) 内的可滚动视图 (RecyclerView) -- 与 ScrollView 内的 WebView 相同。

        用属性android:descendantFocusability="blocksDescendants" 将我的内部可滚动视图包装在FragmentLayout 中解决了我的自动到底部滚动问题。

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
        
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
        
                ....
        
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:descendantFocusability="blocksDescendants"
                    ...>
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/image_gridview"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
               </FrameLayout>
        
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>
        

        【讨论】:

          【解决方案5】:

          实际上,我找到了一个简单的答案,只需在相关的 java 文件中添加“setfocusable(false)”......在这种情况下,“webview”首先滚动到那里......所以添加“webview. setfocusable(false)" 在关联的 webview java 类中。

          【讨论】:

            【解决方案6】:

            实际上,如果ScrollView 中的子级也像ListView 和您的情况WebView 一样支持滚动,我们将面临滚动问题。

            我发现了一个类似的问题,并提供了一些解决方法。它支持垂直滚动,但如果HTML 页面超过WebView 宽度,则不支持水平滚动。

            参考this

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-08-31
              • 1970-01-01
              • 1970-01-01
              • 2016-10-08
              • 2020-02-11
              • 1970-01-01
              • 1970-01-01
              • 2013-02-14
              相关资源
              最近更新 更多