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

时间:2017-01-27 13:49:45

标签: java android xml list android-recyclerview

我不确定使用RecyclerView是否很难,我认为有一种简单的方法可以做到,但我遇到了一些困难。 我在活动上有一个RecyclerView,当我去活动时,我希望它滚动到一个项目。看起来很好。但是,我的问题是,一次只有5个项目适合屏幕,我们有6个项目,我们滚动到第5或第4项,回收器视图确实滚动了一点,但它看起来不像它滚动到'元素,因为它受到RecyclerView边界的限制。

如果我滚动到RecyclerView中的第4或第5项,有没有办法实现它,它会将它定位在视图的顶部,最后一项下方的区域将只是背景颜色普通视图,我可以正常向上滚动吗? 我希望我能正确解释自己。我不确定这是否可以轻松完成,我在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>

感谢任何帮助和指导。

1 个答案:

答案 0 :(得分:1)

你应该使用

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

甚至更好:

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

因此,您需要获取列表中的Y位置或子位置。