【问题标题】:Android RecyclerView scroll and position item at topAndroid RecyclerView 在顶部滚动和定位项目
【发布时间】:2017-06-13 05:24:03
【问题描述】:

我不确定 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>

感谢任何帮助和指导。

【问题讨论】:

    标签: java android xml list android-recyclerview


    【解决方案1】:

    你应该使用

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

    甚至更好:

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

    所以你需要获取Y位置,或者列表中的子位置。

    【讨论】:

    • 唯一有效的是scrollToPosition。这导致了我提到的问题,它没有位于顶部,所以它看起来并不像它的焦点。 ScrollTo、smoothScrollBy 和 smoothScrollToPosition 对我来说根本不滚动。这就是为什么我认为我可能有 XML 设置错误或缺少其他一些设置
    • 我尝试创建自己的 LinearLayout 管理器并覆盖平滑滚动的内容。它现在平滑滚动,但它不会使项目出现在顶部,仍然是它出现偏离中心的问题
    猜你喜欢
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 2020-05-17
    相关资源
    最近更新 更多