Reputation: 1090
I am having RecyclerView
with Grid layout
. Everything works well.
But I need to set column at center horizontal. For example. I am having 18 items and span count is 4. For the last row, I have 2 item. these two items should be at the center horizontal instead of left.
This is my RecyclerView
:
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:layout_gravity="center_horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rv_answer" />
This is my adapter layout
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_marginStart="@dimen/marginVSmall"
android:layout_marginEnd="@dimen/marginVSmall"
android:layout_marginTop="@dimen/marginVSmall"
android:layout_marginBottom="@dimen/marginVSmall"
android:layout_height="wrap_content">
<curvegraph.com.vijay.widgets.SquareTextView
android:id="@+id/tv_option"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/circle"
android:gravity="center"
android:text="A"
android:textStyle="bold"
android:maxLength="1"
android:textColor="@color/colorWhite"
android:textSize="@dimen/textXLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
</android.support.constraint.ConstraintLayout>
My Kotlin code to set LayoutManager
rv_option.layoutManager = GridLayoutManager(this, 4)
Here is my Kotlin Code :
val grid = GridLayoutManager(this, 8)
grid.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (position > 15) { // totalRowCount : How many item you want to show
2 // the item in position now takes up 4 spans
} else 1
}
}
rv_option.layoutManager=grid
rv_option.setHasFixedSize(true)
optionsAdapter.spanCountFun(8)
Upvotes: 2
Views: 4058
Reputation: 7669
Try this Hope this will help.
val gridLayoutManager = GridLayoutManager(this, 4)
gridLayoutManager.setSpanSizeLookup(object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (position == 16) { // totalRowCount : How many item you want to show
4 // the item in position now takes up 4 spans
} else 1
}
})
recyclerView.layoutManager = gridLayoutManager
recyclerView.setHasFixedSize(true)
Already have an answer of my own: https://stackoverflow.com/a/50247597/5167909
Upvotes: 9
Reputation: 3711
it's impossible, because item of grid layout is fixed. U can using another trick, like using temp item for your list, but its can not help u if last row have one or three items
Upvotes: 1