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;
}
});
2种方法效果:
关注公众号:Android老皮
解锁 《Android十大板块文档》 ,让学习更贴近未来实战。已形成PDF版
内容如下:
1.Android车载应用开发系统学习指南(附项目实战)
2.Android Framework学习指南,助力成为系统级开发高手
3.2023最新Android中高级面试题汇总+解析,告别零offer
4.企业级Android音视频开发学习路线+项目实战(附源码)
5.Android Jetpack从入门到精通,构建高质量UI界面
6.Flutter技术解析与实战,跨平台首要之选
7.Kotlin从入门到实战,全方面提升架构基础
8.高级Android插件化与组件化(含实战教程和源码)
9.Android 性能优化实战+360°全方面性能调优
10.Android零基础入门到精通,高手进阶之路
敲代码不易,关注一下吧。ღ( ´・ᴗ・` ) 🤔