简体   繁体   English

Android RecyclerView滚动和位置项位于顶部

[英]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. 我不确定使用RecyclerView是否很难做到这一点,我认为有一种简单的方法可以做到,但是我遇到了一些困难。 I have a RecyclerView on a activity, and when I go to the activity, I want it to scroll to an item. 我在一个活动上有一个RecyclerView,当我转到该活动时,我希望它滚动到某个项目。 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. 但是,我的问题是,一次只能显示5个项目,而我们有6个项目,然后滚动到第5个或第4个项目,recycler视图的确滚动了一点,但看起来不像它滚动到“元素”,因为它受RecyclerView边界限制。

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? 有没有办法做到这一点,如果我滚动到RecyclerView中的第4或第5个项目,它将把它放在视图的顶部,而最后一个项目下面的区域将只是正常视图的背景颜色,我可以正常向上滚动吗? 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. 我不确定是否可以轻松完成此操作,并且在XML设置或代码设置中犯了一个错误。

<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. 因此,您需要在列表中获得Y位置或子位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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