dim*_*suz 21 animation android android-recyclerview
我有一些基本的项目装饰,在ItemDecoration.onDrawOver
方法中绘制一些东西.
这RecyclerView
也是DefaultItemAnimator
它的基础.动画正在运作,一切都很棒.除了一件事.
当所有现有项目与此适配器中设置的新项目交换时,动画正在运行时将显示装饰.
我需要一种隐藏它们的方法.当动画结束时,需要显示它们,但是当它运行时,它们必须被隐藏.
我尝试了以下方法:
public void onDrawOver(..., RecyclerView.State state) {
if(state.willRunPredictiveAnimations() || state.willRunSimpleAnimations()) {
return;
}
// else do drawing stuff here
}
Run Code Online (Sandbox Code Playgroud)
但这没有帮助.装饰仅在短时间的动画中被删除,但在它仍在运行时再次出现.
另外,setup还包括RecyclerView.Adapter
hasStableIds()(如果该位很重要).
Lor*_*rte 25
它可能在某种程度上取决于您正在使用的动画类型,但至少DefaultItemAnimator
您需要考虑动画期间完成的X/Y转换.您可以使用child.getTranslationX()
和获取这些值child.getTranslationY()
.
例如,对于垂直情况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);
}
}
Run Code Online (Sandbox Code Playgroud)
(ViewCompat.getTranslationY(child)
如果需要支持<API 11,您可能更愿意使用.)
注意:对于其他类型的动画,可能需要进行其他调整.(例如,也可能需要考虑水平翻译.)
自己找到答案:
要在项目动画期间隐藏项目装饰,可以使用此检查onDraw/onDrawOver
:
public void onDrawOver(..., RecyclerView parent, ...) {
if(parent.getItemAnimator() != null && parent.getItemAnimator().isRunning()) {
return;
}
// else do drawing stuff here
}
Run Code Online (Sandbox Code Playgroud)