Пространство между строками внутри Recyclerview с gridlayoutmanager перепутано - PullRequest
0 голосов
/ 29 апреля 2020

У меня есть просмотрщик с прикрепленным к нему сетевым менеджером. Столбцы расположены правильно, но строки перепутаны. Кажется, что пространство между каждой строкой меняется каждый раз, когда я прокручиваю вверх и вниз. Picture showing the problem

Здесь я настраиваю представление переработчика и менеджер компоновки сетки:

  var inflater = view.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    var rootLayout = view.findViewById<LinearLayout>(R.id.ll_linear)
    var heroListView = inflater.inflate(R.layout.hero_list_tab_card, rootLayout, true)

    //get the recycler view
    var heroListRv = heroListView.findViewById<RecyclerView>(R.id.rv_heroes)
    heroListRv.layoutManager = GridLayoutManager(view.context, 4)

Здесь hero_list_tab_card:

<?xml version="2.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

       <TextView
           android:id="@+id/tv_primary_attribute"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="30sp"
           android:textAllCaps="true"
           android:textColor="@color/colorTextWhite"
           android:text="@string/test"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintStart_toStartOf="parent"/>

        <ImageView
            android:layout_width="60dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="@id/tv_primary_attribute"
            app:layout_constraintEnd_toEndOf="parent"
            android:src="@drawable/dotabuddy_icon"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_heroes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            app:layout_constraintTop_toBottomOf="@id/tv_primary_attribute"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

Вот фактические элементы героя, которые заполнены в сетке:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/iv_heroimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dotabuddy_icon"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <TextView
        android:id="@+id/tv_heroname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorTextWhite"
        android:text="@string/test"
        app:layout_constraintTop_toBottomOf="@id/iv_heroimage"
        app:layout_constraintStart_toStartOf="@id/iv_heroimage"
        app:layout_constraintEnd_toEndOf="@id/iv_heroimage"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Пожалуйста, дайте мне знать, если вы хотите, чтобы я опубликовал любую другую информацию. Любая помощь будет принята с благодарностью.

Редактировать: для пояснения Я говорю об огромном разрыве между 4-й строкой и 5

.

Ответы [ 2 ]

1 голос
/ 29 апреля 2020

Вы также должны добавить ограничение от ImageView к вашему TextView, также, если вы хотите добавить пробел между вашим элементом, я рекомендую использовать ItemDecoration.

Поскольку вы находитесь в ConstraintLayout Вы можете указать соотношение вашего изображения, используя, например, app:layout_constraintDimensionRatio="H,16:9" или 4:3, если хотите квадрат.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/iv_heroimage"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:src="@drawable/dotabuddy_icon"
        android:scaleType="fitXY"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/tv_heroname"/>

    <TextView
        android:id="@+id/tv_heroname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorTextWhite"
        android:text="@string/test"
        android:maxLines="1"
        app:layout_constraintTop_toBottomOf="@id/iv_heroimage"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
1 голос
/ 29 апреля 2020

Я рекомендую вам исправить maxLine для этого TextView и убедиться, что изображение, которое вы загружаете в ImageView, такой же высоты, или вы должны фиксировать высоту ImageView.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...