【问题标题】:How can I draw ItemDecoration correctly when RecyclerView element is animating its height?当 RecyclerView 元素为其高度设置动画时,如何正确绘制 ItemDecoration?
【发布时间】:2015-11-24 20:38:29
【问题描述】:

我有一个使用 LinearLayoutManager(垂直)的 RecyclerView。我想要完成的是让项目装饰(全宽和 1px 高)分隔线在视图移动(translationY() 正确处理)以及视图改变其高度时与视图一起动画。在我下面的当前代码中,分隔线将在动画期间跳转到视图底部的未来位置,而不是当前底部。有什么方法可以考虑动画期间物品装饰的高度变化以使动画看起来更好?

我正在通过在 RecyclerView 的适配器上使用 notifyItemChanged() 来更改视图高度。

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);
        }
    }
}

【问题讨论】:

  • 这只是物品装饰api无法实现的吗?
  • 所以...正常的动画(添加、删除)会正确地为装饰设置动画,但是当更改视图height 时它会跳转吗?如果是这样,您能否提供更改视图高度的代码?你动画视图高度变化?仅供参考,装饰本身看起来还不错。
  • 默认的 recyclerview 动画器会自动更改高度(通过将内部视图设置为 GONE)

标签: android animation android-animation android-recyclerview


【解决方案1】:

我有同样的问题。通过在 onDraw 方法中添加孩子的过渡到计算来解决这个问题,如下所示:

@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);
    }
}

【讨论】:

  • 感谢您的回答。由于在我的情况下新插入的单元格显示带有 alpha 动画,我添加了: mDivider.setAlpha((int)(255.0 * child.getAlpha()));在绘制之前以相同的动画显示分隔线。
猜你喜欢
  • 2019-04-07
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多