'animatedVectorDrawable is not animating in for loop

I have been trying to implement animation vectors . I managed to do it correctly but there's seem to be a problem. I used recycler view to display all contacts. I have used selected and unselected functions to play animations on contact profile pic. I did it and selecting and unselecting animations have been working correctly but when I tried to unselect all selected contacts through this loop it seems to be not working and it only happens when I want to unselect them all .

    private void deselectAll(viewHolder holder) {
        for(int i = 0; i < selected_contacts.size(); i++){
            unselected(holder, contacts.indexOf(selected_contacts.get(i)),true);
        }
        selected_contacts.clear();
    }

i have used unselected function which's code is down below. can anybody help me with this problem here cause i cant seem to figure it out.

private void unselected(viewHolder holder, int position, boolean b) {
        if(!b){
        selected_contacts.remove(contacts.get(position));
        }
        contacts.get(position).setSelected(false);

        AnimatedVectorDrawable animatedVectorDrawable = (AnimatedVectorDrawable) AppCompatResources.getDrawable(context, R.drawable.item_unselected);
        holder.image2.setBackground(animatedVectorDrawable);
        animatedVectorDrawable.start();

        Contact.selectedItems--;
        Log.i("items", "numbers of selected items are updated to " + Contact.selectedItems);

        String top_text = Contact.selectedItems + " Contacts selected";
        selectedItems.setText(top_text);

        Log.i("animation", "animation of item no " + position + " is unselected");
    }

And also animation is not working even for single item selected when tried to use setImageDrawable instead of setBackground idk what's the difference.


If you wanna see my whole project here is link to My project : https://github.com/kevaldonga/contacts



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source