如何从XML设置RecyclerView layoutManager ?

    <android.support.v7.widget.RecyclerView
        app:layoutManager="???"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

你可以在文档中查看:

要使用的布局管理器的类名。 该类必须扩展androidx.recyclerview.widget。RecyclerViewView$LayoutManager,并有一个默认的构造函数或签名的构造函数(android.content. contentmanager)。上下文,android.util。AttributeSet, int, int) 如果名称以'开头。’,申请包为前缀。否则,如果名称包含'。'时,类名被假定为完整的类名。否则,回收器视图包(androidx.appcompat.widget)是前缀

使用androidx,您可以使用:

<androidx.recyclerview.widget.RecyclerView
     xmlns:app="http://schemas.android.com/apk/res-auto"
     app:layoutManager="androidx.recyclerview.widget.GridLayoutManager">

通过支持库,您可以使用:

<android.support.v7.widget.RecyclerView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layoutManager="android.support.v7.widget.GridLayoutManager" >

你也可以添加这些属性:

android:orientation = "horizontal|vertical":控制布局管理器的方向(例如:LinearLayoutManager) app:spanCount:设置GridLayoutManager的列数

例子:

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="2"
    ...>

or:

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    android:orientation="vertical"
    ...>

你也可以使用tools命名空间(即tools:orientation和tools:layoutManager)添加它们,然后它只影响IDE预览,你可以继续在代码中设置这些值。


如果你想和LinearLayoutManager一起使用

<android.support.v7.widget.RecyclerView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager" >

这相当于

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);

我来这里寻找androidx版本,虽然它很容易找到,在这里

LinearLayoutManager:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

例子:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

GridLayoutManager:

app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"

例子:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:spanCount="2"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>

正如你在上面的例子中看到的,你可以在xml中使用

android:orientation="vertical"

and

android:orientation="horizontal"

和设置GridLayoutManager的列数

app:spanCount="2"

我最常用的是:

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" 
    tools:listitem="@layout/grid_item"
    android:orientation="vertical" app:spanCount="3"/>

And:

<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/grid_item"
    android:orientation="vertical"/>

建议设置listitem,这样你就能在布局编辑器的预览中看到它的样子。

如果你想颠倒顺序,我认为你必须在代码中做,如果你真的想看到一些东西,就使用XML中的“工具”……


这对我来说很有用-只需添加app:layoutManager="LinearLayoutManager",你就可以开始了

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recordItemList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:clipToPadding="false"
        android:scrollbars="none"
        app:layoutManager="LinearLayoutManager"
        app:stackFromEnd="true"
        app:reverseLayout="true"/>

你可以像这样为recyclerview设置布局管理器,app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"


例子

implementation 'com.android.support:recyclerview-v7:28.0.0'
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
    />

layoutManager可以是android.support.v7.widget。LinearLayoutManager, android.support.v7.widget.GridLayoutManager

[点击此处阅读更多]