在RecyclerView

时间:2015-08-30 12:52:46

标签: android android-layout android-recyclerview android-cardview gridlayoutmanager

我正在使用RecyclerView GridLayoutManager并动态添加包含此xml的新元素:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true">

    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        style="@style/cardView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">

            <ImageView
                android:id="@+id/direction_list_icon"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:src="@drawable/icon"
                android:layout_gravity="center"
                android:padding="8dp"
                android:clickable="true" />

            <TextView
                android:id="@+id/direction_list_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Dummy text"
                android:layout_gravity="center"
                android:padding="8dp"
                android:clickable="true" />

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

问题是,只有当我双击它或长按一次时,我才会在CardView项目上看到点击效果。

我尝试在LinearLayout内放置CardView并使用android:background="?android:attr/selectableItemBackground"点击,但结果保持不变

2 个答案:

答案 0 :(得分:2)

我通过将背景设置为CardView的根元素,在我的项目中解决了这个问题。在您的布局中,这应该有效:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    style="@style/cardView">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:background="?android:selectableItemBackground">

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

答案 1 :(得分:0)

设置

android:focusable="false"
android:focusableInTouchMode="false"
在CardView上