Created
December 4, 2019 12:21
-
-
Save StanGenchev/772abe51df08d74eae43c2918e13aad9 to your computer and use it in GitHub Desktop.
AutoFitGrid/AutoFitStaggerdGrid is a RecyclerView with a Grid/StaggeredGridLayoutManager which automatically sets the number of columns based on the column width.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<com.package.AutoFitGrid xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/grid_id" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:columnWidth="300dp" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<com.package.AutoFitStaggeredGrid xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/grid_id" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:columnWidth="300dp" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AutoFitGrid : RecyclerView { | |
private var manager: GridLayoutManager? = null | |
private var columnWidth = -1 | |
constructor(context: Context) : super(context) { | |
initialization(context, null) | |
} | |
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { | |
initialization(context, attrs) | |
} | |
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super( | |
context, | |
attrs, | |
defStyle | |
) { | |
initialization(context, attrs) | |
} | |
private fun initialization(context: Context, attrs: AttributeSet?) { | |
if (attrs != null) { | |
// List the attributes we want to fetch | |
val attrsArray = intArrayOf(android.R.attr.columnWidth) | |
val array = context.obtainStyledAttributes(attrs, attrsArray) | |
// Retrieve the value of the 0 index, which is columnWidth | |
columnWidth = array.getDimensionPixelSize(0, -1) | |
array.recycle() | |
} | |
manager = GridLayoutManager(context, 1) | |
layoutManager = manager | |
} | |
override fun onMeasure(widthSpec: Int, heightSpec: Int) { | |
super.onMeasure(widthSpec, heightSpec) | |
if (columnWidth > 0) { | |
// The spanCount should always be at least 1 | |
val spanCount = max(1, measuredWidth / columnWidth) | |
manager!!.spanCount = spanCount | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AutoFitStaggeredGrid : RecyclerView { | |
private var manager: StaggeredGridLayoutManager? = null | |
private var columnWidth = -1 | |
constructor(context: Context) : super(context) { | |
initialization(context, null) | |
} | |
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { | |
initialization(context, attrs) | |
} | |
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super( | |
context, | |
attrs, | |
defStyle | |
) { | |
initialization(context, attrs) | |
} | |
private fun initialization(context: Context, attrs: AttributeSet?) { | |
if (attrs != null) { | |
// List the attributes we want to fetch | |
val attrsArray = intArrayOf(android.R.attr.columnWidth) | |
val array = context.obtainStyledAttributes(attrs, attrsArray) | |
// Retrieve the value of the 0 index, which is columnWidth | |
columnWidth = array.getDimensionPixelSize(0, -1) | |
array.recycle() | |
} | |
manager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) | |
layoutManager = manager | |
} | |
override fun onMeasure(widthSpec: Int, heightSpec: Int) { | |
super.onMeasure(widthSpec, heightSpec) | |
if (columnWidth > 0) { | |
// The spanCount should always be at least 1 | |
val spanCount = max(1, measuredWidth / columnWidth) | |
manager!!.spanCount = spanCount | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment