【问题标题】:NestedScrollView scroll down itself when content is fillsNestedScrollView 在内容填充时自行向下滚动
【发布时间】:2023-03-29 13:35:02
【问题描述】:

我有 xml,它由 DrawerLayoutCoordinatorLayoutcustom viewsAppBarLayoutNestedScrollView 组成。

问题:NestedtScrollView 中的内容填满时,NestedtScrollView 会自行向下滚动。 scrollView.setScrollY(0)layout_behavior = FixedScrollingViewBehavior 的自定义类等所有研究都没有帮助我。

如何防止这种滚动

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">


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

    <android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:background="@color/semitransparet_color_primary">

            <android.support.v7.widget.Toolbar
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/actionbar_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="?attr/actionBarSize"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:elevation="4dp"/>

        </android.support.design.widget.AppBarLayout>

        <ProgressBar
                android:id="@+id/progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"/>
        <android.support.v4.widget.NestedScrollView
                android:id="@+id/product_scroll_wrpr"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="gone">

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

                // my content is here

            </LinearLayout>


        </android.support.v4.widget.NestedScrollView>



    </android.support.design.widget.CoordinatorLayout>

    <LinearLayout
            android:id="@+id/buttons_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:background="#01579B"
            android:layout_gravity="bottom"
            android:orientation="horizontal"
            android:padding="8dp"
            android:gravity="center_vertical">

    // here are my buttons

</LinearLayout>

<android.support.design.widget.NavigationView
        android:id="@+id/navi"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@android:color/white"
        app:headerLayout="@layout/drawer_header_left"
        app:menu="@menu/nav_drawer_menu"/>

我的 build.gradle 构成

compile 'com.android.support:support-v4:23.1.0'

compile 'com.android.support:design:23.0.1'

【问题讨论】:

    标签: android nestedscrollview


    【解决方案1】:

    尝试将android:descendantFocusability="blocksDescendants" 设置为NestedScrollView 内的LinearLayout。它对我有用。

    UPD: 小心在布局中使用像EditText 这样的后代元素,它应该获得焦点:这些元素不会获得焦点。如果您知道如何解决此问题,请告诉我们。

    【讨论】:

    • 它成功了,但是这个的解释是什么?
    • 是不是表示关注里面的views会被屏蔽,用户无法访问?
    • 也许你可以再解释一下,这会提高你的回答质量。
    • 如果您有任何带有android:textIsSelectable="true"TextView 元素,这将阻止文本选择。最好将父视图设置为可聚焦。
    • 如果我能投上千张赞成票,我会这样做。挣扎了两天,试图理解为什么会发生这种情况,或者至少想出一个解决方法。在我找到这个解决方案之前,它还有很多数据绑定,所以看起来完全像一个黑魔法。
    【解决方案2】:

    另一种阻止焦点的方法是添加一个新的视图来窃取焦点。将它放在 NestedScrollView 上方的任何位置,它应该可以工作:

        <!--focusStealer, to avoid NestedScrollingView to scroll due to dynamically created views that take the focus-->
        <View
            android:layout_width="0px" android:layout_height="0px" android:focusable="true"
            android:focusableInTouchMode="true"/>
    

    【讨论】:

    • 如果您的 NestedScrollView 包含 EditText 视图,这将正常工作!
    【解决方案3】:

    这对我有用:

    mNestedScrollViewChat.post(new Runnable(){
        @Override
        public void run(){
            mNestedScrollViewChat.fullScroll(View.FOCUS_DOWN);
        }
    });
    

    【讨论】:

      【解决方案4】:

      这在 Kotlin 中对我有用

       nestedScrollView.post { nestedScrollView.fullScroll(View.FOCUS_DOWN) }
      

      【讨论】:

        【解决方案5】:

        在 Nested ScrollView 的子视图中添加 android:descendantFocusability="beforeDescendants" 并添加 android:focusableInTouchMode="true"android:focusableInTouchMode="true"' 因为nestedScrollView 需要在滚动之前聚焦一个视图。我尝试从子视图中聚焦 Edittext 以获得焦点

         <EditText
                android:id="@+id/edtAddressAccount"
                style="@style/StyleEditText"
                android:autofillHints=""
                android:ems="10"
                android:inputType="textPersonName"
                android:singleLine="true"
                android:nextFocusUp="@+id/edtAddressAccount"
                android:nextFocusLeft="@id/edtAddressAccount"
                app:layout_constraintEnd_toEndOf="@+id/edtDistrict"
                app:layout_constraintStart_toStartOf="@+id/textView13"
                app:layout_constraintTop_toBottomOf="@+id/textView13" />
        

        完整代码在这里

         <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical"
        android:id="@+id/nestedAccount"
        android:layout_weight="1"
        tools:context="jp.co.ramen.ui.auth.account.AccountFragment">
        
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:descendantFocusability="beforeDescendants"
        
            >
        
            <jp.co.ramen.utils.customview.DropDown
                android:id="@+id/spGenderAccount"
                style="@style/StyleSpinner"
                app:layout_constraintEnd_toEndOf="@+id/edtAddressAccount"
                app:layout_constraintStart_toStartOf="@+id/textView14"
                app:layout_constraintTop_toBottomOf="@+id/textView14" />
        
            <include
                android:id="@+id/textView14"
                layout="@layout/include_text_require"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_12sdp"
                app:layout_constraintStart_toStartOf="@+id/edtAddressAccount"
                app:layout_constraintTop_toBottomOf="@+id/edtAddressAccount" />
        
            <EditText
                android:id="@+id/edtDistrict"
                style="@style/StyleEditText"
                android:autofillHints=""
                android:ems="10"
                android:inputType="textPersonName"
                android:singleLine="true"
                android:nextFocusUp="@+id/edtAddressAccount"
                android:nextFocusLeft="@id/edtAddressAccount"
                app:layout_constraintEnd_toEndOf="@+id/liner2"
                app:layout_constraintStart_toStartOf="@+id/textView12"
                app:layout_constraintTop_toBottomOf="@+id/textView12" />
        ...more view
        </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>
        

        【讨论】:

          猜你喜欢
          • 2021-10-20
          • 1970-01-01
          • 2019-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多