gpt4 book ai didi

java - Android:RecyclerView 不触发 onClick 事件

转载 作者:行者123 更新时间:2023-12-01 06:06:48 28 4
gpt4 key购买 nike

我在这里阅读了不同的文章,但找不到我做错了什么。我有 RecyclerView,它显示项目列表,我想在单击项目时引发事件。但点击后没有任何反应,事件也没有被触发。这是 fragment 布局的代码:

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

然后是卡片 View (我已将所有 Controller 设置为可点击):

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

然后是显示recyclerview的fragment的代码:

....    
@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 的原因不被调用。将不胜感激任何帮助

最佳答案

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

注册点击事件..

在 notificationHolder 构造函数中添加以下行... itemView.setOnClickListener(this);它将起作用......

关于java - Android:RecyclerView 不触发 onClick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42888643/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com