问题
I have a RecyclerView in my app that displays a row containing pictures and photographs downloaded from the internet. There is a photo and a button on screen. When the user clicks a button, I want to make another view visible on top of the on screen photo.
I have a CustomViewHolder that holds all the views for the RecyclerView and a CustomRecyclerViewAdapter that displays the viewholder views. The onClick method for the button is overridden in the CustomViewHolder as below -
public static class CustomViewHolder extends RecyclerView.ViewHolder
implements OnClickListener {
public Button mButton;
public ImageView mImageView1;
public ImageView mImageView2;
public CustomViewHolder(View v)
{
super(v);
mButton = (Button) ... // Initializing views
mImageView1 = (ImageView) ...
mImageView2 = (ImageView) ...
}
@Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.buttonid)
{
// TODO mImageView2.setVisibility(View.VISIBLE);
// Any ideas on how to get the mImageView2 of this same item so it can be made visible?
}
}
I need a way to get a reference of the mImageView2 view at the same position as the clicked button. Does anybody know how to do this? I'd really appreciate any help.
回答1:
Another way is implement onBindViewHolder(ViewHolder holder, final int position)
in your Adapter
This method already have position as a third argument.
LIKE
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mImageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
回答2:
Try getAdapterPosition method of the ViewHolder. it will return the position of the Holder in the Recycler/Adapter
来源:https://stackoverflow.com/questions/30951551/how-to-get-other-views-in-same-viewholder-at-same-position-as-the-onclicked-view