部分索引器覆盖不会随着适配器数据的更改而更新

Shr*_*aya 9 android

我已经为Adapter类实现了Section Indexer,它扩展了BaseAdapter.现在,对于第一次启动,Section Indexer正确显示叠加层.但是当列表的内容更新时,Section Overlay不会更新并给出ArrayOutOfBoundException.对于一个修复我做的是我做了listview.setFastScrollEnabled(false); 更新适配器内容; 然后listview.setFastScrollEnabled(true); 现在发生的事情是叠加得到更新,但叠加层将出现在列表视图的左上角.我怎样才能解决这个问题.

Mik*_*l_S 8

以为我会为ListActivity共享上述解决方法的现成版本:

private boolean FLAG_THUMB_PLUS = false;
private void jiggleWidth() {

    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;
}
Run Code Online (Sandbox Code Playgroud)

适合我.


小智 5

要使其在所有情况下都能正常工作(特别是使用FILL_PARENT布局和方向更改),而不是直接使用ListView,请在更改适配器后使用此类并调用invalidateSectionIndexer().

class MyListView extends ListView {
    public MyListView(Context context) {
        super(context);
    }

    public void invalidateSectionIndexer() {
        // 1. this invalidates the Section Indexer
        super.setFastScrollEnabled(false);
        // Your adapter might be wrapped, e.g. when adding headers/footers.
        ListAdapter adapter = getAdapter();
        BaseAdapter baseAdapter = (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.
        int width = getWidth();
        int height = getHeight();
        super.onSizeChanged(width, height, width, height);
    }
}
Run Code Online (Sandbox Code Playgroud)


Shr*_*aya 1

lee.wilmot 建议的一个小解决方法是

private void jiggleGrid() {

          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 );
  }
Run Code Online (Sandbox Code Playgroud)

flag 和toggleFlag 是一些函数。

设置后调用该方法setFastScrollEnabled(true);。它对我有用......