如何禁用RecyclerView项目装修图纸为项目动画的持续时间 [英] How to disable RecyclerView item decoration drawing for the duration of item animations

查看:137
本文介绍了如何禁用RecyclerView项目装修图纸为项目动画的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些基本项目的点缀得出了一些东西,在 ItemDecoration.onDrawOver 方法。

I have some basic item decoration which draws some stuff in ItemDecoration.onDrawOver method.

RecyclerView 也有 DefaultItemAnimator 设置就可以了。
动画工作,全部是伟大的。除了一件事。

This RecyclerView also has DefaultItemAnimator set on it. Animations are working, all is great. Except one thing.

在所有现有项目都与此适配器设置一个新的项目交换,而动画运行正在显示的装饰品。

When all existing items are swapped with a new item set in this adapter, the decorations are being shown while animation is running.

我需要一种方法来隐藏他们。
当动画结束后,他们需要被显示,但在运行时,它们必须被隐藏。

I need a way to hide them. When animation finishes, they need to be shown, but while it is running, they must be hidden.

我试过如下:

public void onDrawOver(..., RecyclerView.State state) {
    if(state.willRunPredictiveAnimations() || state.willRunSimpleAnimations()) {
        return;
    }
    // else do drawing stuff here
}

,但这是没有帮助。装修只为动画的短期内拆除,但随后再次出现,同时它仍在运行。

but this isn't helping. Decoration is only removed for the short period of animation, but then appears again while it is still running.

还设置包括 RecyclerView.Adapter 这hasStableIds()(在该位问题的情况下)。

Also setup includes a RecyclerView.Adapter which hasStableIds() (in case that bit matters).

推荐答案

这可能有些你所使用的类型依赖动画,但至少 DefaultItemAnimator 你需要考虑到在X /动画时正在做Y平移。你可以用获取这些值child.getTranslationX() child.getTranslationY()

It may depend somewhat on the type animation you're using, but at least for DefaultItemAnimator you need to account for the X/Y translation being done during the animation. You can get these values with child.getTranslationX() and child.getTranslationY().

例如,对于垂直情况下的onDraw / onDrawOver

For example, for the vertical case of onDraw/onDrawOver:

private void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();
    final int childCount = parent.getChildCount();
    final int dividerHeight = mDivider.getIntrinsicHeight();

    for (int i = 1; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
        final int ty = (int)(child.getTranslationY() + 0.5f);
        final int top = child.getTop() - params.topMargin + ty;
        final int bottom = top + dividerHeight;
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

(您可能preFER使用 ViewCompat.getTranslationY(子)如果您需要支持&LT; API 11)

(You may prefer to use ViewCompat.getTranslationY(child) if you need to support < API 11.)

注:其它类型的动画,其他调整可能需要作出。 (例如,水平译文还可能需要考虑。)

Note: for other types of animations, additional adjustments may need to be made. (For example, horizontal translation might also need to be accounted for.)

这篇关于如何禁用RecyclerView项目装修图纸为项目动画的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆