user6586661 user6586661 - 4 months ago 21
Android Question

RecyclerView | OnItemClickListener With Shared Transition

I want for my app a shared transition. For this I need a

OnItemClickListener
. I've searched for a while, but I haven't found anything for me.

Here is my
Adapter
with
ViewHolder
:

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.PostViewHolder>{

List<Post> posts;


public ListAdapter(List<Post> posts) {
Log.d("ListAdapter", "");
this.posts = posts;
}

@Override
public int getItemCount() {
Log.d("getItemCount", "");
return posts.size();
}

@Override
public PostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
Log.d("onCreateView", "");
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.insert_layout, viewGroup, false);
PostViewHolder pvh = new PostViewHolder(v);
return pvh;
}

@Override
public void onBindViewHolder(PostViewHolder postViewHolder, int i) {
Log.d("onBindView", "");
postViewHolder.username.setText(posts.get(i).getUsername());
postViewHolder.text.setText(posts.get(i).getText());
postViewHolder.time.setText(Long.toString(posts.get(i).getTime()));
postViewHolder.postPhoto.setImageResource(posts.get(i).returnIMG());
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
Log.d("onAttached", "");
super.onAttachedToRecyclerView(recyclerView);
}


public static class PostViewHolder extends RecyclerView.ViewHolder{
CardView cv;
TextView username;
TextView time;
TextView text;
ImageView postPhoto;

PostViewHolder(View itemView) {
super(itemView);
Log.d("PostViewHolder", "");
cv = (CardView) itemView.findViewById(R.id.cv);
username = (TextView) itemView.findViewById(R.id.usernameText);
time = (TextView) itemView.findViewById(R.id.timeText);
text = (TextView) itemView.findViewById(R.id.textText);
postPhoto = (ImageView) itemView.findViewById(R.id.postPhoto);
}


}}


And for the transition I have this:

Intent intent = new Intent(MainActivity.this, PostViewActivity.class);

ImageView placeImage = (ImageView)view.findViewById(R.id.postPhoto);
TextView textView = (TextView)view.findViewById(R.id.textText);

Pair<View, String> imagePair = Pair.create((View) placeImage, getString(R.string.transition_image));
Pair<View, String> holderPair = Pair.create((View) textView, getString(R.string.transition_username));

ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, imagePair, holderPair);
ActivityCompat.startActivity(MainActivity.this, intent, options.toBundle());


But now I need a
OnClickListener
to connect this. Can someone help me?

Thanks!

Answer

You could pass an OnClickListener to your adapter as a constructor param (or write a setter), and bind it to your itemView in onBindViewHolder():

For example:

List<Post> posts;
View.OnClickListener listener;

public ListAdapter(List<Post> posts, View.OnClickListener listener) {
    Log.d("ListAdapter", "");
    this.posts = posts;
    this.listener = listener;
}

// ...

@Override
public void onBindViewHolder(PostViewHolder postViewHolder, int i) {
    Log.d("onBindView", "");
    postViewHolder.username.setText(posts.get(i).getUsername());
    postViewHolder.text.setText(posts.get(i).getText());
    postViewHolder.time.setText(Long.toString(posts.get(i).getTime()));
    postViewHolder.postPhoto.setImageResource(posts.get(i).returnIMG());

    postViewHolder.itemView.setOnClickListener(listener);
}

Instantiating your adapter in MainActivity:

ListAdapter adapter = new ListAdapter(posts, new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // do your stuff
    }
});