62

I have xml, which consits of DrawerLayout, CoordinatorLayout with custom views, AppBarLayout, NestedScrollView.

Problem: When content in NestedtScrollView fills, NestedtScrollView scrolls down itself. All researches like scrollView.setScrollY(0) or custom class for layout_behavior = FixedScrollingViewBehavior didn't help me.

How do i prevent this scrolling

    <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"/>

my build.gradle consits

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

compile 'com.android.support:design:23.0.1'
Apurva
  • 7,871
  • 7
  • 40
  • 59
Ognev Zair
  • 1,626
  • 1
  • 13
  • 17

5 Answers5

207

Try to set android:descendantFocusability="blocksDescendants" to the LinearLayout inside NestedScrollView. It works for me.

UPD: beware of using into the layout descendant elements like EditText, which should take a focus: that elements will not take a focus. If you know how to solve this, please let us to know.

Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62
  • It worked, but what is the explanation for this one? – David Jul 21 '17 at 14:37
  • Does it mean that focus on the views inside will be blocked, and users won't be able to reach them? – android developer Aug 10 '17 at 11:25
  • 2
    Maybe you could explain this a little bit further, this would improve your answer quality. – Stefan Sprenger Feb 14 '18 at 15:43
  • 2
    If you have any `TextView` elements with `android:textIsSelectable="true"` this will block text selection. Better to just set the parent view to focusable. – Victor Rendina Feb 16 '18 at 04:41
  • If I can put a thousands of upvotes I would do it. Was struggling for two days trying to understand why it happens or at least make a workaround. It was also a lot of data binding around so totally looked like a black magic before I found this solution. – Mikhail Apr 24 '20 at 02:45
19

An alternative to blocking the focus, is to add a new View that will steal the focus. Put it anywhere above the NestedScrollView, and it should work:

    <!--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"/>
android developer
  • 114,585
  • 152
  • 739
  • 1,270
6

This working for me:

mNestedScrollViewChat.post(new Runnable(){
    @Override
    public void run(){
        mNestedScrollViewChat.fullScroll(View.FOCUS_DOWN);
    }
});
Hitesh sapra
  • 248
  • 2
  • 12
2

This worked for me in Kotlin

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

add android:descendantFocusability="beforeDescendants" and add and android:focusableInTouchMode="true"android:focusableInTouchMode="true"' in children view of Nested ScrollView becase nestedScrollView need focus a view before scroll. i try focus a Edittext from children of view to get focus

 <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" />

full code here

 <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>
Minh Nhat
  • 1
  • 1