【问题标题】:Is there a way to show a preview of a RecyclerView's contents in the Android Studio editor?有没有办法在 Android Studio 编辑器中显示 RecyclerView 内容的预览?
【发布时间】:2015-07-07 22:13:39
【问题描述】:

当我将 RecyclerView 添加到布局时,它显示为空白屏幕。有没有办法,比如通过tools命名空间,显示RecyclerView的内容预览?

【问题讨论】:

    标签: android android-studio android-recyclerview android-tools-namespace


    【解决方案1】:

    从 Android Studio 1.3.1 开始,它会在预览中显示默认列表项,但还不允许您指定自己的列表项。希望它会来。

    【讨论】:

    • 在 AS 1.4 中,您可以从一些预定义的布局中进行选择,例如:tools:listitem="@android:layout/simple_list_item_checked"。只需在布局编辑器中右键单击 RecyclerView 并选择“预览列表内容”。不幸的是,你仍然不能将它用于你自己的布局,至少对我来说它会引发渲染错误。
    【解决方案2】:

    @oRRs 是对的!

    我使用的是 Android Studio 1.4 RC2,您现在可以指定任何自定义布局。

    我尝试了一个自定义 CardView,它可以工作。

    tools:listitem="@android:layout/simple_list_item_checked"
    

    【讨论】:

    • 查看@oRRs 评论:tools:listitem="@android:layout/simple_list_item_checked"
    • 有没有办法在网格模式下预览?
    • @sajad 试试这个应用:layoutManager="GridLayoutManager" tools:listitem="@layout/layout_list_item_select_seat" app:spanCount="5"
    • 如果你也想设置水平方向,你可以:tools:orientation="horizontal"
    • 除了指定tools:orientation="horizontal"android:orientation="horizontal"之外,我还必须按照stackoverflow.com/questions/35681433/…指定app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    【解决方案3】:

    Android 工具和 LayoutManager

    tools 命名空间启用设计时功能(例如在片段中显示哪种布局)或编译时行为(例如将哪种收缩模式应用于您的 XML 资源) 这是一个非常强大的功能,正在开发中,让您不必每次都编译代码来查看更改

    AndroidX[About]GridLayoutManager

    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    
    <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="10"
        tools:orientation="vertical"
        tools:scrollbars="vertical"
        tools:spanCount="3"/>
    

    支持库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"
    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    
        tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
        tools:listitem="@layout/item"
        tools:itemCount="3"
        tools:orientation="horizontal"
        tools:scrollbars="horizontal" />
    

    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_marginRight="15dp"
        android:layout_marginBottom="10dp"
        android:orientation="vertical"
        tools:background="@tools:sample/backgrounds/scenic">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorWhite"
            tools:text="@tools:sample/first_names" />
    
    </FrameLayout>
    

    模拟器结果:

    【讨论】:

    • 这应该被标记为答案,因为它是详细的并且与 RecylerView 兼容。现在根本不使用 ListView。
    • 我必须在属性区域切换到更少的属性选项卡才能看到listitem选项,我只需将它输入到xml代码中!
    • 如果您使用自己的列表项布局并​​且只看到一 (1) 个列表项,请检查布局上的 layout_height="wrap_content"。
    • 有趣的是,它只适用于我使用 ListView 而不是 RecyclerView 的情况。有任何想法吗?我复制粘贴以确保我没有弄乱我的 RecyclerView。但实际上它是有效的,所以它是一个有效的 XML。
    • "@tools:sample/*" 是您可以注入布局的占位符数据的预留 android 资源类型。 last_names - 常见的姓氏。完整的官方文档 - developer.android.com/studio/write/…
    【解决方案4】:

    首先,在您的项目 XML 中添加以下行,以便在您编辑项目时预览您的列表:

    tools:showIn="@layout/activity_my_recyclerview_item"
    

    还有他们,在您的 RecyclerView XML 中添加以下行以预览您的项目在列表中的外观:

    tools:listitem="@layout/adapter_item"
    

    【讨论】:

      【解决方案5】:

      如果您已经有 custom_item 布局:

      <androidx.recyclerview.widget.RecyclerView
                   android:id="@+id/recyclerView"
                   ...
                   ...
                   **tools:listitem="@layout/name_of_your_custom_item_view"**
                   ...>
      </androidx.recyclerview.widget.RecyclerView>
      

      【讨论】:

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