I have read different articles here, but cannot find what I am doing wrong. I have RecyclerView which shows a list of items and I want to raise an event when an item is clicked. But after clicking nothing happens and the event is not fired. Here is the code of the layout of the 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>
Then the cardview (I have set all controllers to clickable):
<?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>
Then the code of the fragement, which displays the 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));
}
And then the code of the adapter:
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);
}
}
I could not find the reason why the function onClick
is not called. Would appreciate any help