Search code examples
androidandroid-layoutuser-interfaceandroid-fragmentslistviewitem

Strange rendering of my listItems in a recyclerview


I can't figure out why my listItem's are rendering in a strange way. I have checked the constraints and everything seems fine. Here is a picture of what I'm getting at display time vs run time:

enter image description here

enter image description here

Here is my XML for the layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list_item_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <ImageView
        android:id="@+id/customer_picture"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        app:srcCompat="@mipmap/ic_launcher"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"/>

    <TextView
        android:id="@+id/customer_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toRightOf="@+id/customer_picture"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="First Name:    Last Name:"/>

    <TextView
        android:id="@+id/customer_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        tools:text="1543 W Kendall Blvd"
        app:layout_constraintLeft_toRightOf="@+id/customer_picture"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.425"
        android:layout_marginBottom="8dp"/>
</android.support.constraint.ConstraintLayout>

Here is the container XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/customer_recycler_view"
        android:layout_width="0dp"
        android:layout_height="380dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="65dp"/>
</android.support.constraint.ConstraintLayout>

I'm also placing the container view (a Fragment) into a containing Activity. I don't know if it helps but here is that Activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bignerdranch.android.personaltrainer.ListActivity">

    <LinearLayout
        android:layout_width="304dp"
        android:layout_height="wrap_content"
        android:id="@+id/logged_in_fragment_container"
        android:orientation="horizontal"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        tools:layout_height="18dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/list_fragment_container">

    </LinearLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/list_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="34dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

I can post my Adapter and ViewHolder classes for the RecyclerView but it's probably not necessary


Solution

  • Something that stands out to me is that the RecyclerView has a fixed height and no width.

    <android.support.v7.widget.RecyclerView
        android:id="@+id/customer_recycler_view"
        android:layout_width="0dp"
        android:layout_height="380dp"
    

    Based on this, it seems your views are being expanded within the list

    Alternatively, you can set this in the root element of the row item layout

    android:height="?android:attr/listPreferredItemHeight"