【发布时间】:2016-02-25 16:11:56
【问题描述】:
我正在使用RecyclerView,其项目为CardView。我将卡片视图的背景设为android:background="?android:attr/selectableItemBackground"。
我尝试设置foreground,尝试将LinearLayout 作为父级并设置其背景等,但没有任何效果。
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<!--TextView-->
<!--Imageview-->
<!--Textview-->
</RelativeLayout>
</android.support.v7.widget.CardView>
为什么会这样?
编辑:CardView 有一个 onClickListener 可以工作。
编辑 2:我将 RelativeLayout 添加为 CardView 的父级。并使其可点击并给它一个可选择的项目背景。还是没有涟漪。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp">
<!-- ... -->
</android.support.v7.widget.CardView>
</RelativeLayout>
编辑 3:我发布了我的确切布局代码结构。我没有提到数据绑定部分。但我希望这不会阻止涟漪。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View"/>
<variable
name="viewModel"
type="com.xxx.xxx.viewmodel.genericviewmodel" />
</data>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp">
<RelativeLayout
android:id="@+id/c_open_case_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<!--TextView-->
<!--Imageview-->
<!--Textview-->
<!--TextView-->
<!--Imageview-->
<!--Textview-->
</RelativeLayout>
</android.support.v7.widget.CardView>
</layout>
【问题讨论】:
-
你从哪里得到这个布局?我的意思是来自
layout资源文件夹 -
将其设置为可点击和可聚焦。
-
@MD :是的,我是从布局资源文件夹中获取的。
-
@GergelyKőrössy :我已经尝试过可点击和可聚焦的东西。没有任何效果..
-
你用的是什么安卓版本?