Gerrit Beuze
Gerrit Beuze

Reputation: 921

How to disable DialogFragment to resize once dialog is displayed?

I'm using a listview in a DialogFragment contained in a LinearLayout

<?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:padding="@dimen/dialog_main_margin" >

<com.modelmakertools.simplemind.DragSortListView
    android:id="@+id/drag_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

where DragSortListView is based on a 3rd party ListView descendant that allows rearranging items using a draghandle image.

All works OK when this code is used in a (full screen) activity, but when used in a android.support.v4.app.DialogFragment, dragging an item causes the dialog + listview combi to continuesly update the dialog size. This causes dragging to be extremely slow and I see a lot of LogCat messages:

06-06 15:37:07.833: I/Choreographer(17281): Skipped 166 frames!  The application may be doing too much work on its main thread.

To locate the source of the problem, I changed the width and height of the listview to a fixed 400dp (rather than match_parent / wrap_content). Fixing the size completely eliminates the problem. Hence my question: is there a way to avoid the dialog fragment to adjust its size once it has been layed out once?

Upvotes: 0

Views: 1952

Answers (2)

Daniel De Le&#243;n
Daniel De Le&#243;n

Reputation: 13649

Try setting the windowSoftInputMode property to adjustNothing in the AndroidManifest.xml of the activities that use the dialog fragment.

<activity
    ...
    android:windowSoftInputMode="adjustNothing">
...

Upvotes: 0

Gerrit Beuze
Gerrit Beuze

Reputation: 921

I found a way to stop the listview updating the dialog size with the code below. However, it turns out, this only solves part of the problem: until the list is "fixated" loading is still very slow. But that has another cause that is beyond the scope of this question/answer

@Override
public void onResume() {
    super.onResume();
    // Fixate the listview height once the listview has been measured rather than keeping it at "wrap_content"
    // This drastically improves rearrange performance.
    if (_listView != null) {
        _listView.postDelayed(new Runnable() {
            @Override
            public void run() {
                fixateListViewHeight();
            }
        }, 500);
    }
}

private void fixateListViewHeight() {
    if (_listView != null) {
        int h = _listView.getMeasuredHeight();
        if (h != 0) {
            LayoutParams params = (LayoutParams) _listView.getLayoutParams();
            if (params != null)
                params.height = h;
        }
    }
}

Upvotes: 1

Related Questions