【问题标题】:Adding ItemDecoration to Recycler View Causes Headers to Shrink将 ItemDecoration 添加到 Recycler 视图会导致标题缩小
【发布时间】:2016-10-05 00:31:38
【问题描述】:

我有一个 RecyclerView,它使用 StaggeredGridLayoutManager 来显示按日期划分的项目。对于每个部分(天),我都有一个插入到回收器视图中的标题视图,在我的 OnBindViewHolderAction 方法中,我将标题设置为 FullSpan。我的问题是,当我将 ItemDecoration 添加到 RecyclerView 以提供所有项目间距时,标题视图会被间距覆盖(好像添加了填充而不增加视图大小)而不是添加边距间距。我不确定下一步该往哪里看,任何建议都会有所帮助。谢谢。

无间距项装饰:

带间距项装饰:

应该是这样的:

最后一张图片与 RecyclerView 和 ItemDecoration 相同,但在 GridLayoutManager 而不是 StaggeredGridLayoutManager 上。

public void OnBindViewHolderAction(RecyclerView.ViewHolder holder, int position)
    {
        var item = _collectionAdapter.SectionedList[position];
        if (holder is ActivityFeedItemViewHolder) {
            var viewHolder = (ActivityFeedItemViewHolder)holder;
            viewHolder.Bind((ActivityFeedItemViewModel)item);
            StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams)holder.ItemView.LayoutParameters;
            layoutParams.FullSpan = false;

        }
        if (holder is RecyclerHeaderViewHolder) {
            var viewHolder = (RecyclerHeaderViewHolder)holder;
            viewHolder.Initialize(((RecyclerViewHeaderItem)item).SectionName);
            StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams)holder.ItemView.LayoutParameters;
            layoutParams.FullSpan = true;
        }
    }



public override void GetItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
    {

// STACKOVERFLOW 注意:调用方法时_headerTypeId设置为0

        int pos = parent.GetChildAdapterPosition(view);

        // apply top spacing to first header only if there is one
        if (pos == 0 && _headerTypeId != -1) {
            outRect.Top = 2 * _spacing;
        }

        // these are fixed spacings for all cells
        outRect.Bottom = 2 * _spacing;
        outRect.Left = _spacing;
        outRect.Right = _spacing;

        // only apply spacing to the top row if we don't have headers
        if (_headerTypeId == -1) {

            // adjust the position index to account for headers
            for (int i = 0; i < pos; i++) {
                if (parent.GetAdapter().GetItemViewType(i) == _headerTypeId) {
                    pos--;
                }
            }

            // apply top spacing to only the top row of cells
            if (pos < _layoutManager.SpanCount) {
                outRect.Top = 2 * _spacing;
            }
        }
    }

【问题讨论】:

  • 页眉布局的高度是否设置为 wrap_content?
  • 这是@Eugen Pechanec 的问题。谢谢。

标签: android xamarin android-recyclerview


【解决方案1】:

所以我可以通过简单地设置标题视图的高度来包装内容而不是将其设置为 axml 中的值(在我的情况下为 32dp)来解决此问题。感谢 Eugen Pechanec。

【讨论】:

  • 尝试添加android:minHeight="32dp" 以产生所需的结果。
【解决方案2】:

改变

outRect.Top = 2 * _spacing;

outRect.Top =_spacing;

如果这不起作用,只需评论该行并尝试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多