Answer the question
In order to leave comments, you need to log in
How to hang a click handler on a RecyclerView element?
Hello everyone, I have a NavigationDraweerActivity in which a Fragment with a ViewPager is displayed, in which I display a fragment with a recycler view + card view. In the recyclerView adapter, the viewholder has an onClick method, the adapter itself implements the OnItemClickListener interface with the onItemClick method. The adapter also has a SetOnItemClickListener method to hang the handler on the cardview.
class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private CardView cv;
private ImageView commandImageView;
private TextView commandTextView;
public myViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.cv);
commandImageView = itemView.findViewById(R.id.commandImageView);
commandTextView = itemView.findViewById(R.id.commandTextView);
cv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
public interface OnItemClickListener {
public void onItemClick(View view , int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recViewAdapter = new RecViewAdapter(get_data(), getContext());
recyclerView.setAdapter(recViewAdapter);
recViewAdapter.SetOnItemClickListener(new RecViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//Мое действие
}
});
Answer the question
In order to leave comments, you need to log in
public class TransportAdapter extends RecyclerView.Adapter<TransportAdapter.TransportViewHolder> {
public interface onClickListener{
void onVariantClick(TransportModel model);
}
private onClickListener listener;
.....
@Override
public void onBindViewHolder(@NonNull TransportViewHolder holder, int position) {
final TransportModel transportModel = transportVariants.get(position);
....
if(listener != null){
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onVariantClick(transportModel);
}
});
}
}
.... бла бла бла
public void setListener(onClickListener listener){
this.listener = listener;
}
class TransportViewHolder extends RecyclerView.ViewHolder {
.....бла бла бла
public TransportViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
How hard are you doing.
Do you need to handle the card view click?
If yes, then you implement the OnClickListener ,
implement the OnClick method in the RecyclerView.ViewHolder instance and catch the card view click through its id.
Accordingly, in the Holder's constructor, you initialize the card view itself
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question