2016-10-05 71 views
1

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

沒有間距項目裝修: 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) 
    { 

//計算器注:_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; 
      } 
     } 
    } 
+1

頁眉佈局是否將其高度設置爲wrap_content? –

+0

這是@Eugen Pechanec的問題。謝謝。 – Sevren

回答

0

變化

outRect.Top = 2 * _spacing; 

outRect.Top =_spacing; 

如果不工作只是評論該行和嘗試。

1

所以我能夠通過簡單地設置首標視圖的高度來包裝內容,而不是具有其在axml設置爲一個值(32dp在我的情況),以解決這個問題。感謝Eugen Pechanec。

+1

嘗試添加'android:minHeight =「32dp」'來產生所需的結果。 –