【发布时间】: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