How can I draw ItemDecoration correctly when a RecyclerView element animates its height?

I have a RecyclerView that uses a LinearLayoutManager (vertical). What I'm trying to accomplish is that the div element (full and 1px tall) bypasses the animation with the view when moving the view (the translation Y () works correctly with this, and when the view changes its height. In the current code, which I have below, the divider will move to the future position of the bottom of the view, and not to the current bottom during the animation. Is there a way to explain the height changes in decorating elements during an animation so that the animation looks better?

I am changing the height of the view using notifyItemChanged () on the adapter for RecyclerView.

public class DividerItemDecoration extends RecyclerView.ItemDecoration { private Drawable mDivider; public DividerItemDecoration(Context context) { mDivider = context.getResources().getDrawable(R.drawable.line_divider); } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { int right = parent.getWidth(); int dividerHeight = mDivider.getIntrinsicHeight(); int childCount = parent.getChildCount(); for (int i = 0; i < childCount - 1; i++) { View child = parent.getChildAt(i); View nextChild = parent.getChildAt(i + 1); RecyclerView.LayoutParams layoutParams1 = (RecyclerView.LayoutParams) child.getLayoutParams(); RecyclerView.LayoutParams layoutParams2 = (RecyclerView.LayoutParams) nextChild.getLayoutParams(); int left = 0; if (layoutParams1 != null && layoutParams2 != null) { left = Math.min(layoutParams1.leftMargin, layoutParams2.leftMargin); } int ty = (int) (child.getTranslationY() + 0.5f); int top = child.getBottom() + ty; int bottom = top + dividerHeight; mDivider.setBounds( left, top, right, bottom); mDivider.draw(c); } } } 
+6
source share
1 answer

I had the same problem. Solving this, adding the child transition to the calculations in the onDraw method, for example:

 @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { int left = parent.getPaddingLeft(); int right = parent.getWidth() - parent.getPaddingRight(); int childCount = parent.getChildCount(); for (int i = 0; i < childCount - 1; i++) { View child = parent.getChildAt(i); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); //Here is the trick! int top = child.getBottom() + params.bottomMargin + Math.round(child.getTranslationY()); int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } 
+3
source

All Articles