RecyclerView动画onRemove与固定项目

时间:2017-07-20 08:44:34

标签: android animation android-recyclerview android-animation

我想知道如何在具有固定位置和特定布局的项目之前删除项目时强制执行RecyclerView以执行正确/漂亮的动画。

我试图在此tutorial的基础上进行构建,直到我的具体要求才能正常工作。

只需对教程代码进行简单的更改:

@Override
public int getItemViewType(int position) {
    if(position == 3){
        return IREGULAR;
    }
    return REGULAR;
}

@Override
public ColorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if(viewType == REGULAR) {
         view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);

    }else{
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_iregular, parent, false);
    }

    // rest of tutorial code down here
}

@Override
public void onBindViewHolder(ColorViewHolder holder, int position) {
    int color;
    if(getItemViewType(position) == REGULAR) {
        color = colors.get(position);
    }else{
        color = 0xFF000000;
    }
    // rest of tutorial code down here
}

所以第三项会更高一点,总是黑色。

enter image description here

正如你在上面的图片中看到的那样,动画工作正常,直到第三个位置(不规则的一个)下面的项目被删除,但是当它在上面时,那么大的黑色空间出现在重新绑定项目的地方。 如何才能获得更好的动画体验?

1 个答案:

答案 0 :(得分:1)

删除行后,必须调用以下方法:

notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount());