将一些xml属性添加到cardview后,ViewHolder ItemView clickListener无法正常工作

时间:2017-12-19 14:40:04

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

ViewHolder itemView.OnClickListener(this)第一次完美运行,单击itemView时调用此方法 onListClick.onSectionListItemClick 。但是现在当我在cardview部分向ItemViewHolder布局中添加了一些这些属性时,itemView.OnClickListener(this)无效, onListClick.onSectionListItemClick()方法未被调用。那有什么,我想念?请检查一下。

item_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0.5dp"
        card_view:cardElevation="5dp"
        android:focusable="true"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground">

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

            <TextView
                android:id="@+id/item_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:textColor="@color/primary"
                android:textSize="16sp"
                android:gravity="left"
                android:text="@string/app_name" />

            <ImageView
                android:id="@+id/item_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:background="@color/transparent"
                android:src="@drawable/ic_arrow"
                android:tint="@color/grey" />

            <ImageButton
                android:id="@+id/item_done_indicator"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginEnd="19dp"
                android:layout_marginRight="19dp"
                android:background="@color/transparent"
                android:drawablePadding="50dp"
                android:padding="20dp"
                android:src="@drawable/ic_check"
                android:tint="@color/primary" />

        </RelativeLayout>

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

</LinearLayout>

ItemViewHolder.java

class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView sectionItemTitle;
    private ImageButton sectionItemDoneIndicator;

    ItemViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        sectionItemTitle = itemView.findViewById(R.id.item_title);
        sectionItemDoneIndicator = itemView.findViewById(R.id.item_done_indicator);
    }

    @Override
    public void onClick(View v) {

        onListClick.onSectionListItemClick(getAdapterPosition());

    }
}

0 个答案:

没有答案