android - 将 ItemDecoration 添加到 Recycler View 会导致标题缩小

标签 android xamarin android-recyclerview

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

无间距项目装饰: enter image description here

带间距项装饰: enter image description here

应该是这样的: enter image description here

最后一张图片与 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;
            }
        }
    }

最佳答案

所以我能够通过简单地设置标题 View 的高度来包装内容而不是将其设置为 axml 中的值(在我的例子中是 32dp)来解决这个问题。感谢 Eugen Pechanec。

关于android - 将 ItemDecoration 添加到 Recycler View 会导致标题缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39863719/

相关文章:

android - 将基于 Material 的对话框主题与 AppCompat 一起使用

ios - Xamarin 在 ios 上形成 zxing ZXingScannerView

java - 如何在 recyclerview 中左右两个方向进行无限滚动?

java - Android.support.v4 导入不工作

Android:通过 MTP 连接时如何检测 MediaStore 的变化

c# - Visual Studio (xamarin) 在执行此操作时卡在后台运行的 AAPT.exe 的每个构建、清理或重建上

Xamarin.Forms Prism 从导航堆栈中删除 View

java - 在 RecyclerView 中滚动后 EditText 丢失内容

java - 我如何删除 RecyclerView 中 CardView 中的额外边距

Android::java.security.KeyStoreException:无法存储私钥