【发布时间】:2016-08-18 20:45:25
【问题描述】:
我的 RecyclerView 项目中有两个并排的文本视图。如果视图很宽,其中一个视图具有 layout_weight="1" 以保持另一个视图可见。
问题是当视图被回收时视图大小没有更新,所以当我滚动列表时 textView id:name 宽度不正确。
我应该以某种方式强制视图更新布局吗?
我的 RecyclerView 项目:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1" />
<LinearLayout
android:id="@+id/code_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/background_code"
android:orientation="vertical">
<TextView
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:singleLine="true"/>
</LinearLayout>
</LinearLayout>
我正在使用 RecyclerView v. 23.4.0。
更新
1.我已尝试在适配器 onBindViewHolder() 的末尾调用我的 RecyclerView 项目布局根 requestLayout。
这没有任何影响,我的观点没有更新。
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
...
viewHolder.mLayoutRoot.requestLayout();
}
2. 问题可能是调用requestLayout时视图还没有完全绘制好?所以我尝试设置 GlobalLayoutListener 并在那里调用 requestLayout 。我认为这没什么帮助,但如果我有足够的项目,在我的列表中并非所有视图都已更新。 Owerall 在我的列表中我最多有 10 项。
ViewTreeObserver vto = viewHolder.mLayoutRoot.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
viewHolder.mLayoutRoot.requestLayout();
}
});
3. 我尝试使用具有shrinkColumns 属性的TableLayout 来接近类似的布局,但结果是一样的。名称文本视图宽度并不总是正确设置。另外,看起来 requestLayout 根本没有影响(或者我在错误的地方调用它?)
4. (2016 年 8 月 28 日)
为了重现这一点,我将代码精简到最低限度。
我设法使用 setIsRecyclable(false) 解决了这个问题,就像 dossik 建议的那样,但我认为这在性能方面不是很好的解决方案?
这是我的完整列表项:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_marginLeft="16dp"
android:orientation="horizontal">
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1" />
<TextView
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_stop_code_small"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:singleLine="true"
android:textSize="11sp"/>
</LinearLayout>
我的适配器代码:
我也尝试调用 invalidate(),但看不到任何影响。
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Item item = getItem(position);
NormalViewHolder viewHolder = (NormalViewHolder) holder;
viewHolder.mName.setText(item.getName());
//viewHolder.mName.invalidate(); // No help
viewHolder.mCode.setText(item.getCode());
//viewHolder.mCode.invalidate(); // No help
}
预期行为:
+----------------------------------------+
|[Name][Code] |
+----------------------------------------+
|[Name qwertyuiopasdfghjklzxcvb...][Code]|
+----------------------------------------+
如果我向上/向下滚动列表,这就是现在的工作方式。所以布局没有更新,代码也没有对齐名称。此外,名称并不总是占用足够的空间。
+----------------------------------------+
|[Name][Code] |
+----------------------------------------+
|[Name qwer...] [Code] |
+----------------------------------------+
|[Name qwertyuiopa][Code] |
+----------------------------------------+
|[Name qwe] [Code] |
+----------------------------------------+
【问题讨论】:
-
你能发布一些关于这个问题的照片/视频吗?您还可以发布您的
onBindViewHolder方法实现的完整代码吗? -
请截图?
-
为什么第二个文本视图在线性布局中?愿意为我详细说明这里的逻辑吗?
-
确保您没有调用 Recycle View 类的方法,即
setHasFixedSize(true)。建议使用false参数调用该方法。 -
我已经回答了你的问题,看看这是不是你想要做的。
标签: android android-layout android-recyclerview