DialogFragment + listview = getView 调用次数过多
DialogFragment + listview = getView called too many times
我有一个显示包含列表视图的自定义布局的 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 也不起作用。
有人知道如何使用自定义布局和正常工作的列表视图进行对话吗?
花了一段时间,但我找到了解决方案:使用 RelativeLayout 解决了问题!示例 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="match_parent"
android:layout_below="@id/first_divider"
android:layout_above="@+id/bottom_elements"
style="@style/ListView"/>
<LinearLayout
android:id="@+id/bottom_elements"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true" >
<View style="@style/Divider"/>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_textview_text" />
</LinearLayout>
</RelativeLayout>
我有一个显示包含列表视图的自定义布局的 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 也不起作用。
有人知道如何使用自定义布局和正常工作的列表视图进行对话吗?
花了一段时间,但我找到了解决方案:使用 RelativeLayout 解决了问题!示例 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="match_parent"
android:layout_below="@id/first_divider"
android:layout_above="@+id/bottom_elements"
style="@style/ListView"/>
<LinearLayout
android:id="@+id/bottom_elements"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true" >
<View style="@style/Divider"/>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_textview_text" />
</LinearLayout>
</RelativeLayout>