0

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

我怎样才能防止这种情况?

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

1 回答 1

0

您可能会为您的 recyclerView “match_parent”和您的 ConstraintLayout “wrap_content”提供高度。这样,您的 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>

试试这个,让我知道结果

于 2017-06-27T09:37:34.807 回答