微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 当RecyclerView元素为其高度设置动画时,如何正确绘制ItemDecoration?

我有一个使用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);
        }
    }
}

解决方法

我遇到过同样的问题.通过在onDraw方法添加子项转换到计算来解决这个问题,如下所示:
@Override
public void onDraw(Canvas c,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,bottom);
        mDivider.draw(c);
    }
}

原文地址:https://www.jb51.cc/android/317828.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐