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