Android:RecyclerView不会触发onClick事件

时间:2017-03-19 15:49:15

标签: java android android-recyclerview

我在这里读过不同的文章,但找不到我做错了什么。我有RecyclerView,它显示了一个项目列表,我想在点击一个项目时引发一个事件。但点击后没有任何事情发生,事件没有被解雇。 以下是fragement布局的代码:

<?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>

然后是cardview(我已将所有控制器设置为可点击):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="82dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:clickable="true"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_marginLeft="10dp"
            android:id="@+id/imageViewNotification"
            android:layout_width="64dp"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/stimmungsabgabe"
            android:clickable="true"/>
        <TextView
            android:id="@+id/info_text"
            android:layout_toRightOf="@id/imageViewNotification"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Just want to test it"
            android:textAlignment="center"
            android:layout_marginLeft="5dp"
            android:gravity="center"
            android:clickable="true"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>

然后是fragement的代码,它显示了recyclerview:

....    
@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);
}
}

我找不到函数onClick未被调用的原因。非常感谢任何帮助

1 个答案:

答案 0 :(得分:-1)

有一个愚蠢的错误......你已经实现了onclick方法,但是你忘了注册点击事件......

注册点击事件..

在notificationHolder构造函数中添加以下行...  itemView.setOnClickListener(本); 它会工作....