具有不同单元格宽度的RecyclerView Grid的LayoutManager

时间:2016-04-09 09:14:57

标签: android gridview android-recyclerview

StaggeredGridLayoutManager似乎不允许自定义单元格宽度或跨越多列(垂直方向除外)以进行垂直方向。

enter image description here

如上所示,组织单元格的首选LayoutManager是什么?

P.S。我只是想知道如何自定义单元格宽度而不是StaggeredGridLayoutManager的高度。我知道高度可以按照sample中的实现进行自定义。

public class VerticalStaggeredGridFragment extends RecyclerFragment {

    public static VerticalStaggeredGridFragment newInstance() {
        VerticalStaggeredGridFragment fragment = new VerticalStaggeredGridFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return new InsetDecoration(getActivity());
    }

    @Override
    protected int getDefaultItemCount() {
        return 100;
    }

    @Override
    protected SimpleAdapter getAdapter() {
        return new SimpleStaggeredAdapter();
    }
}

适配器

public class SimpleStaggeredAdapter extends SimpleAdapter {
    @Override
    public void onBindViewHolder(VerticalItemHolder itemHolder, int position) {
        super.onBindViewHolder(itemHolder, position);

        final View itemView = itemHolder.itemView;
        if (position % 4 == 0) {
            int height = itemView.getContext().getResources()
                    .getDimensionPixelSize(R.dimen.card_staggered_height);
            itemView.setMinimumHeight(height);
        } else {
            itemView.setMinimumHeight(0);
        }
    }
}

itemView.setMinimumWidth(customWidth)不会影响单元格宽度。

为简化起见,我将网格布局更新为

enter image description here

StaggeredGridLayoutManager或任何其他布局管理器中的单元格1相比,如何增加单元格0宽度?

2 个答案:

答案 0 :(得分:7)

对于其他人而言,在不简化布局的情况下寻找解决方案,您可以查看my answer here,或者您可以阅读更多内容。

非常感谢Nick Butcher!他的布局经理Sub Export() Dim lastRowcheck As Long, n1 As Long Dim rCopy As Range With Worksheets("Sheet1") lastRowcheck = Application.Max(.Range("B" & .Rows.Count).End(xlUp).Row, _ .Range("C" & .Rows.Count).End(xlUp).Row) For n1 = lastRowcheck To 1 Step -1 If Application.CountIfs(.Columns("B"), .Cells(n1, "B").Value2, .Columns("C"), .Cells(n1, "C").Value2) > 1 Then Debug.Print .Cells(n1, "A") & ":" & .Cells(n1, "B") & ":" & .Cells(n1, "C") '''export to new sheet If rCopy Is Nothing Then Set rCopy = .Rows(n1) Else Set rCopy = Union(rCopy, .Rows(n1)) End If Next n1 End With With Sheets("Sheet2") 'For using a sheet that already exists 'With Sheets.Add(After:=Sheets(Sheets.Count)) 'For creating a brand new sheet to use If Not rCopy Is Nothing Then rCopy.EntireRow.Copy _ Destination:=.Cells(.Rows.Count, "A").End(xlUp).Offset(1) End With End Sub 能够做到这一点。

  1. 下载SpannableGridLayoutManager from here

    出于某种原因,我不得不改变这一行:

    SpannableGridLayoutManager

    while (availableSpace > 0 && lastVisiblePosition < lastItemPosition) {
    

    让经理工作。

  2. 将SpannableGridLayoutManger设置为您的RecyclerView

  3. 在你的情况下:

    while (lastVisiblePosition < lastItemPosition) {
    

    这将准确地说明问题的第一张图片中的内容。

    enter image description here

答案 1 :(得分:3)

最终做到了,它将为所有尝试实现任何模式的人提供帮助

我一直在寻找相同的结果,但是有一个页眉,并且延迟加载了页脚。 我也尝试了 Nick Butcher Library @Kristyna 答案。但是它有很多问题,例如extra space or fixed cell height 快速滚动视图消失问题

我找到了Arasthel SpannedGridLayoutManager的另一个库,并尝试实现它。但是它也有很多问题,例如固定高度页脚多余空间问题

最后,在挖掘Arasthel的SpannedGridLayoutManager 5小时后,我解决了问题并找到了结果。

我最终编辑的分叉库下载从此处SpannedGridLayoutManager

enter image description here enter image description here

就我而言

  val adapter = GridItemAdapter()//This is your adapter
  val spannedGridLayoutManager = SpannedGridLayoutManager(orientation = VERTICAL, spans = 3)
  spannedGridLayoutManager.itemOrderIsStable = true
  recyclerview.layoutManager = spannedGridLayoutManager

对于第一张图像结果

  spannedGridLayoutManager.spanSizeLookup = SpannedGridLayoutManager.SpanSizeLookup { position ->
        when {
            position == 0 -> {
                /**
                 * 150f is now static 
                 * should calculate programmatically in runtime
                 * for to manage header hight for different resolution devices
                 */
                SpanSize(3, 1, 150f)
            }
            position % 7 == 1 ->
                SpanSize(2, 2)
            else ->
                SpanSize(1, 1)
        }
    }
  recyclerview.adapter = adapter

第二张图像结果

 spannedGridLayoutManager.spanSizeLookup = SpannedGridLayoutManager.SpanSizeLookup { position ->
        when (position % 8) {
            0, 5 ->
                SpanSize(2, 2)
            3, 7 ->
                SpanSize(3, 2)
            else ->
                SpanSize(1, 1)
        }
    }
    recyclerview.adapter = adapter

spanSizeLookup 逻辑可能会根据要求而有所不同,对我来说,我出于测试目的已对其进行了随机设置。