Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestedScrollView scroll down itself when content is fills

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'
like image 212
Ognev Zair Avatar asked Dec 22 '15 16:12

Ognev Zair


People also ask

How do I stop NestedScrollView scrolling?

setnestedscrollingenabled set it to false.

What is nested scrolling?

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. It is enabled by default. NestedScrollView is used when there is a need for a scrolling view inside another scrolling view.

How do I scroll NestedScrollView to bottom programmatically?

Try this: scrollView. post(new Runnable() { @Override public void run() { scrollView. fullScroll(ScrollView.

What is difference between nested ScrollView and NestedScrollView?

Nested scrolling is enabled by default. Show activity on this post. NestedScrollView is just like ScrollView, but in NestedScrollView we can put other scrolling views as child of it, e.g. RecyclerView. But if we put RecyclerView inside NestedScrollView, RecyclerView's smooth scrolling is disturbed.


3 Answers

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.

like image 164
Konstantin Konopko Avatar answered Oct 21 '22 17:10

Konstantin Konopko


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"/>
like image 19
android developer Avatar answered Oct 21 '22 17:10

android developer


This working for me:

mNestedScrollViewChat.post(new Runnable(){
    @Override
    public void run(){
        mNestedScrollViewChat.fullScroll(View.FOCUS_DOWN);
    }
});
like image 6
Hitesh sapra Avatar answered Oct 21 '22 16:10

Hitesh sapra