将项目分组到CardView的最佳做法是什么?

bvi*_*iyg 36 android android-cardview android-recyclerview

CardView通常用于装饰一个元素.但有时你需要将几个项目包装到这个小部件中.例如,在收件箱应用程序中.

在此输入图像描述

那么最好的方法是什么?它可以通过自定义LayoutManager甚至自定义ItemDecoration来实现.自定义LayoutManager的实现不是一件容易的事(完全支持动画,项目装饰等).在第二个选项中,必须手动实现边界的绘制,忽略CardView(和Android-L提升)实现.

Kon*_*nov 35

TL; DR:

这不是一个 CardView它承载的元素,这是几个连续的CardView小号 有不同幅度:

对于CardView组中的顶部:

    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="0dp"
    card_view:cardCornerRadius="0dp"
Run Code Online (Sandbox Code Playgroud)

对于CardView组中的底部:

    android:layout_marginTop="0dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    card_view:cardCornerRadius="0dp"
Run Code Online (Sandbox Code Playgroud)

中间的一个,设置边距Top&Bottom为0:

    android:layout_marginTop="0dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="0dp"
    card_view:cardCornerRadius="0dp"
Run Code Online (Sandbox Code Playgroud)

关于收件箱应用:

这是app的层次结构(当然,简化了一下):

| android.support.v4.widget.DrawerLayout
--- | FrameLayout
------- | android.support.v7.widget.RecyclerView
------- | android.support.v7.widget.Toolbar
- - | android.support.design.widget.NavigationView

完整的结构,即使没有导航抽屉和折叠卡看起来像: 在此输入图像描述

当你深入了解RecyclerView物品结构时,有趣的部分开始了.
Google使用了两种类型的项目 - 分隔符(右侧有日期和操作)和卡片.即使卡片里面有不同的内容,从ViewHolder视角来看 - RecyclerView有两种类型的项目)

  1. 分隔器 在此输入图像描述

    这一次仅仅是一个LinearLayoutTextViewImageView内:

    在此输入图像描述

  2. 物品卡
    在此输入图像描述

    其布局调整基于所述内容被绑定ViewHolder .例如,像在焦点简单的电子邮件是一个CardView具有嵌套ImageView和3 TextViewS:

    在此输入图像描述

所以唯一的问题就是,Google员工如何将卡"合并"成一张大卡并避免额外的阴影.

诀窍很简单:

  1. 一切CardView都有card_view:cardCornerRadius="0dp"
  2. CardView组的有底部顶级/左/右页边距设置5DP,但0dp:

    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="0dp"
    
    Run Code Online (Sandbox Code Playgroud)
  3. CardView该组底部的左/右/底部边距设置为5dp,顶部为0dp:

    android:layout_marginTop="0dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    
    Run Code Online (Sandbox Code Playgroud)
  4. 中学CardView组的有上/下左/右页边距设置5DP,但0dp:

    android:layout_marginTop="0dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="0dp"
    
    Run Code Online (Sandbox Code Playgroud)

而已!

这是我写的一个小例子:

在此输入图像描述

布局(有棘手的边距)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    xmlns:card_view="http://schemas.android.com/apk/res-auto">
    <android.support.v7.widget.CardView
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="0dp"
        card_view:cardCornerRadius="0dp"
        card_view:contentPadding="10dp">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:text="card1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"/>
        </FrameLayout>
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="0dp"
        card_view:cardCornerRadius="0dp"
        card_view:contentPadding="10dp">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:text="card2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"/>
        </FrameLayout>
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="5dp"
        card_view:cardCornerRadius="0dp"
        card_view:contentPadding="10dp">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:text="card3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"/>
        </FrameLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我希望,这有帮助

  • @veon正确的方法是像上面所写的那样做 - 一个recyclerview,几种类型的ViewHolders,连续的CardViews"假"组行为.脏解决方案 - 将LinearLayout放入CardView并在其中动态创建行.这很糟糕,因为你的巨大CardView会被一次性回收,如果它太大了 - 它可能会导致OutOfMemory等等.就是你要杀死_RecyclerView_的所有要点.但如果你的名单**适度**大 - 你可以试试看,如果它对你有用. (3认同)
  • @thekekc尝试添加负值边距,它对我有用.机器人:layout_marginBottom = " - 5DP" (3认同)
  • 在Android 4.4.4中,这不起作用,因为卡片之间有空间布局:https://i.stack.imgur.com/pDWzl.png我在这里阅读:http://stackoverflow.com/questions/27599603/cardview-not-showing-shadow-in-android-l"CardView中的内容区域在棒棒糖前和棒棒糖设备上采用不同的大小" (2认同)