Skip to content Skip to sidebar Skip to footer

Section Indexer Overlay Is Not Updating As The Adapter's Data Changes

I have implemented Section Indexer for an Adapter class which extends BaseAdapter. Now for the first launch Section Indexer is showing an overlay correctly. But when the contents o

Solution 1:

Thought I'd share a ready-made version of the above workaround for a ListActivity:

private boolean FLAG_THUMB_PLUS = false;
privatevoidjiggleWidth() {

    ListView view = getListView();
    if (view.getWidth() <= 0)
        return;

    int newWidth = FLAG_THUMB_PLUS ? view.getWidth() - 1 : view.getWidth() + 1;
    ViewGroup.LayoutParams params = view.getLayoutParams();
    params.width = newWidth;
    view.setLayoutParams( params );

    FLAG_THUMB_PLUS = !FLAG_THUMB_PLUS;
}

Works for me.

Solution 2:

To make it work in all cases (especially with FILL_PARENT layout and orientation change), instead of using a ListView directly, use this class and call invalidateSectionIndexer() after your changed your Adapter.

classMyListViewextendsListView {
    publicMyListView(Context context) {
        super(context);
    }

    publicvoidinvalidateSectionIndexer() {
        // 1. this invalidates the Section Indexersuper.setFastScrollEnabled(false);
        // Your adapter might be wrapped, e.g. when adding headers/footers.ListAdapteradapter= getAdapter();
        BaseAdapterbaseAdapter= (adapter instanceof WrapperListAdapter) ?
                (BaseAdapter) ((WrapperListAdapter) adapter).getWrappedAdapter() :
                (BaseAdapter) adapter;
        baseAdapter.notifyDataSetChanged();
        super.setFastScrollEnabled(true);

        // 2. This fixes the android bug of section overlay not positioned correctly.intwidth= getWidth();
        intheight= getHeight();
        super.onSizeChanged(width, height, width, height);
    }
}

Solution 3:

a small workaround which was suggested by lee.wilmot is

privatevoidjiggleGrid() {

          int newWidth = flag( FLAG_THUMB_PLUS ) ?
FrameLayout.LayoutParams.FILL_PARENT :
                  grid.getWidth() - 1;

          FrameLayout.LayoutParams l = new FrameLayout.LayoutParams(
                          newWidth,
                          FrameLayout.LayoutParams.FILL_PARENT
          );

          grid.setLayoutParams( l );

          toggleFlag( FLAG_THUMB_PLUS );
  }

flag and toggleFlag are some functions.

Call this method after setting setFastScrollEnabled(true);. It worked for me....

Solution 4:

Above solutions are ok except for case when width is set to FILL_PARENT. To make them work for this case you should override onSizeChanged like this

@OverrideprotectedvoidonSizeChanged(int w, int h, int oldw, int oldh)
{
    super.onSizeChanged( w , h , oldw , oldh );
    if ( mInstance.getLayoutParams().width != FrameLayout.LayoutParams.FILL_PARENT )
    {
        mInstance.post( newRunnable()
        {   
            @Overridepublicvoidrun()
            {
                mInstance.setLayoutParams( newFrameLayout.LayoutParams( FrameLayout.LayoutParams.FILL_PARENT , FrameLayout.LayoutParams.FILL_PARENT ) );   
            }
        });
    }
}

With this modification size will be ok after changing screen orientation.

Solution 5:

I got similar problem using ResourceCursorAdapter . Section indexer was not updating after new adapter was set for list or swapCursor() method for updating cursor was called. I tried all the solutions posted on StackOverflow without any success. Only working workaround that i found is that you need to detach/attach fragment which you want to update (you need to trigger onCreateView() method of fragment where you build your UI) after i update my Cursor (not CursorAdapter.. you need to update/set adapter in onCreateView() ). This worked for me. Here is code which i use to reattach fragment.

fragmentManager.beginTransaction().detach(frag).attach(frag).commitAllowingStateLoss();

You can also try to remove corresponding listView from layout and that add it to layout again but i got into some issues using this approach.

Post a Comment for "Section Indexer Overlay Is Not Updating As The Adapter's Data Changes"