FlexboxLayout

4061 ワード

今日はFlexboxLayoutについて
RecyclerViewとの併用方法を簡単にご紹介したいと思います.
Installation
dependencies {
    implementation 'com.google.android.flexbox:flexbox:3.0.0'
}
Xml
<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_category"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="24dp"
        android:overScrollMode="never"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
Code
val flexBoxLayoutManager = FlexboxLayoutManager(this)
flexBoxLayoutManager.flexWrap = FlexWrap.WRAP
binding.rvCategory.apply {
    layoutManager = flexBoxLayoutManager
    adapter = flexBoxLayoutAdapter
}
layoutManager.flexWrap = FlexWrap.WRAP
layoutManager.setFlexDirection(FlexDirection.COLUMN)
layoutManager.setJustifyContent(JustifyContent.FLEX_END)
リファレンス
https://github.com/google/flexbox-layout
https://android-developers.googleblog.com/2017/02/build-flexible-layouts-with.html