I have a RecyclerView
. When I click a button inside an item in RecyclerView
, I want to change the color of a View
in that item. 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
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
Try this
csRecyclerView.getItemAnimator().setChangeDuration(0);
for more information RecyclerView.ItemAnimator
in kotlin : recyclerView.itemAnimator = null
in 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.
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();
});;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.