einUsername
einUsername

Reputation: 1619

Animate single view in recyclerview after button click

I've got a button and a recycler view. The button refreshes the list. I want to animate one of the text views in my recycler view when it gets updated. Not the whole recyclerview, not the whole row - just one view (in every row).

I tried putting the animation in onBindViewHolder. But this starts the animation on scrolling and when i add a list entry:

@Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
Animation animation = AnimationUtils.loadAnimation(context, R.anim.rv_animation_clockwise);
                ((ViewHolderItem)holder).tv.startAnimation(animation);

Then i tried adding a TextChangedListener to my text view. But this has the same effect as putting it straight into onBindViewHolder:

((ViewHolderItem)holder).tv.addTextChangedListener(new TextWatcher() {
@Override
            public void afterTextChanged(Editable s) {

My last attempt was using findViewHolderForAdapterPosition. But it just doesn't do anything. This is the refresh method which my button calls. It's in the RecyclerViewAdapter. recyclerview is an instance variable which i set in onAttachedToRecyclerView:

RecyclerView recyclerView;

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    void refresh (List<Entry> al){
            this.al = al;
            notifyDataSetChanged();

    ((ViewHolderItem)recyclerView.findViewHolderForAdapterPosition(0)).tv.startAnimation(animation);

bump

Upvotes: 0

Views: 548

Answers (2)

einUsername
einUsername

Reputation: 1619

Thanks, i'll check out ItemAnimator later. What i ended up doing yesterday was to add in a small delay between my adapter.refresh() and the animation. Like they suggested here. Weirdly this delay seems to be necessary even if notifyDataSetChanged(); is called after the animation. I don't think it's a good solution but it works for now.

                final Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rv_animation_clockwise);
                recyclerView.postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        for (int i = 0; i < al.size(); i++) {
                            if (recyclerView.findViewHolderForAdapterPosition(i)!=null) {
                            ((RecyclerViewAdapter.ViewHolderItem) recyclerView.findViewHolderForAdapterPosition(i)).tv.startAnimation(animation);
                            }

                        }
                                            }
                },50);

Upvotes: 0

Miller Go Dev
Miller Go Dev

Reputation: 555

You should implement your own ItemAnimator and set it to your RecyclerView. There is some useful information here: https://hackmd.io/@nesquena/r1IEQ-jAl?type=view

Upvotes: 3

Related Questions