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

问题描述

如何从XML设置 RecyclerView layoutManager ?

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

您可以在文档中检入:

通过 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预览,您可以继续在代码中设置这些值./p>

How to set RecyclerView layoutManager from XML?

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

As you can check in the doc:

With androidx you can use:

<androidx.recyclerview.widget.RecyclerView
     xmlns:app="http://schemas.android.com/apk/res-auto"
     app:layoutManager="androidx.recyclerview.widget.GridLayoutManager">

With the support libraries you can use:

<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 = "horizontal|vertical": to control the orientation of the LayoutManager (eg:LinearLayoutManager)
  • app:spanCount: to set the number of columns for GridLayoutManager

Example:

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

or:

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

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 =&quot;&quot;从XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:40