当只有一个内容时,将recyclelerview项目与Top对齐

时间:2017-06-27 09:29:48

标签: android android-recyclerview linearlayoutmanager

如果数据源包含多个数据,则我的回收站视图实现似乎正确对齐到顶部,但只有一个数据项目垂直居中。

我该如何防止这种情况?

<?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>

1 个答案:

答案 0 :(得分:0)

你可以给你的recyclerView&#34; match_parent&#34;以及你的ConstraintLayout&#34; wrap_content&#34;。通过这个,您的recyclerView将其高度设置为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>

试试这个,让我知道结果