2

I am implementing an interface that stacks CardViews in a RecyclerView, and my desired result is similar to Inbox by Gmail. As you can see in this screenshot, it works great on 5.0+! This is exactly what I want.

5.0+ stacked CardViews (what I want across all API versions)

But 4.4 and earlier do not have access to the android:elevation attribute, so there's a visual separation between the elements.

4.4- stacked CardViews (do not want)

Now, there's an attribute android.support.v7.cardview:cardUseCompatPadding that will make API 21+ work like API 20 and below, but I want the opposite (to have 4.4 behave like 5.0). How can I do this?

ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107

1 Answers1

2

You can use a FrameLayout or a RelativeLayout as the parent of your item views to get that consistent look. If your item layout looks like the following:

<CardView android:layout_width="match_parent"
          android:layout_height="match_parent">

        ...
 </CardView>

You can just replace the CardView element with a FrameLayout:

 <FrameLayout android:layout_width="match_parent"
              android:layout_height="match_parent">
        ...
 </FrameLayout>

In RecyclerView.Adapter.onCreateViewHolder, you will return the latter item layout.

WindsurferOak
  • 4,861
  • 1
  • 31
  • 36
  • You mean something like this? `RecyclerView` -> `FrameLayout` -> `CardView`? Where each `FrameLayout` that contains a `CardView` is a child of the `RecyclerView`? – ZakTaccardi Aug 31 '15 at 23:11
  • You would just toss out the CardView all together. There's no need to use a CardView here because it seems that you are trying to get the visual feel of a tile: https://www.google.com/design/spec/components/lists.html#lists-usage. I just updated my answer. – WindsurferOak Sep 01 '15 at 00:22
  • But I want the elevation that comes with a cardview. If you put Inbox in landscape on a phablet or tablet, you can see that each view is a card and has elevation, even pre 4.4. – ZakTaccardi Sep 01 '15 at 02:57
  • You can use a background that will give you a similar effect. I found this post: http://stackoverflow.com/a/30962710/361902 that could help you with it. – WindsurferOak Sep 01 '15 at 17:51