我有一個使用StaggeredGridLayoutManager以顯示按日期分段項RecyclerView。對於每個部分(日期),我有一個插入到回收站視圖中的標題視圖,在我的OnBindViewHolderAction方法中,我將標題設置爲FullSpan。我的問題是,當我將我的ItemDecoration添加到RecyclerView以提供所有項目間距時,標題視圖會被間距覆蓋(就像添加了填充而不增加視圖尺寸一樣),而不是添加邊距。我不確定接下來要看哪裏,任何建議都會有所幫助。謝謝。添加ItemDecoration到回收站查看導致頭縮水
這最後的畫面是一樣的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)
{
//計算器注:_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的問題。謝謝。 – Sevren