【发布时间】:2015-02-08 21:53:07
【问题描述】:
任何人都可以阐明在 Android 中为 ListView 页脚充气的正确方法吗?
根据我的理解,使用 inflate 的正确方法是至少传递两个参数,即要膨胀的资源和根 ViewGroup。或者,可以传入第三个 bool 参数,指示资源是否应附加到 ViewGroup。
现在,如果我尝试以这种方式填充页脚,它不起作用(页脚不会出现在 UI 中)。
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, getListView(), false);
getListView().addFooterView(footerView);
基本上,它的唯一工作方式是将 null 作为 ViewGroup 传递。
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null, false);
getListView().addFooterView(footerView);
但是,根据我所阅读的所有内容,这被认为是不好的做法。因此,问题是:膨胀 ListView 页脚的正确方法是什么?
谢谢。
页脚视图 XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/add_new_todo_item_string"
android:textSize="24sp" >
</TextView>
我不知道为什么,但是这两个版本现在都可以在模拟器中运行...... 抱歉有任何混淆。
【问题讨论】:
-
我没有尝试将布局膨胀为页脚。页脚由框架提供的
ListAdapter管理的事实可能是问题的一部分,尽管我没想到会是这样。当您使用您的第一段代码,并在层次视图或uiautomatorviewer中检查结果时,您是否得到任何线索?此外,您可以考虑粘贴footer_view布局文件的内容,以防我们提供一些想法。 -
@CommonsWare 我没有查看层次结构视图,但我会尽快查看并更新帖子。
标签: java android listview android-listview