package com.xw.common.viewAdapter import android.annotation.SuppressLint import androidx.annotation.IntDef import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.StaggeredGridLayoutManager import java.lang.annotation.Retention import java.lang.annotation.RetentionPolicy /** * A collection of factories to create RecyclerView LayoutManagers so that you can easily set them * in your layout. */ object LayoutManagers { /** * A [LinearLayoutManager]. */ fun linear(): LineManagerFactory { return object : LineManagerFactory { override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { return LinearLayoutManager(recyclerView.context) } } } /** * A [LinearLayoutManager] with the given orientation and reverseLayout. */ fun linearHorizontal(): LineManagerFactory { return object : LineManagerFactory { override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { return LinearLayoutManager(recyclerView.context, RecyclerView.HORIZONTAL, false) } } } /** * A [GridLayoutManager] with the given spanCount. */ fun grid(spanCount: Int): LineManagerFactory { return object : LineManagerFactory { override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { return GridLayoutManager(recyclerView.context, spanCount) } } } /** * A [GridLayoutManager] with the given spanCount, orientation and reverseLayout. */ fun grid( spanCount: Int, @Orientation orientation: Int, reverseLayout: Boolean ): LineManagerFactory { return object : LineManagerFactory { @SuppressLint("WrongConstant") override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { return GridLayoutManager( recyclerView.context, spanCount, orientation, reverseLayout ) } } } /** * A [StaggeredGridLayoutManager] with the given spanCount and orientation. */ fun staggeredGrid( spanCount: Int, @Orientation orientation: Int ): LineManagerFactory { return object : LineManagerFactory { override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { return StaggeredGridLayoutManager(spanCount, orientation) } } } @IntDef(LinearLayoutManager.HORIZONTAL, LinearLayoutManager.VERTICAL) @Retention(RetentionPolicy.SOURCE) annotation class Orientation }