简体   繁体   中英

Android RecyclerView scroll and position item at top

I'm not sure if this is hard to do with a RecyclerView or not, I thought there was an easy way to do it but I'm having some difficulties. I have a RecyclerView on a activity, and when I go to the activity, I want it to scroll to an item. Seems fine enough. However, my issue is that, say only 5 items fit on screen at a time, and we have 6 items, and we scroll to the 5th or 4th item, the recycler view does scroll a tiny bit, but it doesn't look like it scrolls 'to' the element, as it is confined by the RecyclerView bounds.

Is there a way to make it so if I scroll to the 4th or 5th item in the RecyclerView, it will position it at the top of the view, and the area below the last item will just be the background color of the normal view, and I can just scroll back up normally? I hope I'm explaining myself correctly. I'm not sure if this can be done easily and I have made a mistake in the XML setup or code setup.

<merge 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:visibility="gone">

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <include
            android:id="@+id/timelineSwitcherView"
            layout="@layout/view_timeline_view_switcher"/>

    <ViewAnimator
            android:id="@+id/viewAnimator"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/timelineSwitcherView">

        <RelativeLayout
                android:id="@+id/timeLineRelativeLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/lighterBlue">

            <android.support.v4.widget.SwipeRefreshLayout
                    android:id="@+id/timeLineSwipeRefreshLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                <android.support.v7.widget.RecyclerView
                        android:id="@+id/timeLineRecyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>

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

        </RelativeLayout>

Appreciate any help and guidance with this.

You should use

    mRecyclerview.scrollTo(x,y);
    mRecyclerview.scrollToPosition();

Or even better:

    mRecyclerview.smoothScrollBy( x , y);        
    mRecyclerview.smoothScrollToPosition(List item position);

So you need to get the Y position, or the child position in the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM