如何在cardView中实现onclick侦听器的视图?

时间:2016-03-10 07:18:50

标签: android android-cardview

android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
cardView:cardUseCompatPadding="true"
cardView:contentPadding="5dp"
cardView:elevation="5dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="10 dead in bombarding"
        android:textAppearance="@android:style/TextAppearance.Medium"
        android:textColor="@android:color/black"
        android:textStyle="bold" />

    <!--<ImageView-->
    <!--android:id="@+id/imageView"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="match_parent"-->
    <!--android:layout_below="@+id/title"-->
    <!--android:padding="5dp" />-->
    <!--<?xml version="1.0" encoding="utf-8"?>-->
    <com.facebook.drawee.view.SimpleDraweeView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:fresco="http://schemas.android.com/apk/res-auto"
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        fresco:actualImageScaleType="fitXY"
        fresco:progressBarAutoRotateInterval="1000"
        fresco:progressBarImage="@drawable/icon"
        fresco:retryImage="@drawable/retry"
        fresco:retryImageScaleType="centerCrop" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal"
        android:weightSum="10">

        <ImageView
            android:id="@+id/shareViaWhatsApp"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:foregroundGravity="left"
            android:layout_weight="1"
            android:src="@drawable/whatsapp" />

        <TextView
            android:id="@+id/published_date"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="9"
            android:gravity="end|center_vertical"
            android:text="published date"
            android:textAppearance="@android:style/TextAppearance.Small"
            android:textColor="@color/colorDate" />

    </LinearLayout>
</LinearLayout>

以上代码适用于Recycler视图中的单个项目(在我的情况下为shareViaWhatsApp)。我在recylcer视图的视图持有者上实现了onclick侦听器,但onClick仅转到cardView,而不是外部的项目。

由于

1 个答案:

答案 0 :(得分:0)

在View Holder类中实现onClicklistener

public static class ViewHolder extends RecyclerView.ViewHolder {
    public View view;
    public Item currentItem;

    public ViewHolder(View v) {
        super(v);
        view = v;
        view.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                // Handle click event
            }
        });
    }
}

@Override public void onBindViewHolder(ViewHolder viewHolder, int i) {
    viewHolder.currentItem = items.get(i);
}