Строки Recyclerview сокращаются после изменения notifydatase или после notifyItemChanged (position) - PullRequest
0 голосов
/ 22 апреля 2020

У меня есть обзор переработчика, который принимает Arraylist в качестве параметра. И когда я добавляю uri-изображение к одной из переменных объекта списка, а затем обновляю recylerview, используя

rv.notifydatasetchanged

, тогда мой код правильно добавляет изображение в view-изображение объекта, переменную uri которого я установил, но все строки сжимаются

, а когда я делаю

rv.notifyItemChanged(position)

, то эта строка сжимается. И остальное остается таким же. Я не изменяю ширину или высоту после refre sh или notifydatasetchange, поэтому я запутался, почему это происходит.

Вот моя строка recylverview:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

        <variable
            name="feedbackData"

            type="in.myapp.event_subscriber.models.MyData" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">

        <LinearLayout

            android:id="@+id/ll_question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_question_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:text="@{Integer.toString(feedbackData.questionNumber)}"
                android:textColor="@color/black"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toStartOf="@id/tv_question" />

            <TextView

                android:id="@+id/tv_question"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="40dp"
                android:text="@{feedbackData.question}"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/tv_question_number"
                app:layout_constraintTop_toTopOf="parent" />

        </LinearLayout>

        <EditText
            android:id="@+id/et_answer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="20dp"
            android:background="@drawable/stroke_rectangle_grey"
            android:gravity="start"
            android:hint="Write you answer here..."
            android:lines="4"
            android:paddingStart="10dp"
            android:paddingTop="15dp"
            android:text="@{feedbackData.answer.description}"
            app:layout_constraintTop_toBottomOf="@id/ll_question" />


        <LinearLayout
            android:id="@+id/ll_action_attachment"
            goneUnless="@{feedbackData.imageUri==null}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="20dp"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/et_answer">

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/ic_attachment" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:text="Add attachment"
                android:textSize="16sp" />

        </LinearLayout>


        <ImageView
            android:id="@+id/iv_attached_image"
            attachImage="@{feedbackData.imageUri}"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="10dp"
            android:elevation="10dp"
            android:scaleType="fitXY"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_image_attached_text"
            goneUnless="@{feedbackData.imageUri!=null}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="5dp"
            android:text="Image Attached Successfully"
            android:textColor="@color/medium_aqua_marine"
            android:visibility="gone" />

        <View
            android:layout_width="match_parent"
            android:layout_height="40dp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:background="@color/grey" />
    </LinearLayout>
</layout>

Before adding image to row

After adding image to row

Первое изображение показывает мое представление defualt recycler, но как только я добавляю изображение URI из моего локального хранилища в переменную списка и затем вызовите notifydatasetchanged или notifyItemChanged (position), что происходит на втором изображении. Все сжимается, даже текстовые просмотры и разделители, и все сжимается.

Код для моего recylerview:

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

    <TextView
        android:id="@+id/tv_event_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="20dp"
        android:background="@color/white"
        android:text="Technical corridor"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="20dp"
        android:background="@color/grey"
        app:layout_constraintTop_toBottomOf="@id/tv_event_name" />




    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_subscriber_feedback"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        app:layout_constraintBottom_toTopOf="@id/btn_submit"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/divider"
        tools:listitem="@layout/row_feedback" />

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_marginBottom="40dp"
        android:background="@color/colorPrimary"
        android:text="Submit"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Ответы [ 2 ]

0 голосов
/ 22 апреля 2020

Добавление изображения, для которого я устанавливал изображение, и текста, который был виден только после того, как изображение было установлено внутри еще одного сработавшего линейного макета. Теперь его не уменьшается. Может быть, теперь только это линейное расположение сокращается до ширины изображения. Я не знаю, почему это происходит. Но, по крайней мере, это сработало и может быть использовано на данный момент.

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            goneUnless="@{feedbackData.imageUri!=null}"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/iv_attached_image"
                attachImage="@{feedbackData.imageUri}"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="10dp"
                android:elevation="10dp"
                android:scaleType="fitXY"
                android:visibility="gone" />

            <TextView
                android:id="@+id/tv_image_attached_text"
                goneUnless="@{feedbackData.imageUri!=null}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="5dp"
                android:text="Image Attached Successfully"
                android:textColor="@color/medium_aqua_marine"
                android:visibility="gone" />
        </LinearLayout>
0 голосов
/ 22 апреля 2020

Я попытаюсь дать некоторые подсказки, возможно, потому, что recyclerView пересчитывает представления:

1) для вашего представления переработчика setHasFixedSize(true)

2) установите LinearLayoutManager для вашего реселлера.

3) продолжайте использовать notifyDataSetChanged()

4) пусть ваш обзор переработчика будет match_parent оба ширина и высота

Посмотрите, что произойдет.

...