P
P
plvskiy2018-11-22 17:49:55
Java
plvskiy, 2018-11-22 17:49:55

How to make onClick in RecyclerView?

I am loading data from Firebase Firestore and displaying in RecyclerView. How to make onClick for each RecyclerView item?
Here is my code:

public class NoteAdapter extends FirestoreRecyclerAdapter<Note, NoteAdapter.NoteHolder> {

    public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull Note model) {
        holder.textViewAmount.setText("");
        holder.textViewCoast.setText("");
        holder.textViewDate.setText(String.valueOf("");
        holder.textViewAmount.setTextColor("");

        Glide.with(holder.icon_opt.getContext())
                .load(model.getPhoto())
                .into(holder.icon_opt);
    }

    @NonNull
    @Override
    public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item,
                parent, false);

        return new NoteHolder(v);
    }

    class NoteHolder extends RecyclerView.ViewHolder{

        TextView textViewAmount, textViewCoast, textViewDate;
        ImageView icon_opt;
        CardView Cardview;

        public NoteHolder(View itemView) {
            super(itemView);

            textViewAmount = itemView.findViewById(textAmount);
            textViewCoast =  itemView.findViewById(textCoast);
            icon_opt = itemView.findViewById(R.id.icon_tr);
            textViewDate = itemView.findViewById(textDate);
            Cardview = itemView.findViewById(cardview);
        }
    }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2018-11-22
@zagayevskiy

You make your interface with the onClick method, throw it into the adapter, put the usual clicklistener on the view, and pull your own in it.

C
crystaldxpe, 2018-11-22
@crystaldxpe

In onBindViewHolder(...)you write holder.itemView.setOnclickListener(...)and assign a listener to each ViewHolder. In Java, if you need to execute code from the outside, you can declare a callback interface and implement it in the enemy or activity on which the list is located, or write in Kotlin, where you can simply pass a lambda as a constructor parameter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question