【问题标题】:How to set RecyclerView app:layoutManager="" from XML?如何从 XML 设置 RecyclerView app:layoutManager=""?
【发布时间】:2016-06-11 07:47:45
【问题描述】:

如何从 XML 中设置RecyclerView layoutManager?

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

【问题讨论】:

  • @dieter_h 你能用 GridLayoutManager 的例子来回答吗?
  • 您可以使用app:layoutManager="android.support.v7.widget.GridLayoutManager"。将使用具有四个参数的构造函数(ContextAttributeSetintint)。根据文档,这是通过 RecyclerView 属性 layoutManager 在 XML 中设置布局管理器时使用的 构造函数。如果 XML 中未指定 spanCount,则默认为单列

标签: android xml android-recyclerview androidx


【解决方案1】:

你可以像这样为recyclerview设置布局管理器app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

【讨论】:

    【解决方案2】:

    这对我有用 - 只需添加 app:layoutManager="LinearLayoutManager" 就可以了

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recordItemList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:clipToPadding="false"
            android:scrollbars="none"
            app:layoutManager="LinearLayoutManager"
            app:stackFromEnd="true"
            app:reverseLayout="true"/>
    

    【讨论】:

      【解决方案3】:

      您可以查看文档:

      要使用的 Layout Manager 的类名。

      该类必须扩展 androidx.recyclerview.widget.RecyclerViewView$LayoutManager 并具有默认构造函数或带有签名 (android.content.Context, android.util.AttributeSet, int, int) 的构造函数

      如果名称以'.' 开头,则应用程序包为前缀。否则,如果名称包含'.',则假定类名是完整的类名。否则,recycler view 包(androidx.appcompat.widget)是前缀

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

      您还可以添加以下属性:

      • android:orientation = "horizontal|vertical":控制LayoutManager的方向(例如:LinearLayoutManager
      • app:spanCount:设置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 预览,您可以继续在代码中设置这些值。

      【讨论】:

      • 您可以在 RecyclerView 元素上使用android:orientation 来控制例如线性布局管理器
      • @Gabriele Mariotti 我检查了实现,它使用反射。从性能方面来说可以吗?
      • 如果我使用 GridLayoutManager 是否有更多可用属性?是否可以设置其列数(spanCount)?
      • @androiddeveloper 是的,例如将其设置为三列使用 app:spanCount="3"
      • 您也可以使用tools:layoutManager, tools:spanCount(即使自动完成功能没有显示)并且如果您不想覆盖任何 recyclerview 设置(或者更喜欢以编程方式)
      【解决方案4】:

      我来这里是为了寻找androidx 版本,虽然很容易弄清楚,但在这里

      LinearLayoutManager:

      app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
      

      示例:

      <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/recyclerView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
      

      GridLayoutManager:

      app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
      

      示例:

      <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/recyclerView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          app:spanCount="2"
          app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>
      

      正如您在上面的示例中看到的,您可以使用

      xml 中控制方向
      android:orientation="vertical"
      

      android:orientation="horizontal"
      

      并使用

      设置GridLayoutManager的列数
      app:spanCount="2"
      

      【讨论】:

      • 你可以设置no。通过添加app:spanCount="2" 使用GridLayoutManager 时的列数
      • 未来兼容的方法。
      • 这里也一样... Androidx 即将起飞?
      • 谢谢我在找spanCount在这里找到了。这就是为什么赞成答案:)
      【解决方案5】:

      我最常用的是:

      <androidx.recyclerview.widget.RecyclerView
          app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" 
          tools:listitem="@layout/grid_item"
          android:orientation="vertical" app:spanCount="3"/>
      

      还有:

      <androidx.recyclerview.widget.RecyclerView
          app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
          tools:listitem="@layout/grid_item"
          android:orientation="vertical"/>
      

      建议设置 listitem ,以便您在布局编辑器的预览中看到它的外观。

      如果您想颠倒顺序,我认为您必须改为在代码中执行,如果您真的想查看某些内容,请使用 XML 中的“工具”...

      【讨论】:

      • 你在上面的xml中使用的appsapp命名空间是什么?因为我收到构建时间错误Android resource linking failed - AAPT: error: attribute orientation,如果我使用app:orientation
      • 谢谢,但它仍然是错误的命名空间。对于orientation,它应该是android。它不适用于 app 命名空间,其他一切都很完美。
      • @Omkar 再次正确。对不起。已更新。
      【解决方案6】:

      如果你想将它与LinearLayoutManager一起使用

      <android.support.v7.widget.RecyclerView
          xmlns:app="http://schemas.android.com/apk/res-auto"
          app:layoutManager="android.support.v7.widget.LinearLayoutManager" >
      

      相当于

      LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
      mRecyclerView.setLayoutManager(mLayoutManager);
      

      【讨论】:

      • 您对 Gabriele Mariotti 的回答有何补充?
      • 用另一个示例 LinearLayout 解释代码中的等价物
      • @UdiOshi 要使其水平,您需要将android:orientation="horizontal" 添加到 RecyclerView xml
      • androidx.recyclerview.widget.LinearLayoutManager for androidX
      猜你喜欢
      • 2021-06-22
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      相关资源
      最近更新 更多