gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:01:29 25 4
gpt4 key购买 nike

我有一个显示包含 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com