Item animation stopping in recyclerview when scrol

2019-07-28 10:33发布

问题:

I have a Recyclerview, im animating a view inside individual list item, but when I scroll the recyclerview the animation is stopping. Its because recyclerview removes the items form its view so when we scroll back it fetches it back! But now i want that animation to keep going as I would stop it only when i get data from server!

All I want is the animation that I start in the individual items inside the recylerview shouldn't stop even if the recyclerview is scrolled and the view is out of focus and comes back to focus! I need to stop the animation in the code when I get the server data! I have the code where to stop the animation and it works if the item is not scrolled off the view!

btn.onClick -- this button is the onClick for the recyclerview list item 1 btn.startAnimation(anim.xml) -- starting the animation

onSuccess -- server returns success btn.clearAnimation();

but before the onSuccess if we scroll the list the animation is stopped!

Please help!

回答1:

Hard to give you a full solution but have you tried saving the animation state inside the ViewHolder that you are using? I'd recommend saving a boolean flag in the ViewHolder class you defined like isAnimating which is initially set to false and in your onBindViewHolder(...) method you can do something like

if (viewHolder.isAnimating) {
    // start animation
} else {
    // clear animation 
}

viewHolder.btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            viewHolder.isAnimating = true;
            // start animation
        }
    });


回答2:

By inspiring from crymson's answer i have made little easy and useful solution using tag method of View instead setting a boolean in complicated logic of your custom adapter.

@Override
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
    super.onViewDetachedFromWindow(holder);
    if (holder.getItemViewType() == TYPE_AD)
        ((ViewHolderForAd) holder).ivStory.setTag(false);
}


public class ViewHolderForAd extends RecyclerView.ViewHolder {
        private ImageView ivStory;
        TextView tvName;

    public ViewHolderForAd(View view) {
        super(view);
        ivStory = (ImageView) view.findViewById(R.id.ivStoryImage);
        tvName = (TextView) view.findViewById(R.id.tvAppName);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int pos = getAdapterPosition();
                if (pos < 0) {
                    pos = (int) v.getTag();
                }
                customItemClickListener.onItemClicked(v, pos);
            }
        });

       //ivStory.startAnimation(AnimationUtils.loadAnimation(context, R.anim.pulse_story));
        ivStory.setTag(false); //Set default tag false to decrease risk of null
    }
}

 @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
//...Your code...
if (!(boolean) holder1.ivStory.getTag()) {
     holder1.ivStory.setTag(true);
     holder1.ivStory.startAnimation(AnimationUtils.loadAnimation(context, R.anim.pulse_story));
}
//...Your code...//
}

You can use setTag(key, object) instead of setTag(object) if you already tagged something(like position) in your imageView. Hope this helps someone.