Android Horizo​​ntal RecyclerView滚动方向

Ham*_*rzi 40 android horizontal-scrolling android-recyclerview

我做了一个Horizo​​ntal RecyclerView,它运行正常(感谢这一点),但滚动和数据的方向从左向右扩展; 那么如何更改RecyclerView滚动方向,如下图所示?

在此输入图像描述

我的代码:

StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(
                        2, //The number of Columns in the grid
                        LinearLayoutManager.HORIZONTAL);
Run Code Online (Sandbox Code Playgroud)

gme*_*tal 109

假设您LinearLayoutManager在RecyclerView中使用,那么您可以trueLinearLayoutManager构造函数中传递第三个参数.

例如:

mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
Run Code Online (Sandbox Code Playgroud)

如果您正在使用StaggeredGridLayoutManager,那么您可以使用setReverseLayout它提供的方法.


Luc*_*llo 28

你可以用xml来做.

应用程序:reverseLayout ="true"完成工作!

<android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:divider="@null"
                        android:orientation="horizontal"
                        app:reverseLayout="true"
                        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
Run Code Online (Sandbox Code Playgroud)


小智 5

使用androidx的XML方法:

<androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/my_recycler_view"
        android:orientation="horizontal"
        tools:listitem="@layout/my_item"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" 
        android:layout_height="wrap_content">
Run Code Online (Sandbox Code Playgroud)


小智 5

要更改滑动方向,您可以使用

反向布局属性 = true。

在科特林中,

val layoutManager = LinearLayoutManager(this@MainActivity,LinearLayoutManager.HORIZONTAL,true)
recyclerview.layoutManager = layoutManager
Run Code Online (Sandbox Code Playgroud)

在爪哇,

 LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true);
 recyclerview.setLayoutManager(layoutManager);
Run Code Online (Sandbox Code Playgroud)

实际上它颠倒了布局。

如果显示如下

1.2..3....10

它将更改为

10.9..8....1

创建 Horizo​​ntal RecyclerView 有很多方法。

在 Android 中创建水平 RecyclerView 的 4 种方法