我有一个使用自定义列表项 View 的自定义 ListView 适配器。自定义列表项 View 的左侧有一个 ImageView ,其中有两个 TextView 的堆栈,主标题位于顶部,副标题位于右侧。我想要发生的是,如果没有要显示的副文本,则让副标题消失,并让正文填充空间。
我已经针对 16 到 23 之间的所有其他手机/平板电脑 API 进行了测试。一切都按预期工作,除了当我在运行 Android 4.3.1 (API 18) Android 4.4.2 (API 19) 的设备上运行时。针对 API 18 或 19 运行,副标题 View 可见性确实设置为 View.GONE,但主标题 View 不会调整大小以占用空间。
有人知道这是怎么回事吗?
这是列表项的布局文件:
list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="@+id/list_item_snapshot_imageview"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/medium_grey"
android:contentDescription="@string/item_snapshot" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@id/list_item_snapshot_imageview"
android:layout_alignTop="@id/list_item_snapshot_imageview"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="@id/list_item_snapshot_imageview"
android:layout_toRightOf="@id/list_item_snapshot_imageview"
android:orientation="vertical">
<TextView
android:id="@+id/list_item_title_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20sp"
tools:text="Main Text" />
<TextView
android:id="@+id/list_item_subtitle_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="14sp"
tools:text="Subtext" />
</LinearLayout>
</RelativeLayout>
这里是负责更新内容的java代码。 viewHolder
只是一个静态类,包含对 TextView 的引用,并保存在列表项上的标记中以供 View 回收。
NamedItem item = getRowItem(section, position);
if (item != null) {
viewHolder.titleTextView.setText(item.getName());
if (item.getSubtitle() != null) {
String subtitle = ((ItemInfo) item).getSubtitle();
viewHolder.subtitleTextView.setText(subtitle);
viewHolder.subtitleTextView.setVisibility(View.VISIBLE);
} else {
viewHolder.subtitleTextView.setVisibility(View.GONE);
}
}
编辑 我做了更多测试,API 18 和 19 上也发生了这种情况。我更改了其余的描述以反射(reflect)这一点。
最佳答案
明白了这一点。看起来 API 18 和 19 中处理 ListView 项 View 的方式不喜欢将relativelayout 作为自定义 ListView 项中的根布局。将整个布局包装在 LinearLayout 中解决了这个问题:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Other Views ... -->
</RelativeLayout>
</LinearLayout>
在遇到这个答案后尝试了这个:https://stackoverflow.com/a/21334982/1777839
仍然没有解释为什么会发生这种情况。
关于android - 自定义 ListView 项目 View 中的 TextView 未填充 API 18 和 19 中的父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36209034/