【问题标题】:Is there a way to show a preview of a RecyclerView's contents as Grid in the Android Studio editor?有没有办法在 Android Studio 编辑器中将 RecyclerView 的内容预览显示为网格?
【发布时间】:2018-01-15 15:32:38
【问题描述】:

当我将 RecyclerView 添加到布局时,它以垂直顺序显示为列表视图。我为此使用tools:listitem。有没有办法让它在 Android Studio 编辑器中显示为网格而不是列表?

【问题讨论】:

    标签: android android-studio android-recyclerview


    【解决方案1】:

    您可以使用xmlns:tools="http://schemas.android.com/tools" 命名空间构建预览。

    AndroidX[About]

    <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="match_parent"
    
        tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        tools:listitem="@layout/item"
        tools:itemCount="7"
        tools:orientation="vertical"
        tools:scrollbars="vertical"
        tools:spanCount="4"/>
    

    支持

    <android.support.v7.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="match_parent"
    
        tools:layoutManager="android.support.v7.widget.GridLayoutManager"
        tools:listitem="@layout/item"
        tools:itemCount="7"
        tools:orientation="vertical"
        tools:scrollbars="vertical"
        tools:spanCount="4"/>
    

    Android studio 3.0你可以通过@tools:sample/* resources预定义一个数据

    item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:layout_marginBottom="15dp"
        android:orientation="vertical"
        tools:background="@tools:sample/avatars">
    
        <TextView
            android:layout_gravity="bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorWhite"
            tools:text="@tools:sample/first_names" />
    
    </FrameLayout>
    

    因此,您的预览将如下所示

    [More examples]

    【讨论】:

    • 如果您使用的是 AndroidX,那么布局管理器现在是 androidx.recyclerview.widget.GridLayoutManager
    【解决方案2】:

    这对我有用(AndroidX): 对于 3 列网格

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvServiceList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        app:spanCount="3"
        tools:itemCount="20"
        tools:listitem="@layout/row_item_service" />
    

    【讨论】:

      【解决方案3】:

      要在预览中水平显示列表,只需使用这两个属性

      tools:orientation="horizontal"
      tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
      

      这是最终代码

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

      【讨论】:

      • 正在寻找网格,这是水平列表。
      • @AnandKumar 看到 alexpfx 的回答,就在我上面,他提到了如何显示网格布局管理器,这里是链接:stackoverflow.com/a/47402709/4859873
      【解决方案4】:

      如果您只想在预览中查看效果而不更改应用行为,您可以像使用 listitem 一样使用“工具”命名空间:

      <android.support.v7.widget.RecyclerView
              android:id="@+id/rcv_collection"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:layoutManager="android.support.v7.widget.GridLayoutManager"
              tools:spanCount="2"
              tools:listitem="@layout/item_collection"/>
      

      【讨论】:

        【解决方案5】:

        使用

        app:layoutManager="GridLayoutManager"
        app:spanCount="3"
        tools:listitem="@layout/table_grid_item"
        

        【讨论】:

          猜你喜欢
          • 2015-07-07
          • 1970-01-01
          • 1970-01-01
          • 2020-12-05
          • 2023-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多