I just started using ItemDecoration . I am trying to achieve a separator in the middle of two RecyclerView columns managed by StaggeredGridLayoutManager .
Here is my ItemDecoration code:
public class LobbyItemDecoration extends RecyclerView.ItemDecoration { private int margin; private Drawable drawable; public LobbyItemDecoration(int margin, Drawable drawable){ this.margin = margin; this.drawable = drawable; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); int position = parent.getChildAdapterPosition(view); StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams)view .getLayoutParams(); int spanIndex = lp.getSpanIndex(); if(position >= 0){ if (position < ((LobiAdapter)parent.getAdapter()).getmDataset().size()){ if(spanIndex == 1){ outRect.left = margin; ((LobiAdapter)parent.getAdapter()).getmDataset().get(position).left = false; } else { outRect.right = margin; ((LobiAdapter)parent.getAdapter()).getmDataset().get(position).left = true; } outRect.bottom = margin; } } } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { if (drawable == null) { super.onDrawOver(c, parent, state);return; } if(parent.getItemAnimator() != null && parent.getItemAnimator().isRunning()) { return; } final int top = parent.getPaddingTop(); final int bottom = parent.getHeight() - parent.getPaddingBottom(); final int childCount = parent.getChildCount(); for (int i=1; i < childCount; i++) { final View child = parent.getChildAt(i); StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams)child .getLayoutParams(); int spanIndex = lp.getSpanIndex(); int size = drawable.getIntrinsicWidth(); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int ty = (int)(child.getTranslationY() + 0.5f); final int tx = (int)(child.getTranslationX() + 0.5f); final int leftIndex1 = child.getLeft() - (margin * 4 / 3) - tx; final int rightIndex1 = child.getLeft() - (margin * 2 / 3) - tx; final int leftIndex0 = child.getRight() + (margin * 2 / 3) + tx; final int rightIndex0 = child.getRight() + (margin * 4 / 3) + tx; if(spanIndex == 1){
The problem is the name of the name: whenever I load a new element or scroll, the decoration sometimes goes away or is removed. Itβs gone, I mean, itβs literally gone, it is no longer there, until I scroll down or revise the View .
And if I'm wrong, I mean, when I scroll down and the ViewHolder loading is in the left column, the separator "sticks" to the left column. If the ViewHolder loading is in the right column, this is normal.
Just in case: I use ViewHolder as an indicator of progress, adding one null item and checking it in getItemViewType() , then delete it later after the download from the server is complete.
This is how I add:
for (PostModel postModel : dataSet) { this.mDataset.add(postModel); notifyItemInserted(mDataset.size() - 1); }
android
Kevin murvie
source share