Asif Ali
Asif Ali

Reputation: 1432

Shared element transition not working- I can't resolve method ActivityOptionsCompat.makeSceneTransitionAnimation(mContext, p1, p2);

I'm trying to start an activity with shared element transition from recycler adapter.

Here is the snippet:-

@Override
public void onBindViewHolder(final ViewholderPostFeed holder, final int position) {

  holder.post_header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!isLoggedIn())
                showAlertDialog(v);
            else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    // Call some material design APIs here
                    Intent myIntent = new Intent(MyApplication.getAppContext(), UserProfile.class);
                    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    myIntent.putExtra("NAME",postFeed.getUser_name());
                    myIntent.putExtra("PIC", postFeed.getUser_pic());
                    myIntent.putExtra("STATUS", postFeed.getUser_status());
                    Pair<View, String> p1 = Pair.create((View)holder.circleImageView, "profile");
                    Pair<View, String> p2 = Pair.create((View)holder.user_name, "user_name");
                    ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(MyApplication.getAppContext(), p1, p2);
                    MyApplication.getAppContext().startActivity(myIntent, options.toBundle());

                } else {
                    // Implement this feature without material design
                    Intent myIntent = new Intent(MyApplication.getAppContext(), UserProfile.class);
                    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    myIntent.putExtra("NAME",postFeed.getUser_name());
                    myIntent.putExtra("PIC", postFeed.getUser_pic());
                    myIntent.putExtra("STATUS", postFeed.getUser_status());
                    MyApplication.getAppContext().startActivity(myIntent);
                }
            }
        }
    });

Error:Can't resolve method ActivityOptionsCompat.makeSceneTransition(...Context,...View,...String);

I have given transition name in row_layout as well as in user_activity.xml:

tansitionName="profile"
tansitionName="user_name"

Please help.

Upvotes: 2

Views: 2390

Answers (2)

Satyam Singh
Satyam Singh

Reputation: 51

This can be done very easily by passing casting of the context into activity. Get the context as a constructor parameter in your adapter.

Context mcontext;
public RecyclerViewAdapter(Context context)
{
    mcontext = context;
}

And then cast the context into activity in onBindViewHolder.

ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity)mcontext, p1, p2);
mcontext.startActivity(myIntent, options.toBundle());

Upvotes: 3

Froyo
Froyo

Reputation: 18477

ActivityOptionsCompat.makeSceneTransition requires Activity and not Context.

Also, since you're using ActivityOptionsCompat you don't need to put a check for lollipop and above. It will work on its own. It's just that you won't see any transitions in phones running kitkat and below.

Edit:

Adding reference of Activity to the Adapter is completely upto you. You may use callback (recommended) or pass Activity as one of the constructor parameter.

@Override
public void onBindViewHolder(final ViewholderPostFeed holder, final int position) {

 holder.post_header.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        clickCallback.openProfile(postFeed);
    }
 }

public void setCallback(ClickCallback callback) {
    this.clickCallback = callback;
}

public interface ClickCallback {
    void openProfile(PostFeed postFeed);
}

In your Activity,

MyAdapter adapter = new Adapter(/* params */);
adapter.setCallback(new ClickCallback() {
    @Override
    public void openProfile(PostFeed postFeed) {
         // Your code here
    }
});

Upvotes: 1

Related Questions