You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现左列靠左、右列靠右的双列RecyclerView?

双列RecyclerView左列左对齐、右列右对齐实现问题

我需要用GridLayoutManager实现一个包含Checkbox的双列RecyclerView,要求左列所有项紧贴RecyclerView左侧,右列所有项紧贴RecyclerView右侧。

实现效果差异:
实现效果差异

我曾尝试实现一个ItemDecoration,为左列项添加右侧固定间距,为右列项添加左侧固定间距,但该方案不适用于所有内容场景——固定间距无法适配动态内容,达不到预期效果。

现有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
        }
    }
}

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)"/>

求各位提供其他可行的解决方案,谢谢!


内容的提问来源于stack exchange,提问作者Alex Al

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 17:42:03