0

My recycler view implementation seems to align properly to the top if the data source contains more than one data, but with only one data the item centers vertically.

How can I prevent this?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent""
        tools:showIn="@layout/activity_home">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/listview_jobs_list"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginRight="8dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:foregroundGravity="top"
            />
    </android.support.constraint.ConstraintLayout>

</LinearLayout>
Jojo Narte
  • 2,767
  • 2
  • 30
  • 52

1 Answers1

0

you might give height to your recyclerView "match_parent" and to your ConstraintLayout "wrap_content". by this your recyclerView will set its height to LinearLayout:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        tools:showIn="@layout/activity_home">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/listview_jobs_list"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:foregroundGravity="top"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

</LinearLayout>

try this and let me know about the result

Meikiem
  • 1,876
  • 2
  • 11
  • 19
  • matching a parent that is wrapping its content creates an infinite loop and the Android will break down with sparks and smoke, well it isn't the right thing to do in layouts. – straya Feb 18 '21 at 10:55