android - CardView 类似于 Google Play 使用的那些

标签 android android-layout android-cardview

enter image description here

我想问一下是否有一种相对简单的方法可以让我在 RecyclerView 中的 CardView 表现得像 Google Play 使用的那些(上图)。此 CardView 具有以下特征:

  • 最上面和最下面的卡片的圆角半径应用于各自的位置。 (底部卡片有阴影,就像具有足够高度的普通 CardView 一样。

  • 其余项目的角根本没有圆角,项目之间只有一点缝线。

最佳答案

您可以通过用卡片 View 包装您的回收器 View 而不是包装每个单元格来轻松地做到这一点。

 <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="6dp">

    <android.support.v7.widget.RecyclerView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>
</android.support.v7.widget.CardView>

您还需要一个绘制分隔线的装饰器。

public class LineItemDecoration extends android.support.v7.widget.DividerItemDecoration {

   public LineItemDecoration(Context context, int orientation, @ColorInt int color) {
       super(context, orientation);

       setDrawable(new ColorDrawable(color));
   }

   public LineItemDecoration(Context context, int orientation, @NonNull Drawable drawable) {
       super(context, orientation);

       setDrawable(drawable);
   }
}

这是可绘制的线

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="60dp">
        <shape>
            <solid android:color="@color/colorDivider" />
            <size android:height="1dp" />
        </shape>
    </item>
</layer-list>

你可以用like;

mRecycler.addItemDecoration(new LineItemDecoration(getContext(), DividerItemDecoration.VERTICAL, ContextCompat.getDrawable(getContext(), R.drawable.divider_cell)));

更新

对于涟漪效果,您可以将其添加到单元格的 Root View 的背景中。

android:background="?selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"

关于android - CardView 类似于 Google Play 使用的那些,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46883335/

相关文章:

android - 清除应用程序数据库

android-studio - 在列和行下对齐卡片中的文本

Android - 布局不统一

android - 在android中实现推送通知

android - 如何使用 box2d 在 Libgdx 中使重力居中

android - 如何将多个 ui 元素合并到 android 中的 ListView 中?

android - 为什么我的卡片 View 被剪裁

android - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.Long 键入 com.realty.drake.kunuk.Property

android - 改变卡片的背景 (Android cardView)

java - onActivityResult 没有给出任何结果