LinearLayoutManager布局实现单行混排:
LinearLayout横排均分多个重复元素,数据1次包含多个。
|A-A-A|
|---B---|
|A-A-A|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<View
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_margin="@dimen/dp_6"
android:background="#E2D849"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<View
android:id="@+id/icon2"
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_margin="@dimen/dp_6"
android:background="#E2D849"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
GridLayoutManager设置参数实现混排:
设置setSpanSizeLookup,需要几个位置返回几个。
SpanSizeLookup方法getSpanSize参数position:当前数据列表的index,返回int是需要占用的位置个数,即SpanSize。通过position获取数据类型,类型判断需要的SpanSize。
GridLayoutManager glm = new GridLayoutManager(this, SPAN_COUNT);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
MixedRowBean mrb = rightAdapter.getDataProvider().getData(position);
return mrb != null && mrb.getViewType() == MixedRowBean.TYPE_MULTI ? SPAN_COUNT : 1;
}
});