1

I have a fragment_layout.xml with two buttons (filter_1_btn, filter_2_btn) that perform filtering to the items of a RecyclerView. The problem is that when I scroll a bit (because the TextView above the buttons contains multiple lines of text) and then apply the filtering, then the NestedScrollView scrolls on top of the screen. Is there a way to stay at the same scrolled height after the filtering?

fragment_layout

<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nested_scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical"> <TextView android:id="@+id/description_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="36dp" android:text="Very long random text..." /> <Button android:id="@+id/filter_1_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Filter 1"/> <Button android:id="@+id/filter_2_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Filter 2"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="24dp" /> </LinearLayout> </androidx.core.widget.NestedScrollView> 
1
  • can you post the code of filtering, how are you setting data after filter? Commented Aug 18, 2021 at 13:24

1 Answer 1

3

This is happening because the recyclerView's container is set to wrap_content , so when the height get smaller than the NestedScrollView, it gets scrolled to top.

you can fix it by providing a height larger than NestedScrollView :

So add minHeight :

<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/nested_scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical"> <TextView android:id="@+id/description_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="36dp" android:text="Very long random text..." /> <Button android:id="@+id/filter_1_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Filter 1" /> <Button android:id="@+id/filter_2_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Filter 2" /> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="1000dp"> <!-- Add min height to support scrolling--> <androidx.recyclerview.widget.RecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" /> </FrameLayout> </LinearLayout> </androidx.core.widget.NestedScrollView> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.