简体   繁体   中英

RecyclerView click listener not triggered on row click

Edit It seems like my @Override for onMentorClick(Mentor mentor) is never being accessed.

I have a recycler view that shows a list of people. I need to be able to click on one of the people in the list and have a new activity open.

in my adapter, I have the following code:

private OnMentorClickListener listener;

public interface OnMentorClickListener {
        void onMentorClick(Mentor mentor);
    }

public void setOnMentorClickListener(OnMentorClickListener listener){
        this.listener = listener;
    }

in my activity, I have the following code:

mentorAdapter.setOnMentorClickListener(new MentorAdapter.OnMentorClickListener(){
            @Override
            public void onMentorClick(Mentor mentor) {
                Intent intent = new Intent(MentorActivity.this, MentorAddEditActivity.class);

                intent.putExtra(MentorAddEditActivity.EXTRA_MENTOR_ID, mentor.getMentor_id());
                intent.putExtra(MentorAddEditActivity.EXTRA_MENTOR_NAME, mentor.getMentor_name());
                intent.putExtra(MentorAddEditActivity.EXTRA_MENTOR_PHONE, mentor.getMentor_phone());
                intent.putExtra(MentorAddEditActivity.EXTRA_MENTOR_EMAIL, mentor.getMentor_email());

                startActivityForResult(intent, EDIT_MENTOR_REQUEST);
            }
        });

I have implemented listeners for other portions of my app the exact same way and they all work. I've scoured my code and this setup seems to be identical to other sections of code.

I get no errors, but when I'm on my "Mentors" page and have all the mentors listed out, nothing happens when I click on one. No errors, no crashes, no nothing.

I've also tried to just implement the OnMentorClickListener strait into the Activity class like the following:

public class MentorActivity extends AppCompatActivity implements MentorAdapter.OnMentorClickListener

and overriding outside of OnCreate:

@Override
    public void onMentorClick(Mentor mentor) {
        Intent intent = new Intent(MentorActivity.this, MentorAddEditActivity.class);

        intent.putExtra(MentorAddEditActivity.EXTRA_MENTOR_ID, mentor.getMentor_id());
        intent.putExtra(MentorAddEditActivity.EXTRA_MENTOR_NAME, mentor.getMentor_name());
        intent.putExtra(MentorAddEditActivity.EXTRA_MENTOR_PHONE, mentor.getMentor_phone());
        intent.putExtra(MentorAddEditActivity.EXTRA_MENTOR_EMAIL, mentor.getMentor_email());

        startActivityForResult(intent, EDIT_MENTOR_REQUEST);
    }

This does not work either.

Any thoughts on my I can't actually click the mentor in the RecyclerView?

Entire MentorAdapter for clarification. This is as identical as possible to all of my other adapters with variable names changed for Mentor.

public class MentorAdapter extends RecyclerView.Adapter<MentorAdapter.MentorHolder> {
    private static final String TAG = AppRepository.class.getSimpleName();

    private OnMentorClickListener listener;


    private List<Mentor> mentorList = new ArrayList<>();


    class MentorHolder extends RecyclerView.ViewHolder{
        private TextView textViewMentorName;
        private TextView textViewMentorPhone;
        private TextView textViewMentorEmail;


        MentorHolder(@NonNull View itemView) {
            super(itemView);
            textViewMentorName = itemView.findViewById(R.id.textView_mentor_name);
            textViewMentorPhone = itemView.findViewById(R.id.textView_mentor_phone);
            textViewMentorEmail = itemView.findViewById(R.id.textView_mentor_email);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if(listener!=null&&position!=RecyclerView.NO_POSITION){
                        listener.onMentorClick(mentorList.get(position));
                    }
                }
            });
        }
    }

    @NonNull
    @Override
    public MentorHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.mentor_item, parent, false);
        return new MentorHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MentorHolder holder, int position) {
        Mentor currentMentor = mentorList.get(position);
        holder.textViewMentorName.setText(currentMentor.getMentor_name());
        holder.textViewMentorEmail.setText(currentMentor.getMentor_email());
        holder.textViewMentorPhone.setText(currentMentor.getMentor_phone());

    }

    @Override
    public int getItemCount() {
        return mentorList.size();
    }

    public void setMentorList(List<Mentor> mentors){
        this.mentorList = mentors;
        notifyDataSetChanged();
    }

    public Mentor getMentorAt(int position){return mentorList.get(position);}

    public interface OnMentorClickListener {
        void onMentorClick(Mentor mentor);
    }

    public void setOnMentorClickListener(OnMentorClickListener listener){
        this.listener = listener;
    }


}

If you are overriding onMentorClick your activity should implement OnMentorClickListener interface, then on your adapter you should set the activity as your listener with:

mentorAdapter.setOnMentorClickListener(this)

In your code, you're using a new click listener (not the activity)

mentorAdapter.setOnMentorClickListener(new MentorAdapter.OnMentorClickListener(){
            @Override
            public void onMentorClick(Mentor mentor) {

            }

});

In addition, you should fire your onMentorClick on the user click in your adapter implementation, preferably in the onCreateViewHolder method:

public YourAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {

    ViewHolder holder = ViewHolder(
        LayoutInflater.from(context).inflate(
            R.layout.your_layout,
            parent,
            false
        ))

    holder.itemView.setOnClickListener {
        listener.onMentorClick(mentor)
    }

    return holder ;
}

Turns out that in my mentor_item.xml file used to create the layout for the Mentor RecyclerView, I had created an across the entire card on the top layer that I wasn't doing anything with.

Once I removed that ImageButton from the xml file, the clicks were being registered and everything worked as intended.

Thank you for everyone's time.

In some place you need a call to these Callback, something like this:

profileView.setOnClickListener(new ....
     listener.onMentorListener(mentor);
)

Do you have this implementation in your Adapter?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM