Recyclerview行在notifydatasetchanged或notifyItemChanged(position)之后变小

问题描述 投票:0回答:2

我有一个recyclerview,它以Arraylist作为参数。当我将图像uri添加到列表的对象变量中,然后刷新recylerview时使用

rv.notifydatasetchanged

然后我的代码将图像正确地添加到我设置了uri变量但所有行都缩小的对象的图像视图中

以及我在做什么

rv.notifyItemChanged(position)

然后该行缩小。和其他保持不变。在刷新或notifydatasetchanged之后,我没有修改宽度或高度,这就是为什么我感到困惑的原因。

这是我的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

第一张图片显示了我的默认recyclerview,但是一旦我将本地存储中的图片uri添加到list变量,然后调用notifydatasetchanged或notifyItemChanged(position),第二张图片就会发生。一切都缩小了,即使是textviews和divider也缩小了。

我的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>
android kotlin android-recyclerview recycler-adapter android-xml
2个回答
0
投票

我将尝试给出一些提示,可能是因为recyclerView重新计算视图:

1]用于您的回收者视图setHasFixedSize(true)

[2)将LinearLayoutManager设置为您的recyclerView

3)继续使用notifyDataSetChanged(

[4)让您的回收站视图的宽度和高度均为match_parent

看看会发生什么。


0
投票

[添加我正在为其设置图像的imageview和仅在将图像设置为一个线性布局后才可见的textview。现在它没有缩小。也许现在只有linearlayout会缩小到imageview的宽度。我不知道为什么会这样。但是,至少这项工作有效,现在可以使用。

<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>
© www.soinside.com 2019 - 2024. All rights reserved.