Android - ItemDecoration sometimes goes away or is inappropriate when adding an item or scrolling

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){ // drawable.setBounds(100, top, 5, bottom); drawable.setBounds(leftIndex1, top, rightIndex1, bottom); drawable.draw(c); } else { drawable.setBounds(leftIndex0, top, rightIndex0, bottom); drawable.draw(c); // drawable.setBounds(5, top, 100, bottom); } } } } 

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); } 
+7
android
source share

No one has answered this question yet.

See related questions:

321
Android: the difference between invisible and gone?
293
How to get the absolute coordinates of the view
2
Android RecyclerView Pagination - How to properly add items to the top of RecyclerView?
2
Android: viewing the pager of fragments containing a scroll; how to reset the scroll view at the top of the screen in OnPause mode
2
How to update basket counter for each list item in android?
2
Scrolling lags after applying font in Recycler view elements
0
Android ListView resets scroll position after first scroll
0
RecyclerView does not automatically scroll to an extended ViewHolder element
0
How to fix view bias outside of RecyclerView while user scrolls through RecyclerView
0
Recyclerview items get reset when scrolling

All Articles