Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare overridePendingTransition inside onBindViewHolder of a RecyclerView adapter?

I've created an intent object along with Bundle when a CardView is clicked and then I start the Activity. How do I declare overridePendingTransition inside it? Code is given belew :

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.textView.setText(strings[position]);
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(holder.itemView.getContext(),SecondActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("list",strings[position]);
            intent.putExtras(bundle);
            holder.itemView.getContext().startActivity(intent);
            //overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);
        }
    });
}
like image 953
Swapnil Sharma Avatar asked Mar 12 '23 21:03

Swapnil Sharma


1 Answers

You must pass an Activity to your RecyclerView.Adapter class when declaring it like :

First of all in your Adapter's constructor add Activity field :

public class MyAdapter extends RecyclerView.Adapter{

Activity mActivity;
ArrayList<YourModel> models;

public MyAdapter(ArrayList<YourModel> models,Activity mActivity){
    this.mActivity=mActivity;
    this.models=models;
}
}

Then add this line in your Activity:

Activity thisActivity=(Activity)this;

Then in your Activity declare your adapter like :

MyAdapter myAdapter=new MyAdapter(models,thisActivity);

And finally you can use this Activity in your adapter for transition like :

mActivity.overridePendingTransition();
like image 87
Yasin Kaçmaz Avatar answered Apr 06 '23 03:04

Yasin Kaçmaz