如何将Horizo​​ntalView的onclick事件传递给它的普通CardView

时间:2015-11-27 15:04:37

标签: android onclicklistener horizontalscrollview android-cardview

CardView作为行项目添加到我的RecyclerView中。当我单击除了Horizo​​ntalScrollView及其子视图之外的CardView内的任何视图时,CardView的OnClickListener工作正常。但是,在单击Horizo​​ntalScrollView及其中的子视图时,无法触发CardView的OnClickListener。

cardview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/license_plate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge"
                tools:text="P3R-178" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical|center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/vehicle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:drawableTop="@drawable/scooter_selected"
                    android:gravity="center"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <HorizontalScrollView
        android:id="@+id/horizontal_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/area_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="10dp">

        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView licensePlate;
        public TextView vehicle;
        public LinearLayout area;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            licensePlate = (TextView) itemView.findViewById(R.id.license_plate);
            vehicle = (TextView) itemView.findViewById(R.id.vehicle);
            area = (LinearLayout) itemView.findViewById(R.id.area_container);
        }

        @Override
        public void onClick(View v) {
            Log.w(Init.TAG, "licensePlate: " + licensePlate.getText().toString());
            Log.w(Init.TAG, "vehicle: " + vehicle.getText().toString());
        }

}

0 个答案:

没有答案