Java Android RecyclerView滚动并将项目放置在顶部
我不确定这是不是很难做到与一个回收或没有,我认为有一个简单的方法来做到这一点,但我有一些困难。 我有一个活动的RecyclerView,当我去活动时,我希望它滚动到一个项目。看起来很好。然而,我的问题是,假设一次只有5个项目可以在屏幕上显示,我们有6个项目,我们滚动到第5个或第4个项目,recycler视图确实滚动了一点点,但它看起来不像滚动到元素,因为它受到RecyclerView边界的限制 如果我滚动到RecyclerView中的第4个或第5个项目,它会将其定位在视图的顶部,最后一个项目下方的区域将只是普通视图的背景色,并且我可以正常地向后滚动吗? 我希望我的解释是正确的。我不确定这是否容易做到,我在XML设置或代码设置中犯了一个错误Java Android RecyclerView滚动并将项目放置在顶部,java,android,xml,list,android-recyclerview,Java,Android,Xml,List,Android Recyclerview,我不确定这是不是很难做到与一个回收或没有,我认为有一个简单的方法来做到这一点,但我有一些困难。 我有一个活动的RecyclerView,当我去活动时,我希望它滚动到一个项目。看起来很好。然而,我的问题是,假设一次只有5个项目可以在屏幕上显示,我们有6个项目,我们滚动到第5个或第4个项目,recycler视图确实滚动了一点点,但它看起来不像滚动到元素,因为它受到RecyclerView边界的限制 如果我滚动到RecyclerView中的第4个或第5个项目,它会将其定位在视图的顶部,最后一个项目下方
<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>
感谢您的帮助和指导。您应该使用
mRecyclerview.scrollTo(x,y);
mRecyclerview.scrollToPosition();
或者更好:
mRecyclerview.smoothScrollBy( x , y);
mRecyclerview.smoothScrollToPosition(List item position);
因此,您需要获取列表中的Y位置或子位置。唯一有效的方法是滚动定位。这导致了我提到的问题,即它没有位于顶部,因此它看起来不像它的焦点。ScrollTo、smoothScrollBy和smoothScrollToPosition对我来说根本不滚动。这就是为什么我认为我可能有XML设置错误或其他一些设置丢失我尝试创建自己的LinearLayout管理器并覆盖平滑滚动的内容。它现在可以平滑滚动,但不会使项目出现在顶部,仍然会出现偏离中心的问题