Вертикально центрированные горизонтальные элементы LinearLayoutManager - PullRequest
0 голосов
/ 11 июля 2019

Я бы хотел центрировать элементы внутри recyclerView, в которых LayoutManager ориентирован по горизонтали как LinearLayoutManager.

Это пример моего кода:

LinearLayoutManager lm = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); 

RecyclerView.ItemDecoration decoration = new WidgetMarginItemDecoration(getContext());
                       recyclerView.setNestedScrollingEnabled(true);
                       recyclerView.addItemDecoration(decoration);
                       recyclerView.setAdapter(adapter);
                       recyclerView.setLayoutManager(m.get(0).getGroupName() == null || m.get(0).getGroupName().isEmpty() ? ngm : lm);

Макет recyclerView:

<androidx.recyclerview.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="match_parent"
  /> 

Изображение показывает, что я хочу: выше красного делителя - то, что я имею сейчас, ниже красного делителя - то, чего я хочу достичь

enter image description here

И один из многих элементов макета:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="32dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
...