简体   繁体   English

RecyclerView 单击侦听器未在行单击时触发

[英]RecyclerView click listener not triggered on row click

Edit It seems like my @Override for onMentorClick(Mentor mentor) is never being accessed.编辑似乎永远不会访问我的@Override for onMentorClick(Mentor Mentor)。

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:我还尝试将 OnMentorClickListener 海峡实现到 Activity 类中,如下所示:

public class MentorActivity extends AppCompatActivity implements MentorAdapter.OnMentorClickListener

and overriding outside of OnCreate:并在 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?关于我实际上无法单击 RecyclerView 中的导师的任何想法?

Entire MentorAdapter for clarification.整个 MentorAdapter 进行说明。 This is as identical as possible to all of my other adapters with variable names changed for Mentor.这与我为 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:如果你覆盖onMentorClick你的活动应该实现OnMentorClickListener接口,那么在你的适配器上你应该将活动设置为你的监听器:

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:此外,您应该在适配器实现中的用户单击时触发onMentorClick ,最好是在onCreateViewHolder方法中:

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.事实证明,在我用于创建 Mentor RecyclerView 布局的mentor_item.xml 文件中,我在顶层的整个卡片上创建了一个我没有做任何事情的卡片。

Once I removed that ImageButton from the xml file, the clicks were being registered and everything worked as intended.一旦我从 xml 文件中删除了那个 ImageButton,点击就会被注册,一切都按预期工作。

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?你的适配器中有这个实现吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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