1

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?

Tim Autin
  • 6,043
  • 5
  • 46
  • 76
  • getView() is called as many as number of rows you have in ListView – Green goblin Jul 13 '15 at 15:03
  • It should, but in my case it is called something like 4-5 times per row. The same problem appears if you define a list view with a WRAP_CONTENT height : Android can't compute the height and have to create a lot of rows. See : http://stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-co – Tim Autin Jul 13 '15 at 15:12
  • What about "fill_parent" ? – Steven_BDawg Jul 13 '15 at 18:41
  • fill_parent = match_parent (deprecated), it doesn't work either. – Tim Autin Jul 13 '15 at 18:42

1 Answers1

1

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>
Tim Autin
  • 6,043
  • 5
  • 46
  • 76