RecyclerView有2列。不垂直居中

时间:2017-11-23 13:09:37

标签: android

这里我的片段设置了recyclerView:

sortRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager rvLayoutManager = new GridLayoutManager(getActivity(), 2);
sortRecyclerView.setLayoutManager(rvLayoutManager);

这里是片段布局:

<?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"
    android:background="@color/place_holder_color">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/sortRecyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

这是我的项目布局:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageViewPhoto"
        android:layout_width="@dimen/preview_image_height"
        android:layout_height="@dimen/preview_image_height"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageViewFavorite"
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:layout_margin="10dp"
        android:src="@drawable/ic_like_inactive"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/bottomContainer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/offer_rounded_corner_bottom_bg"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageViewPhoto">

        <TextView
            android:id="@+id/titleTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            app:layout_constraintEnd_toStartOf="@+id/imageViewDownload"
            app:layout_constraintStart_toStartOf="@id/bottomContainer"
            app:layout_constraintTop_toTopOf="@id/bottomContainer" />              

    </android.support.constraint.ConstraintLayout>   

</android.support.constraint.ConstraintLayout>

这里有两列结果:

rv

正如您所看到的,RecyclerView的项目左侧对齐。但我需要垂直居中。

这里有一列结果(再次没有在中心对齐)

one

2 个答案:

答案 0 :(得分:1)

试试这个

<RelativeLayout
    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"
    android:background="@color/place_holder_color">

     <android.support.v7.widget.RecyclerView
        android:id="@+id/sortRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true" />


</RelativeLayout>

像这样更改您的自定义布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageViewPhoto"
        android:layout_width="wrap_content"
        android:src="@mipmap/ic_launcher_round"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageViewFavorite"
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:layout_margin="10dp"
        android:src="@mipmap/ic_launcher_round" />

    <LinearLayout
        android:id="@+id/bottomContainer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher_round">

        <TextView
            android:id="@+id/titleTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1" />

    </LinearLayout>

</LinearLayout>

答案 1 :(得分:0)

你创建了课程&amp;扩展此RecyclerView.ItemDecoration在构造函数中传递行和项中的项目数。这样的项目之间的空间  公共类SpaceItemDecoration扩展了RecyclerView.ItemDecoration {

private int space;
private int columnSpan;
private int topSpace;

public SpaceItemDecoration(int space, int columnSpan,int topSpace) {
    this.space = space;
    this.columnSpan = columnSpan;
    this.topSpace = topSpace;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view);
    int rightPosition = position - columnSpan;
    parent.getLayoutManager();

    // Left
    if(position % columnSpan == 0){
        outRect.right = space;
    }
    // Right
    else if(rightPosition % columnSpan != 0){
        outRect.left = space;
    }       

    outRect.top = topSpace;
    outRect.bottom = topSpace;
}

}