首页 > 解决方案 > 如何将水平 RecyclerView 对齐到底部?

问题描述

如何将水平 RecyclerView 对齐到底部?例如,我有一个看起来像这样的水平RecyclerViewLinearLayoutManager在此处输入图像描述 我希望所有 RecyclerView 项目都与底线对齐,如下所示: 在此处输入图像描述 有什么方法可以做到这一点?

UPD 这是我的项目布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <ImageView
        android:id="@+id/columnImageView"
        android:layout_width="@dimen/column_width"
        android:layout_height="match_parent"
        android:layout_marginHorizontal="@dimen/spacing_xs"
        android:src="@drawable/graph_rectangle_white_b"
        android:contentDescription="@null"
        tools:layout_height="@dimen/spacing_3xl"
        tools:visibility="visible"
        tools:src="@drawable/graph_rectangle_blue_a" />

</layout>

这就是我的 RecyclerView 的布局:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <merge 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="wrap_content"
        android:background="@color/grey_200">

        ...

        <View
            android:id="@+id/line5"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:background="@color/grey_400"
            app:layout_constraintBottom_toBottomOf="@id/main"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/chartRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:overScrollMode="never"
            android:layout_marginHorizontal="@dimen/spacing_xs"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintBottom_toBottomOf="@id/line5"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_alignBottom="@id/line5"
            tools:layout_height="wrap_content"
            tools:itemCount="5"
            tools:listitem="@layout/chart_column_item"
            tools:spanCount="5" />

    </merge>
</layout>

标签: androidandroid-recyclerview

解决方案


I had your same problem, and I resolved setting in onBindViewHolder the height of the item programmatically like this:

holder.itemView.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT)

Note that this works if Views in your ViewHolder have fixed height.

Another solution would be to calculate at runtime the margin_top and apply it to holder.itemView


推荐阅读