I use RecyclerView with custom ItemDecoration . Moreover, the separators between the cells are different, depending on the types.

When scrolling through the list, everything is OK, but problems begin when using notifyItem *** . In this case, the distance between the cells that are visible on the screen and do not participate in the update, change. After the changes are over, when you scroll again, everything is fine.

Has anyone come across this?

recyclerView.addItemDecoration( new RecyclerView.ItemDecoration() { @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { int position = linearLayoutManager.getPosition(view); int type = linearLayoutManager.getItemViewType(view); outRect.bottom = getSize( type, messageManager.getType(position + 1) ); outRect.left = 0; outRect.right = 0; outRect.top = getSize( messageManager.getType(position - 1), type ); } private int getSize(int type1, int type2) { if ((type1 & MessageManager.FLAG_MESSAGE) == MessageManager.FLAG_MESSAGE && (type2 & MessageManager.FLAG_MESSAGE) == MessageManager.FLAG_MESSAGE) return (type2 & MessageManager.FLAG_FIRST) == MessageManager.FLAG_FIRST && ((type1 & MessageManager.FLAG_FIRST) == MessageManager.FLAG_FIRST || (type1 & MessageManager.FLAG_FIRST) != MessageManager.FLAG_FIRST) ? Sizes.DP_8 : Sizes.DP_2; return 0; } } ); 
  • I suspect that you need to move all the visible elements of the list ... - aratj
  • What does it mean? Is there an example? - Marcus J. Weyland
  • Here is the first thing that papal, pay attention that they run through all the elements. gist.github.com/alexfu/0f464fc3742f134ccd1e - aratj
  • It does not help, since the separator is drawn in the onDraw method, and distances are also counted in the getItemOffsets method. - Marcus J. Weyland

1 answer 1

Corrected. It turned out the wrong position was getting. Well, I decided to calculate offset from the bottom of the cell, and not from the top and bottom.

 recyclerView.addItemDecoration( new RecyclerView.ItemDecoration() { @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.set( 0, 0, 0, getSize(parent, view) ); } private int getSize(int type1, int type2) { if ((type1 & MessageManager.FLAG_MESSAGE) == MessageManager.FLAG_MESSAGE && (type2 & MessageManager.FLAG_MESSAGE) == MessageManager.FLAG_MESSAGE) return (type2 & MessageManager.FLAG_FIRST) == MessageManager.FLAG_FIRST && ((type1 & MessageManager.FLAG_FIRST) == MessageManager.FLAG_FIRST || (type1 & MessageManager.FLAG_FIRST) != MessageManager.FLAG_FIRST) ? Sizes.DP_16 : Sizes.DP_4; return 0; } private int getSize(RecyclerView recyclerView, View view) { int position = recyclerView.getChildAdapterPosition(view); return getSize( messageManager.getType(position), messageManager.getType(position + 1) ); } } );