RecyclerView 结合 CardView 使用(二)

时间:2023-03-09 05:56:20
RecyclerView 结合 CardView 使用(二)

上一篇的基础上,修改了,CardView的布局和点击效果

总结:

CardView的奇葩属性 :app:cardPreventCornerOverlap="false" 和园角边框重叠的效果;

实现点击事件是CardView的子布局!

注意:一个细节,没明白。

在CardView控件的属性下,android:foreground="?android:attr/selectableItemBackground"
但是其他控件实现点击水波纹效果的是 android:background="?android:attr/selectableItemBackground"

1. RecyclerViewAdapter.java

RelativeLayout cardView;//修改
ImageView news_photo;
TextView news_title;
TextView news_desc;
Button share;
Button readMore; public NewsViewHolder(final View itemView) {
super(itemView);
cardView= (RelativeLayout) itemView.findViewById(R.id.card_view);

2. news_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
> <android.support.v7.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
app:cardCornerRadius="5dp"
app:cardPreventCornerOverlap="false"
app:elevation="2dp"
> <RelativeLayout
android:id="@+id/card_view"
android:background="?android:attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/news_header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/news_photo"
android:scaleType="centerCrop"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="150dp"/>
<TextView
android:id="@+id/news_title"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:maxLines="1"
android:textSize="20sp"
android:padding="5dp"
android:textColor="#ffffff"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
            <TextView
android:id="@+id/news_desc"
android:maxLines="2"
android:layout_below="@+id/news_header"
android:layout_margin="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_below="@+id/news_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_share"
android:text="SHARE"
android:background="#00000000"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_more"
android:background="#00000000"
android:textColor="#7AD3E0"
android:text="READ MORE"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> </RelativeLayout> </android.support.v7.widget.CardView> </RelativeLayout>