android - 如何使用 StaggeredGridLayoutManager 确定 RecyclerView 中的 viewholder 是向左还是向右

标签 android staggered-gridview staggeredgridlayout staggeredgridlayoutmanager

我读过很多类似的文章,但仍然没有找到如何使用 StaggeredGridLayoutManager 知道 RecyclerView 中 View 持有者是左侧还是右侧的答案。

情况: 我有 RecyclerView、StaggeredGrid 并想要进行填充

8 dp [left view] 8 dp [right view] 8 dp

因此,由于我无法在 XML 中执行此操作,因此我必须添加一些边距 -

对于左 View :左边距 8dp,右边距 4dp

右 View :左边距 4dp,右边距 8dp

通常 View 的放置方式如下:

[0][1]
[2][3]
[4][5]

最简单的解决方案是尝试按位置确定它:

override fun onBindViewHolder(ViewHolder holder, int position) {
    ...
        val params = holder.cardView.layoutParams as FrameLayout.LayoutParams

        if (position % 2 == 0) {
            params.leftMargin = pxFromDp(context, 8f).toInt()
            params.rightMargin = pxFromDp(context, 4f).toInt()
        }
        if (position % 2 == 1) {
            params.rightMargin = pxFromDp(context, 8f).toInt()
            params.leftMargin = pxFromDp(context, 4f).toInt()
        }
        params.bottomMargin = pxFromDp(context, 2f).toInt()
        params.topMargin = pxFromDp(context, 6f).toInt()

        holder.cardView.layoutParams = params
    ...
}

这可行,但如果 View 2 的高度小于 View 1,它们就会被放置

[0][1]
[3][2]
[5][4]

所以它不起作用。

我如何知道是否有左或右观看者?

最佳答案

唯一真正有帮助的选项是使用自定义RecyclerView.ItemDecoration()

class StaggeredGridDecoration(val margin: Int) : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State?) {
        super.getItemOffsets(outRect, view, parent, state)
        val position = parent.getChildAdapterPosition(view)
        val spanIndex = (view.layoutParams as StaggeredGridLayoutManager.LayoutParams).spanIndex
        val type = parent.adapter.getItemViewType(position)
        val halfOfMargin = margin / 2
        when (type) {
            1 -> {//use here 0 if you didnt create custom types
                var top = 0
                val bottom = pxFromDp(parent.context, margin.toFloat()).toInt()
                //for first 2 elements I need additional margin top
                if (position < 3) {
                    top = pxFromDp(parent.context, margin.toFloat()).toInt()
                }
                if (spanIndex == 0) {
                    //settings for left column
                    val left = pxFromDp(parent.context, margin.toFloat()).toInt()
                    val right = pxFromDp(parent.context, halfOfMargin.toFloat()).toInt()
                    setMargins(view, left, right, top, bottom)
                } else {
                    //settings for right column
                    val left = pxFromDp(parent.context, halfOfMargin.toFloat()).toInt()
                    val right = pxFromDp(parent.context, margin.toFloat()).toInt()
                    setMargins(view, left, right, top, bottom)
                }
            }
        }
    }

    private fun setMargins(view: View, left: Int, right: Int, top: Int, bottom: Int) {
        val cardView: CardView = view.findViewById(R.id.cardView)
        val params = cardView.layoutParams as FrameLayout.LayoutParams

        params.rightMargin = right
        params.leftMargin = left
        params.bottomMargin = bottom
        params.topMargin = top

        cardView.layoutParams = params
    }

}

刚刚添加

recyclerView.addItemDecoration(new StaggeredGridDecoration(8));

关于android - 如何使用 StaggeredGridLayoutManager 确定 RecyclerView 中的 viewholder 是向左还是向右,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50176839/

相关文章:

java - 通用类输入参数类型不匹配

java - 如何将数组中的项目放入不同的复选框中?

java - 交错网格布局管理器 : Adding Margin makes the layout items pushed to the side

android - 如何从 StaggeredGridLayoutManager 中找到单元格宽度?

android - 可在两个方向上工作的交错网格布局管理器

Android 应用程序通知每 15 分钟一次

Android - StaggeredGrid 错误项目处置

android - 滚动在交错的 Gridview 中产生间隙

android - StaggaredGridLayoutManager 无法正常工作

android - 一个按钮有阴影,两个没有 : I cannot tell why