【发布时间】:2019-05-23 14:00:16
【问题描述】:
我的回收站视图中附加了一个物品装饰。它应该仅在最后一项上添加大量填充。这工作正常。但是,当我添加新项目时,getItemOffsets 仅对最后一个项目(而不是回收站视图中的每个项目)调用。这样,我最终在每个项目上都有这么大的填充。
添加新视图后,如何删除其余视图上的正确填充?我想以正确的方式添加项目以保留动画
public void onItemInserted(Card card, int position){
cardList.add(card);
notifyItemInserted(getItemCount() -1);
}
我的物品偏移方法:
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.top = horizontalSpace;
outRect.bottom = horizontalSpace;
outRect.left = horizontalSpace;
outRect.right = 0;
if (parent.getChildAdapterPosition(view) == parent.getChildCount() - 1){
outRect.right = endSpace;
}
}
【问题讨论】:
-
您正在添加一个项目,但您想重新绘制所有项目。因此,尝试用 notifyDataSetChanged() 替换 notifyItemInserted()。这将重新绘制所有项目。
-
@W0rmH0le 使用 notifyDataSetChanged 丢失动画
标签: android android-recyclerview item-decoration