在RecyclerView中回收时,项目视图无法正确测量

loe*_*chg 2 android android-linearlayout android-recyclerview

我有一个场景,当视图被回收时,布局是不正确的.我的行项目布局如下(注意wrap_contentLinearLayout宽度weight,,和ellipsize):

     <LinearLayout
          android:id="@+id/header"
          android:layout_width="wrap_content"       
          android:layout_height="wrap_content"    
          android:orientation="horizontal"
          >
        <TextView
            android:id="@+id/text1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"               
            android:ellipsize="middle"
            android:singleLine="true"
            tools:text="Ann really freaking long last name Droid"
            />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
      </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

期望的行为

    row1 | 1-short-text | 2-text | 3-more-text
    ----------------------------------------------------
    row2 | 1-short-text | 2-text | 3-more-text
    ----------------------------------------------------
    row3 | 1-long-ellipsiz...text | 2-text | 3-more-text
    ----------------------------------------------------
    row4 | 1-tiny | 2-text-longer | 3-more-text-blah
    ----------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

实际行为

    row1 | 1-...et | 2-text | 3-more-text     <-- ellipsized unnecessarily
    ----------------------------------------------------
    row4 | 1-short-text | 2-text | 3-more-text
    ----------------------------------------------------
    row2 | 1-long-ellipsiz...text | 2-text | 3-more-text
    ----------------------------------------------------
    row3 | 1-tiny |    2-text-longer | 3-more-text-blah  <-- spacing
    ----------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

解决这个布局问题的最佳方法是什么?注意:它在4.1.1模拟器中工作正常.

loe*_*chg 11

有一个已知问题,TextViews在这种情况下不会重新测量.在ViewHolder中设置数据时,我可以通过执行以下操作来解决此问题:

text1.setText("some-dynamic-text");
text1.requestLayout();  // <-- This is key... requestLayout after setting text.
Run Code Online (Sandbox Code Playgroud)

当视图被回收并且设置了新文本时,它没有正确布局.明确要求重新布局似乎正确地重新计算.

  • 可以提供有关该问题的更多信息吗?你在哪里读过它或什么的? (2认同)