Cardview Inside Recyclerview Has Extra Margins

CardView inside RecyclerView has extra margins

did you check if it is margin or padding? (Dev Options / show layout bounds)

CardView adds padding in platforms pre-L to draw shadows. In L, unless you set useCompatPadding=true, there should not be any gap.

Adding negative margins (although it is ugly) should work. If it is not, please add some more code on how you are adding them and how you are setting up the RecyclerView.

Android CardView inside RecyclerView - cardElevation cause unwanted margin & bottom shadow

My raw guess is, you are using CardView from support library. There are different card view implementation under different version of of Android.

I believe before Android L, the library add padding in order to reserve space for shadow drawing.

Please check CardView inside RecyclerView has extra margins for more details.

Android RecyclerView with CardView: Margins between cards

Try this .To card view layout apply these

android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"

And to recyclerview add padding

android:paddingTop="4dp"
android:paddingBottom="4dp"

how i can delete extras margin in CardView inside RecyclerView

I solved my problem, the size the images it's very big, i use image small and work fine.

Result

Sample Image

Unable to remove extra space between CardView items - Scrollable RecyclerView

I have not implemented a RecyclerView, however I have replicated two rows in a linear layout, minus the padding from your original question:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:background="#303030"
android:id="@+id/cv1"
card_view:cardElevation="5dp"
android:foreground="?android:attr/selectableItemBackground"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="4dp"
android:longClickable="true"
android:background="#303030">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/person_photo"
android:background="@drawable/vector_red"
android:layout_alignBottom="@+id/txtSub" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/txtMain"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/person_photo"
android:layout_toEndOf="@+id/person_photo"
android:elevation="4dp"
android:textSize="20dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/txtSub"
android:layout_below="@+id/txtMain"
android:layout_toRightOf="@+id/person_photo"
android:layout_toEndOf="@+id/person_photo" />

</RelativeLayout>

</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:background="#303030"
android:id="@+id/cv1"
card_view:cardElevation="5dp"
android:foreground="?android:attr/selectableItemBackground"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="4dp"
android:longClickable="true"
android:background="#303030">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/person_photo"
android:background="@drawable/vector_red"
android:layout_alignBottom="@+id/txtSub" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/txtMain"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/person_photo"
android:layout_toEndOf="@+id/person_photo"
android:elevation="4dp"
android:textSize="20dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/txtSub"
android:layout_below="@+id/txtMain"
android:layout_toRightOf="@+id/person_photo"
android:layout_toEndOf="@+id/person_photo" />

</RelativeLayout>

</android.support.v7.widget.CardView>

</LinearLayout>

Play around with the padding and you will get better results. You can also try adding a background color to your card, then you can get rid of layout margins in your RelativeLayout!

How do I decrease the space between cards in a CardView within a RecyclerView?

Edit these attributes

card_view:cardMaxElevation="1dp"
card_view:cardElevation="1dp"

so full code will be

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

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="200dp"
card_view:cardCornerRadius="5dp"
card_view:cardMaxElevation="1dp"
card_view:cardElevation="1dp"
card_view:cardBackgroundColor="@color/colorPrimary"
card_view:cardUseCompatPadding="true"
card_view:cardPreventCornerOverlap="false"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/card_list">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/bag"
android:contentDescription="string/video_thumbnail_description"
android:layout_weight="1"
/>

<TextView
android:id="@+id/video_title_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="lovelksdjslkdjlsdj"
android:layout_weight="3"
/>
</LinearLayout>
</android.support.v7.widget.CardView>


Related Topics



Leave a reply



Submit