これおもしろい

らふにかいてこ

RecyclerViewで最後の行を中央揃えしたい

こういうのやりたい時

結論: FlexboxLayoutManagerを適用する

binding.recyclerView.layoutManager = FlexboxLayoutManager(requireContext()).also {
    it.flexDirection = FlexDirection.ROW
    it.justifyContent = JustifyContent.CENTER
}

github.com

行内の最大数の指定は出来ないから、幅計算してRecyclerViewの幅指定とかで実装するしかないのかな?

RecyclerView + Flexbox / Flow

アイテムの数が少ないのが明確なら

ConstraintLayout Flowで良さそう

<androidx.constraintlayout.helper.widget.Flow
    ...
    app:flow_wrapMode="chain"
    app:flow_horizontalStyle="packed"
    app:flow_maxElementsWrap="5"/>

アイテムの数が固定でないなら、programmaticallyにView作ってFlowのreferenceIdsに設定する


上2つを試したコードのPR
RecyclerView Multiline Centering by takattata · Pull Request #1 · takattata/TasksForStudy · GitHub (もしコードご覧になって、この書き方すると危ないのではなどお気づきの点あったら教えてください^^)

flexbox-layout初めて使ったなぁ