android - DialogFragment + listview = getView 调用次数过多

标签 android listview android-fragments dialogfragment getview

我有一个显示包含 ListView 的自定义布局的 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);

它正在工作,但我的 ListView 的适配器调用 getView 的次数过多,调用 notifyDatasetChanged 的​​速度非常慢。这是由我膨胀布局的方式引起的:我没有将它附加到父 View ,因此 Android 似乎无法计算 ListView 的高度,因此,使用 getView 创建了很多 View 。

如果我设置固定高度,一切正常,但我做不到。将高度设置为 MATCH_PARENT 也不起作用。

有人知道如何使用自定义布局和正常工作的 ListView 进行对话吗?

最佳答案

花了一段时间,但我找到了解决方案:使用 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>

关于android - DialogFragment + listview = getView 调用次数过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31386827/

相关文章:

android - 如何为以下代码实现onCompletion监听器

android - 使用最后选择的 fragment 重新打开应用程序

java - 在 Activity 中单击按钮时在回收器 View 和 fragment 之间切换

android - 启动一个没有 UI 的 Fragment

android - 如何在一定次数后停止计时器

android - 我可以从 UI 线程以外的不同线程访问 android 小部件引用吗?

java - 如何用 <pre> 解析内容?

Android:深层链接网址

android - 即使 ListView 没有项目也显示 ListView 标题

android - 在android自定义 ListView 中向右滑动显示按钮