如何实现左列靠左、右列靠右的双列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
相关产品推荐
相关产品推荐

