CardView 的 OnClickListener 不工作 android?

OnClickListener for CardView is not working android?

我有联系人列表,我已使用 CardView 在 RecyclerView 上显示此列表。

列表显示完美但现在我想制作可点击的卡片视图。这样当我点击特定联系人时,我将显示特定联系人的详细信息。

列表显示完美,但当我点击特定联系人时,点击事件不起作用。

我还在 CardView 上使用了 clickable true,在 ContactAdapter 上使用了 OnClickListener 事件 class。

我做了什么:

我有三种布局activity_main、contact_list、contact_detail。

在activity_main中我使用了recycler_view。 在 contact_list 中,我使用了 card_view 并将其与回收站视图绑定。

我有三个 java class : 联系人 - 我用 getter 和 setter 定义了变量 ContactAdapter - 我在其中获取联系人并将其与卡片视图绑定。 MainActivity - 我在其中获取所有联系人并将其显示在回收站视图中。

activity_main布局代码:

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view">

</android.support.v7.widget.RecyclerView>

contact_list_布局代码:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/card_view"
    android:clickable="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Remo"
            android:id="@+id/person_name"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/person_name"
            android:text="example@gmail.com"
            android:id="@+id/person_email"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

Java Class代码:

主要活动代码:

RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] name,email;
ArrayList<Contact> list = new ArrayList<Contact>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    name = getResources().getStringArray(R.array.person_name);
    email = getResources().getStringArray(R.array.person_email);

    int count = 0;
    for (String Name : name)
    {
        Contact contact = new Contact(Name,email[count]);
        count++;
        list.add(contact);
    }
    recyclerView= (RecyclerView) findViewById(R.id.recycler_view);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapter=new ContactAdapter(list,this);
    recyclerView.setAdapter(adapter);
}

ContactAdapter 代码:

 ArrayList<Contact> contacts = new ArrayList<Contact>();
Context ctx;

public ContactAdapter(ArrayList<Contact> contacts, Context ctx) {
    this.contacts = contacts;
    this.ctx = ctx;
}

@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_list_layout, parent, false);
    ContactViewHolder contactViewHolder = new ContactViewHolder(view, ctx, contacts);
    return contactViewHolder;
}

@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
    Contact CON = contacts.get(position);
    holder.person_name.setText(CON.getName());
    holder.person_email.setText(CON.getEmail());
}

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

public static class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView person_name, person_email;
    ArrayList<Contact> contacts = new ArrayList<Contact>();
    Context ctx;

    public ContactViewHolder(View view, Context ctx, ArrayList<Contact> contacts) {
        super(view);
        this.contacts = contacts;
        this.ctx = ctx;
        person_name = (TextView) view.findViewById(R.id.person_name);
        person_email = (TextView) view.findViewById(R.id.person_email);
    }

    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        Contact contact = this.contacts.get(position);
        Intent intent =new Intent(this.ctx,ContactDetail.class);
        this.ctx.startActivity(intent);
    }
}

不要在 ContactViewHolder class(适配器内部的私有静态 class)中实现 OnClickListener(),而是在 onBindViewHolder 中实现它()方法.

检查此代码。

@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_list_layout, parent, false);
    ContactViewHolder contactViewHolder = new ContactViewHolder(view);
    return contactViewHolder;
}

@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
    Contact CON = contacts.get(position);
    holder.person_name.setText(CON.getName());
    holder.person_email.setText(CON.getEmail());
    holder.card_view.setOnClickListener(new OnClickListener{
    @Override
    public void onClick(View v) {
         // your click code here
         }
     });
}

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

public static class ContactViewHolder extends RecyclerView.ViewHolder {
    TextView person_name, person_email;
    CardView card_view;

    public ContactViewHolder(View view) {
        super(view);
        person_name = (TextView) view.findViewById(R.id.person_name);
        person_email = (TextView) view.findViewById(R.id.person_email);
        card_view=(CardView) view.findViewById(R.id.card_view);
    }

}

从你的代码来看,你错过了为你的卡片视图设置监听器。要触发 onClick(View v) 方法,您必须为相应的视图设置侦听器。 示例:

@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
    Contact CON = contacts.get(position);
    holder.person_name.setText(CON.getName());
    holder.person_email.setText(CON.getEmail());
    holder.card_view.setOnClickListener(this)
}

已更新 ViewHolder

 public static class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView person_name, person_email;
        CardView card_view;
        ArrayList<Contact> contacts = new ArrayList<Contact>();
        Context ctx;

        public ContactViewHolder(View view, Context ctx, ArrayList<Contact> contacts) {
            super(view);
            this.contacts = contacts;
            this.ctx = ctx;
            card_view = (CardView) view.findViewById(R.id.card_view);
            person_name = (TextView) view.findViewById(R.id.person_name);
            person_email = (TextView) view.findViewById(R.id.person_email);
        }

编辑

因为你的父布局是 CardView 你可以直接设置监听器而不用初始化

例子

public ContactViewHolder(View view, Context ctx, ArrayList<Contact> contacts) {
    super(view);
    view.setOnClickListener(this);
    this.contacts = contacts;
    this.ctx = ctx;
    person_name = (TextView) view.findViewById(R.id.person_name);
    person_email = (TextView) view.findViewById(R.id.person_email);
}

确保使用卡片视图的前景和 cardBackgroundColor

  <android.support.v7.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="true"
                android:foreground="?android:attr/selectableItemBackground"
                app:cardBackgroundColor="@color/blue">

这看起来很奇怪但确实有效,因为没有它 cardview 不会触发 OnClickEvent。

我进行了测试,只有 app:cardBackgroundColor="@color/blue" 才有效!!!

我在项目视图中删除 android:clickable="true" 时解决了同样的问题。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/item_dashboard_size"
    android:layout_margin="@dimen/item_dashboard_margin">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?selectableItemBackground"
        android:padding="@dimen/item_dashboard_padding">
    .....
</android.support.v7.widget.CardView>