【问题标题】:Android: RecyclerView does not fire onClick Event [duplicate]Android:RecyclerView 不会触发 onClick 事件 [重复]
【发布时间】:2017-03-19 23:57:03
【问题描述】:

我在这里阅读了不同的文章,但找不到我做错了什么。我有 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>

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

<?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 未被调用的原因。希望有任何帮助

【问题讨论】:

  • 您忘记将NotificationHolder 设置为OnClickListener
  • @MikeM。是的,就是这样。谢谢

标签: java android android-recyclerview


【解决方案1】:

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

注册点击事件..

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    相关资源
    最近更新 更多