Reputation: 111
I want to implement a RecyclerView
using a GridLayoutManager
with 2 columns which will contain Checkbox
es. I want all the items of the first column to be 'attached' to the left side of the RecyclerView and the items of the right column to be attached on the right side of the RecyclerView.
Here is a diagram of the expected and the actual results:
I have implemented an ItemDecoration
with adds a fixed value spacing on the right if the item is in the left column and on the left if the item is in the right column, but this does not work for every possible content of the cells because sometimes the fixed value of the spacing does not result in the expected result and needs to be dynamic.
This is the ItemDecoration:
class StationListItemDecoration(val spacing: Int) : ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val position = parent.getChildAdapterPosition(view)
val column = position % 2
if (column == 0) {
// Left column item, adjust to the start
outRect.left = 0
outRect.right = spacing
} else {
// Right column item, adjust to the end
outRect.left = spacing
outRect.right = 0
}
}
}
This is the layout of the content of each item in the RecyclerView
.
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkbox"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="1245 (4)"/>
I do not know what else to try, so any help would be grateful. Thanks !
Upvotes: 0
Views: 43