Java Android:RecyclerView不会触发onClick事件

Java Android:RecyclerView不会触发onClick事件,java,android,android-recyclerview,Java,Android,Android Recyclerview,我在这里读过不同的文章,但找不到我做错了什么。我有一个显示项目列表的RecyclerView,我想在单击项目时引发一个事件。但单击后,不会发生任何事情,也不会触发事件。 以下是框架布局的代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.andr

我在这里读过不同的文章,但找不到我做错了什么。我有一个显示项目列表的RecyclerView,我想在单击项目时引发一个事件。但单击后,不会发生任何事情,也不会触发事件。 以下是框架布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/recycler"
    android:scrollbars="vertical"
    android:clickable="true"/>
</RelativeLayout>
然后是适配器的代码:

public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationHolder> {
List<Notification> notifications;
TabFragment context;
public  class NotificationHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    CardView cv;
    TextView tv;
    ImageView imageView;
    NotificationHolder(View itemView) {
        super(itemView);
        tv = (TextView) itemView.findViewById(R.id.info_text);
        cv = (CardView) itemView.findViewById(R.id.card_view);
        imageView = (ImageView) itemView.findViewById(R.id.imageViewNotification);
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(ActivityMain.activityMain,"Clicked",Toast.LENGTH_SHORT).show();

        }

    }
}
public void removeAt(int position) {
    notifications.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, notifications.size());
}

public NotificationAdapter(List<Notification> notifications, TabFragment context){
    this.notifications = notifications;
    this.context = context;
}

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

@Override
public NotificationHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_notification, viewGroup, false);
    NotificationHolder bvh = new NotificationHolder(v);
    return bvh;
}

@Override
public void onBindViewHolder(final NotificationHolder holder, final int position) {
    NotificationHolder notificationHolder = (NotificationHolder) holder;
    notificationHolder.tv.setText(notifications.get(position).getText());
    notificationHolder.imageView.setImageResource(notifications.get(position).getImage());
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}
公共类NotificationAdapter扩展了RecyclerView.Adapter{
列出通知;
tab片段上下文;
公共类NotificationHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
卡德维尤简历;
文本视图电视;
图像视图图像视图;
通知持有者(查看项目视图){
超级(项目视图);
tv=(TextView)itemView.findViewById(R.id.info\u text);
cv=(CardView)itemView.findViewById(R.id.card\u视图);
imageView=(imageView)itemView.findViewById(R.id.imageViewNotification);
}
@凌驾
公共void onClick(视图){
Toast.makeText(ActivityMain.ActivityMain,“点击”,Toast.LENGTH_SHORT.show();
}
}
}
公共无效删除(内部位置){
通知。删除(位置);
已移除(位置)的项目;
notifyItemRangeChanged(位置,notifications.size());
}
公共通知适配器(列表通知、选项卡片段上下文){
this.notifications=通知;
this.context=上下文;
}
@凌驾
public int getItemCount(){
返回通知。size();
}
@凌驾
公共通知持有者onCreateViewHolder(视图组视图组,int i){
视图v=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.cardwiew_通知,viewGroup,false);
通知持有人bvh=新通知持有人(v);
返回bvh;
}
@凌驾
BindViewHolder上的公共无效(最终通知持有人,最终整数位置){
通知持有人通知持有人=(通知持有人)持有人;
notificationHolder.tv.setText(notifications.get(position.getText());
notificationHolder.imageView.setImageResource(notifications.get(position.getImage());
}
@凌驾
附加ToRecyclerView(RecyclerView RecyclerView)上的公共无效{
super.onAttachedToRecyclerView(recyclerView);
}
}

我找不到没有调用函数
onClick
的原因。非常感谢您的帮助有个愚蠢的错误。。。您已实现onclick方法,但忘记注册单击事件

注册单击事件

在notificationHolder构造函数中添加以下行。。。 setOnClickListener(这个);
它将起作用。…

您忘记将
NotificationHolder
设置为任何内容的
OnClickListener
。@MikeM。是的,就是这样。谢谢
....    
@Override
public void onStart() {
    Notification n1 = new Notification("Trainingeintrag","Nun ist es soweit. Haben Sie heute trainert?", R.drawable.trainingseinheit);
    Notification n2 = new Notification("Stimmungsabfrage", "Wie fühlen Sie sich in dem Moment?", R.drawable.stimmungsabgabe);
    Notification n3 = new Notification("Fragebogen zur Aktivität", "Wie aktiv sind Sie?",R.drawable.aktivitaet_fragebogen);
    Notification n4 = new Notification("Fitnessfragebogen", "Wie ist ihr aktueller Fitnessstand?",R.drawable.fitness_fragebogen);


    notifications =Arrays.asList(n1,n2,n3,n4);

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
    // setup notifications that appear only on specific occasions
    final Notification nMotivationMessage = new Notification(
            "Schon gewusst?",
            "Wissenswertes über Sport",R.drawable.trainingseinheit);
    if(preferences.getBoolean("motivationMessage",false)) {
        notifications.add(nMotivationMessage);
        preferences.edit().remove("motivationMessage").commit();
    }

    // fill the recycler
    rv = (RecyclerView)view.findViewById(R.id.recycler);
    LinearLayoutManager lm = new LinearLayoutManager(getContext());
    rv.setLayoutManager(lm);
    // just create a list of tasks
    rv.setAdapter(new NotificationAdapter(notifications, this));
}
public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationHolder> {
List<Notification> notifications;
TabFragment context;
public  class NotificationHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    CardView cv;
    TextView tv;
    ImageView imageView;
    NotificationHolder(View itemView) {
        super(itemView);
        tv = (TextView) itemView.findViewById(R.id.info_text);
        cv = (CardView) itemView.findViewById(R.id.card_view);
        imageView = (ImageView) itemView.findViewById(R.id.imageViewNotification);
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(ActivityMain.activityMain,"Clicked",Toast.LENGTH_SHORT).show();

        }

    }
}
public void removeAt(int position) {
    notifications.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, notifications.size());
}

public NotificationAdapter(List<Notification> notifications, TabFragment context){
    this.notifications = notifications;
    this.context = context;
}

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

@Override
public NotificationHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_notification, viewGroup, false);
    NotificationHolder bvh = new NotificationHolder(v);
    return bvh;
}

@Override
public void onBindViewHolder(final NotificationHolder holder, final int position) {
    NotificationHolder notificationHolder = (NotificationHolder) holder;
    notificationHolder.tv.setText(notifications.get(position).getText());
    notificationHolder.imageView.setImageResource(notifications.get(position).getImage());
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}