XMLからRecyclerView layoutManagerを設定するには?
<android.support.v7.widget.RecyclerView
app:layoutManager="???"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
XMLからRecyclerView layoutManagerを設定するには?
<android.support.v7.widget.RecyclerView
app:layoutManager="???"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
ドキュメントで確認できるように:
使用する のクラス名
Layout Manager
。クラスは拡張
androidx.recyclerview.widget.RecyclerViewView$LayoutManager
し、デフォルトのコンストラクターまたは署名付きのコンストラクターのいずれかを持つ必要があります(android.content.Context, android.util.AttributeSet, int, int)
名前が で始まる場合
'.'
、アプリケーション パッケージがプレフィックスとして付けられます。それ以外の場合、名前に が含まれている場合、'.'
クラス名は完全なクラス名と見なされます。それ以外の場合は、リサイクラー ビュー パッケージ (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:orientation
と)を使用してそれらを追加することもできますtools:layoutManager
。これは IDE プレビューにのみ影響し、コードでこれらの値を引き続き設定できます。
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"/>