2

I want some equal space between the cardview in adapter.

How to put some space like this?

enter image description here

Here is my Xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    card_view:cardBackgroundColor="@color/White"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="2dp"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="4dp">
....

with the above code am getting like this..

enter image description here

Can somebody help me?

Rethinavel
  • 3,912
  • 7
  • 28
  • 49
  • You should give your cardLayout margin: margin:"4dp" and note elevation must be less than margin,i.e if margrin="4dp" than elevation="2dp". If problem still araise put cardview in frame layout. – Rajesh Tiwari Dec 30 '15 at 10:46
  • Are you using RecyclerView to display items? – ajantha Dec 30 '15 at 10:47

3 Answers3

0

Using RecyclerView.Adapter's getItemViewType() function with separate viewholders consisting of their own respective margins can work, but gets super messy when it comes to binding data, especially for handling input events like the Play Store app (screenshot you shared in your post) since each viewholder will have its own margins while implementing interfaces for click events.

That said, the only ideal, concise solution I've come across is to make good use of the ItemDecoration class, where you can apply offsets (margins in our case) to each inflated list item accordingly, and then add it to your RecyclerView. As mentioned in the docs:

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more.

A sample implementation can be found here.

DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
-1

Wrap your CardView in LinearLayout and set padding attribute to LinearLayout.

Evgeniy Mishustin
  • 3,343
  • 3
  • 42
  • 81
-1

check cheesesquare code use below way(in your case use this in recycleview item layout)

enter image description here

<android.support.v7.widget.CardView 
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dip">

                    <LinearLayout
                        android:id="@+id/relativeLayout1"
                        style="@style/Widget.CardContent"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:orientation="vertical">
                         // add your content here!
                    </LinearLayout>
                </android.support.v7.widget.CardView>

Widget.CardContent

<style name="Widget.CardContent" parent="android:Widget">
        <item name="android:paddingLeft">10dp</item>
        <item name="android:paddingRight">10dp</item>
        <item name="android:paddingTop">12dp</item>
        <item name="android:paddingBottom">12dp</item>
        <item name="android:orientation">vertical</item>
    </style>
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • This doesn't answer the question - I'm looking at the Cheesesquare sample app right now, and the XML layout shows that each card is hardcoded (with respective margins applied within each CardView) as opposed to rendering the cards via a RecyclerView. – DaveNOTDavid Jan 30 '18 at 01:46