package com.fsck.k9.view;

import android.content.Context;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;


public class K9PullToRefreshListView extends PullToRefreshListView {

    public K9PullToRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected ListView createListView(Context context, AttributeSet attrs) {
        return new ListView(context, attrs) {
            @Override
            public void onRestoreInstanceState(Parcelable state) {
                super.onRestoreInstanceState(state);

                /*
                 * Force the list view to apply the restored state instantly instead of
                 * asynchronously, so potential data changes (which in turn cause an internal
                 * position save/restore) don't overwrite our saved position.
                 */
                layoutChildren();
            }
        };
    }
}

@@ -52,16 +52,48 @@
 ontext;%0A
+import android.graphics.Canvas;%0A
 import a
@@ -1056,16 +1056,1128 @@
        %7D
+%0A%0A            @Override%0A            protected void dispatchDraw(Canvas canvas) %7B%0A                if (getAdapter() != null) %7B%0A                    int count = getChildCount();%0A                    int itemCount = getAdapter().getCount();%0A%0A                    /*%0A                     * 2013-03-18 - cketti%0A                     *%0A                     * Work around a bug in ListView (?) that leads to a crash deep inside the%0A                     * framework code.%0A                     * I didn't track down the exact cause of this. My best guess is that we change%0A                     * the data (remove items) while the ListView isn't visible. Probably because%0A                     * the view is hidden, layoutChildren() is never called and when this method%0A                     * runs the layout contains more item views than the adapter contains elements%0A                     * and bad things(tm) happen.%0A                     */%0A                    if (itemCount %3C count) %7B%0A                        layoutChildren();%0A                    %7D%0A                %7D%0A%0A                super.dispatchDraw(canvas);%0A            %7D
 %0A       

Work around a bug in ListView