如何设置 RecyclerView app:layoutManager="";来自 XML? [英] How to set RecyclerView app:layoutManager="" from XML?

查看:37
本文介绍了如何设置 RecyclerView app:layoutManager="";来自 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 XML 设置 RecyclerView layoutManager?

How to set RecyclerView layoutManager from XML?

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

推荐答案

您可以查看文档:

要使用的Layout Manager 的类名.

Class name of the Layout Manager to be used.

该类必须扩展 androidx.recyclerview.widget.RecyclerViewView$LayoutManager 并且具有带有签名 (android.content.Context, android.util.AttributeSet,int, int)

The class must extend androidx.recyclerview.widget.RecyclerViewView$LayoutManager and have either a default constructor or constructor with the signature (android.content.Context, android.util.AttributeSet, int, int)

如果名称以 '.' 开头,则应用程序包是前缀.否则,如果名称包含 '.',则假定类名是完整的类名.否则,回收器视图包 (androidx.appcompat.widget) 是前缀

If the name starts with a '.', application package is prefixed. Else, if the name contains a '.', the classname is assumed to be a full class name. Else, the recycler view package (androidx.appcompat.widget) is prefixed

使用 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" >

你也可以添加这些属性:

Also you can add these attributes:

  • android:orientation = "horizo​​ntal|vertical":控制LayoutManager的方向(例如:LinearLayoutManager)
  • app:spanCount:设置GridLayoutManager
  • 的列数
  • android:orientation = "horizontal|vertical": to control the orientation of the LayoutManager (eg:LinearLayoutManager)
  • app:spanCount: to set the number of columns for GridLayoutManager

示例:

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

或:

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

您还可以使用 tools 命名空间(即 tools:orientationtools:layoutManager)添加它们然后它只会影响 IDE 预览,您可以继续在代码中设置这些值.

You can also add them using the tools namespace (i.e. tools:orientation and tools:layoutManager) and then it only impacts the IDE preview and you can continue setting those values in code.

这篇关于如何设置 RecyclerView app:layoutManager="";来自 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆