在Android Studio中预览水平RecyclerView

83

我发现可以通过使用以下方法预览列表项:

tools:listitem="@layout/my_item_layout"

Android Studio预览RecyclerView为垂直列表。是否有一种方法可以告诉Android Studio使用LinearLayoutManager以水平方式显示布局预览?

7个回答

158

添加一个LayoutManager并设置为水平方向。

这里是一个例子:

<android.support.v7.widget.RecyclerView
    android:id="@+id/homesRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    android:layout_centerVertical="true"
    />

31
你还可以使用工具命名空间(即tools:orientationtools:layoutManager)来添加它们,这样只会影响IDE预览,你仍然可以在代码中设置这些值。 - gMale
6
在使用AndroidX库时,将app:layoutManager="android.support.v7.widget.LinearLayoutManager"替换为app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" :) - stkent

29

如果您正在使用AndroidX库:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/homesRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    android:layout_centerVertical="true"
    />

这个被RecyclerView转换成了水平布局,但是在使用androidx.recyclerview:recyclerview:1.1.0-alpha01时,tools:listitem对我没有起作用。如果我不使用tools:listitem,我只能在设计模式下看到水平列表。 - Michael Osofsky
1
我发现这是我的问题,因为我的列表项布局指定了 android:layout_width="match_parent"。将其更改为 android:layout_width="wrap_content" 后,我可以在 Android Studio 的设计选项卡中使用 androidx.recyclerview:recyclerview:1.1.0-alpha01 进行水平滚动。 - Michael Osofsky

13

只需在布局中使用app:layoutManagerandroid:orientation属性,并且还要使用tools名称空间添加它们。
类似于以下内容:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:orientation="horizontal"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/ly_item"
    tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:orientation="horizontal"
    ../>

4
这应该是最佳答案。"tools" 只影响预览。"app:" 实际上将您的 RV 连接到布局管理器,这可能不是首选行为。 - PatchesDK
嗨Gabriele,你能帮我解决这个问题吗?-> https://stackoverflow.com/questions/76786965/facing-issue-while-validating-amongst-the-edittexts-in-recyclerview - android dev

7

被接受的答案非常准确。对于androidx,只需在您的recyclerView中使用以下内容:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" android:orientation="horizontal"


0

AndroidX

<androidx.recyclerview.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
    tools:listitem="@layout/item"
    tools:orientation="horizontal"/>

[在这里阅读更多]


0
要在Android中显示RecyclerView的预览,请按照以下步骤进行:
  • 在父布局小部件中添加此行:
xmlns:tools="http://schemas.android.com/tools"

现在在RecyclerView中添加一个属性:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal"

例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerQueueItems"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:itemCount="10"
        tools:listitem="@layout/item_queue_token_number" 
    />

</LinearLayout>

-6

否则你可以在Java文件中使用:

mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));

3
问题是在Android Studio中预览而不是实际实现。 - Vidhi Dave

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接