[英]How to update RecyclerView item without animation?
I have a RecyclerView
.我有一个RecyclerView
。 When I click a button inside an item in RecyclerView
, I want to change the color of a View
in that item.当我单击RecyclerView
某个项目内的按钮时,我想更改该项目中View
的颜色。 The following is my code and it works fine.以下是我的代码,它工作正常。 But, the problem is the item will have an animation which is ugly.但是,问题是该项目会有一个丑陋的动画。 I want to update the item without the animation.我想更新没有动画的项目。 How should I do that?我该怎么做? By the way, I don't want to turn off the animation, only for this click event.顺便说一下,我不想关掉动画,只针对这个点击事件。
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView imageView;
public Button button;
public ItemViewHolder(View view) {
super(view);
//do something
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
//change color
notifyItemChanged(getAdapterPosition());
}
}
Try this试试这个
notifyItemChanged(position, Object);
This will update the position without animating it as we are passing our Object in it.当我们在其中传递对象时,这将更新位置而不对其进行动画处理。
Try this and do let me know.试试这个,让我知道。
For Kotlin
you can use对于Kotlin
您可以使用
notifyItemChanged(int position, @Nullable Object payload)
根据Rakshit 的回答,在Kotlin 1.2 中,以下代码可以正常工作:
notifyItemChanged(position, Unit)
recyclerView.getItemAnimator().setChangeDuration(0);
或者这个。
There is a dedicated method to disable just item changed animations:有一种专门的方法可以禁用仅更改项目的动画:
((SimpleItemAnimator) myRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
Ref.: https://developer.android.com/reference/androidx/recyclerview/widget/SimpleItemAnimator参考: https : //developer.android.com/reference/androidx/recyclerview/widget/SimpleItemAnimator
Try this试试这个
csRecyclerView.getItemAnimator().setChangeDuration(0);
for more information RecyclerView.ItemAnimator更多信息RecyclerView.ItemAnimator
in kotlin : recyclerView.itemAnimator = null在 kotlin 中:recyclerView.itemAnimator = null
in java : recyclerView.setItemAnimator(null);在 Java 中:recyclerView.setItemAnimator(null);
developer said : A null return value indicates that there is no animator and that item changes will happen without any animations.开发人员说:一个空返回值表示没有动画师,项目更改将在没有任何动画的情况下发生。
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#getItemAnimator() https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#getItemAnimator()
Try this:试试这个:
public ItemViewHolder(View view) {
super(view);
//do something
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
holder.itemView.setBackgroundColor(Color.parseColor("#000000"));
notifyDataSetChanged();
});;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.