CardView的OnClickListener无法运行android?

时间:2016-02-18 04:32:25

标签: android onclicklistener android-adapter android-recyclerview android-cardview

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

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

列表已完美显示,但是当我点击特定联系人时,点击事件无效。

我还在ContactAdapter类的CardView和OnClickListener事件上使用了clickable true。

我做了什么:

我有三个布局activity_main,contact_list,contact_detail。

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

我有三个java类: 联系 - 我用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_layout代码:

<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类代码:

MainActivity Code:

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

5 个答案:

答案 0 :(得分:6)

我在项目视图中移除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>

答案 1 :(得分:2)

从您的代码中,您错过了为您的cardview设置监听器。要触发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);
}

答案 2 :(得分:2)

确保使用卡片视图的前景和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 =&#34; @ color / blue&#34;工作!!!

答案 3 :(得分:1)

不是在 ContactViewHolder 类(适配器内的私有静态类)中实现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);
    }

}

答案 4 :(得分:0)

请参阅此处:https://github.com/codepath/android_guides/wiki/Using-the-RecyclerView 您可以为旧代码获得更漂亮的代码。