Recycler view inside NestedScrollView causes scroll to start in the middle

后端 未结 11 1005
眼角桃花
眼角桃花 2020-12-12 10:07

I am getting a weird scrolling behavior when I add a RecyclerView inside a NestedScrollView.

What happens is that whenever the scrollview has more rows than can be s

相关标签:
11条回答
  • 2020-12-12 10:30

    In Java code, after initializing your recyclerView and setting the adapter, add this line:

    recyclerView.setNestedScrollingEnabled(false)
    

    You can also try to wrap the layout withing a relativeLayout so that the views stay at the same position but recyclerView (which scroll) is first in xml hierarchy. The last suggestion a desperate attempt:p

    0 讨论(0)
  • 2020-12-12 10:31

    I solved such issue by setting:

    <ImageView ...
    android:focusableInTouchMode="true"/>
    

    to my view above RecyclerView (which was hidden after unwanted scroll). Try to set this property to your LinearLayout above RecyclerView or to LinearLayout which is container of RecyclerView (helped me in another case).

    As I see in NestedScrollView source it tries to focus the first possible child in onRequestFocusInDescendants and if only RecyclerView is focusable it wins.

    Edit (thanks to Waran): and for smooth scroll don't forget to set yourRecyclerView.setNestedScrollingEnabled(false);

    0 讨论(0)
  • 2020-12-12 10:39

    I have two guesses.

    First:Try putting this line on your NestedScrollView

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    

    Second: Use

    <android.support.design.widget.CoordinatorLayout
    

    as your parent view Like this

    <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="10dp">
    
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:orientation="vertical"
                          android:padding="16dp">
    
                <TextView
                    style="@style/TextAppearance.AppCompat.Caption"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title:"/>
    
                <TextView
                    style="@style/TextAppearance.AppCompat.Body1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/bodyPadding"
                    android:text="Neque porro quisquam est qui dolorem ipsum"/>
    
                <TextView
                    style="@style/TextAppearance.AppCompat.Caption"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Subtitle:"/>
    
                <TextView
                    style="@style/TextAppearance.AppCompat.Body1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/bodyPadding"
                    android:text="Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."/>
    
            </LinearLayout>
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"/>
    
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    

    My last possible solution. I swear :)

    0 讨论(0)
  • 2020-12-12 10:40

    This problem arrives due to recycle view Focus.

    Automatically all focus gone to recycle view if its size extended the size of screen.

    adding android:focusableInTouchMode="true" to first ChildView like TextView, Button and so on(Not on ViewGroup like Linear, Relative and So on) make sense to solve the problem but API Level 25 and above solution doesn't work.

    Just add these 2 line in your ChildView like TextView, Button and So on (Not on ViewGroup like Linear, Relative and So on)

     android:focusableInTouchMode="true"
     android:focusable="true"
    

    I just faced this problem on API level 25. I hope other people don't waste time in this.

    For Smooth Scrolling On RecycleView add this line

     android:nestedScrollingEnabled="false"
    

    but adding this attiributes only works with API level 21 or above. If you want that smoothing scrolling work on below API level 25 then add this line in your class

     mList = findViewById(R.id.recycle_list);
     ViewCompat.setNestedScrollingEnabled(mList, false);
    
    0 讨论(0)
  • 2020-12-12 10:42
    android:descendantFocusability="blocksDescendants"
    

    inside LinearLayout Worked for me .

    0 讨论(0)
提交回复
热议问题