【问题标题】:Previewing horizontal recyclerview in android studio在android studio中预览水平recyclerview
【发布时间】:2016-06-11 10:08:36
【问题描述】:

我发现了如何使用预览列表项

tools:listitem="@layout/my_item_layout"

然而,Android Studio 将 recyclerview 预览为垂直列表。有没有办法告诉 Android Studio 使用LinearLayoutManager 以水平方式显示布局预览?

【问题讨论】:

    标签: android android-studio android-recyclerview


    【解决方案1】:

    添加一个 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"
        />
    

    【讨论】:

    • 您也可以使用工具命名空间(即tools:orientationtools:layoutManager)添加它们,然后它只会影响 IDE 预览,您可以继续在代码中设置这些值。
    • 在使用 AndroidX 库时将 app:layoutManager="android.support.v7.widget.LinearLayoutManager" 替换为 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" :)
    【解决方案2】:

    如果您使用的是 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 转换为水平但tools:listitem 不适用于androidx.recyclerview:recyclerview:1.1.0-alpha01。如果我不使用tools:listitem,我只能在设计模式下看到水平列表
    • 我发现这是我的错,因为我的列表项布局指定了android:layout_width="match_parent"。通过将其更改为android:layout_width="wrap_content",我无法使用androidx.recyclerview:recyclerview:1.1.0-alpha01 在Android Studio 的设计选项卡中水平滚动。
    【解决方案3】:

    只需在您的布局中使用 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"
        ../>
    

    【讨论】:

    • 这应该是最佳答案。 “工具”只影响预览。 “app:”实际上将您的 RV 连接到布局管理器,这可能不是首选行为
    【解决方案4】:

    接受的答案准确无误。对于 androidx,只需在您的 recyclerView 中使用它

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

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

      • 问题是在 android studio 中预览而不是实际实现。
      猜你喜欢
      • 2020-12-05
      • 2016-01-20
      • 2017-08-29
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多