其他分享
首页 > 其他分享> > android – 禁用/暂停RecyclerView动画和/或滚动时只允许RecyclerView动画

android – 禁用/暂停RecyclerView动画和/或滚动时只允许RecyclerView动画

作者:互联网

所以基本上,我有一个简单的RecyclerView.Adapter,我有这样的事情:

public void onBindViewHolder(ViewHolder viewHolder, int position) {
    final Session currentSession = sessionList.get(position);

    viewHolder.textTittle.setText(currentSession.getTitle());
    viewHolder.textStartEnd.setText(currentSession.getStartHour() + " - " + currentSession.getEndHour());
    viewHolder.textDate.setText(currentSession.getDateFormatedString());
    viewHolder.textTrack.setText(currentSession.getTrack());
    viewHolder.textRoom.setText(currentSession.getRoom());


    if (position > this.lastPosition) {
        animate(viewHolder, true);
    } else {
        animate(viewHolder, false);
    }

    this.lastPosition = position;
    viewHolder.setClickListener(clickListener, currentSession);
}

当我向上/向下滚动时动画很好但是当我更改所有数据甚至加载RecyclerView(包含CardViews列表)时,动画显得非常混乱.
我在尝试更改所有数据时暂时禁用动画,或者在我向上滚动CardView列表时启用动画.

你知道怎么做吗?

谢谢.

解决方法:

RecyclerView内置了动画支持,但在滚动时它不支持动画.

看起来你自己动画,在这种情况下(假设你不想添加/删除/更改动画)只需将ItemAnimator设置为null. (RecyclerView#setItemAnimator).

您添加的这些自定义动画将导致回收问题,因此您还应实现onFailedToRecycle. https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#onFailedToRecycleView(VH)

标签:android,android-recyclerview,animation,android-cardview
来源: https://codeday.me/bug/20190829/1764282.html