【发布时间】:2015-07-13 15:00:06
【问题描述】:
我有一个 DialogFragment 显示包含列表视图的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="20dp">
<View
android:id="@+id/first_divider"
style="@style/Divider"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="@style/ListView"/>
<View
android:id="@+id/second_divider"
style="@style/Divider" />
<TextView
android:id="@+id/textview_error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:textColor="@color/red"
android:layout_marginTop="10dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:text="@string/ui_alertdialog_checkable_list_dialog_no_item_selected_error"/>
</LinearLayout>
我在 onCreateDialog 方法中以这种方式扩展我的布局:
View contentView = getActivity().getLayoutInflater().inflate(R.layout.ui_alertdialog_checkable_list, null);
它正在工作,但我的列表视图的适配器调用 getView 太多次,调用 notifyDatasetChanged 真的很慢。这是由我膨胀布局的方式引起的:我没有将它附加到父视图,因此 Android 似乎无法计算列表视图的高度,因此使用 getView 创建了很多视图。
如果我设置了一个固定的高度,一切都会正常工作,但我做不到。将高度设置为 MATCH_PARENT 也不起作用。
有人知道如何使用自定义布局和正常工作的列表视图来创建对话框吗?
【问题讨论】:
-
getView() 被调用的行数与 ListView 中的行数一样多
-
应该,但在我的情况下,它被称为每行 4-5 次。如果您使用 WRAP_CONTENT 高度定义列表视图,则会出现同样的问题:Android 无法计算高度并且必须创建很多行。请参阅:stackoverflow.com/questions/2618272/…
-
“fill_parent”呢?
-
fill_parent = match_parent(已弃用),它也不起作用。
标签: android listview android-fragments dialogfragment getview