I have a DialogFragment showing a custom layout containing a list view:
<?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>
I inflate my layout this way in the onCreateDialog method:
View contentView = getActivity().getLayoutInflater().inflate(R.layout.ui_alertdialog_checkable_list, null);
It is working, but my list view's adapter is calling getView too many times and calling notifyDatasetChanged is really slow. This is caused by the way I'm inflating my layout : I don't attach it to a parent view, so Android seems unable to compute the list view's height, and so, creates a lot of views using getView.
If I set a fixed height, everything works properly, but I can't do that. Setting the height to MATCH_PARENT doesn't works either.
Any one knows how to have a dialog with a custom layout and a properly working list view?
It took a while but I found a solution : using a RelativeLayout fixed the problem! Sample 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>