【问题标题】:Recyclerview rows gets shrinked after notifydatasetchanged or after notifyItemChanged(position)在 notifydatasetchanged 或 notifyItemChanged(position) 之后,Recyclerview 行会缩小
【发布时间】:2020-04-22 18:29:39
【问题描述】:

我有一个以 Arraylist 作为参数的 recyclerview。当我将图像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>

第一张图片显示了我的默认 recyclerview,但是一旦我将本地存储中的图片 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>

【问题讨论】:

    标签: android kotlin android-recyclerview android-xml


    【解决方案1】:

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

    1) 供您的回收站查看setHasFixedSize(true)

    2) 为您的 recyclerView 设置 LinearLayoutManager

    3) 继续使用notifyDataSetChanged()

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

    看看会发生什么。

    【讨论】:

    • 我已经有了线性布局管理器。我只使用 notifydataSetchanged 。我不能在高度上使用匹配父级,因为我需要在两个视图之间使用我的 recylerview,如果您看到顶部有一个提交按钮和一个文本。所以这就是我在那里使用约束布局的原因。我正在添加我的 recylerview 布局也有问题等待。
    • 而且,我之前在单行/项目布局中也使用了约束布局。事情变得更糟了。然后当我上传图片时,一切都在这里和那里。即使我对彼此和父母有完美的约束,但仍然。
    • 您是否将固定大小设置为 true?
    • 是的,我就是这么做的。因为这是我唯一没有按照你的建议尝试的方法,但它没有用。
    • 将recyclerView的高度设置为true能解决问题吗?我知道你不想这样,但试着看看会发生什么。
    【解决方案2】:

    添加我设置图像的图像视图和仅在图像被设置在另一个线性布局中后才可见的文本视图。现在它没有缩小。也许现在只有线性布局正在缩小到 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 2016-02-20
      • 2021-08-23
      • 1970-01-01
      • 2019-01-15
      相关资源
      最近更新 更多