3
上CardView没有点击效果

我使用的是RecyclerViewGridLayoutManager和我动态添加具有此XML的新元素:在RecyclerView

<?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项目的影响,只有当我双击它或长时间点击。

我试图把里面的CardViewLinearLayout使点击与android:background="?android:attr/selectableItemBackground"但结果保持不变

+1

问题只有通过与GridView控件取代RecyclerView解决。 –

+0

但是如果我想使用RecyclerView? – temirbek

回答

1

我已经在我的项目设置背景,以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

+0

没有在我的工作 – temirbek