有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

更改列表视图适配器后禁用java Fast scoll

我有一个启用了快速滚动的列表视图:

mList.setFastScrollEnabled(true);

这很有效。但是当我更换适配器时,我不能再快速滚动了

mList.setAdapter(mAdapter);

我的适配器已正确实现getCount()。召唤

mList.setFastScrollEnabled(true);

设置新适配器后,也无法将安卓:fastScrollEnabled="true"添加到ListViewXML中

有没有办法重新启用快速滚动


共 (1) 个答案

  1. # 1 楼答案

    在深入挖掘Android源代码之后,我终于找到了一个解决方案:

        mAdapter = new ArrayAdapter....
        mList.setAdapter(mAdapter);
    
        // We have to post notifyDataSetChanged() here, so fast scroll is set correctly.
        // Without it, the fast scroll cannot get any child views as the adapter is not yet fully
        // attached to the view and getChildCount() returns 0. Therefore, fast scroll won't
        // be enabled.
        // notifyDataSetChanged() forces the listview to recheck the fast scroll preconditions.
        mList.post(new Runnable() {
            @Override
            public void run() {
                mAdapter.notifyDataSetChanged();
            }
        });
    

    FastScroller.java有以下方法:

    public void onItemCountChanged(int totalItemCount) {
        final int visibleItemCount = mList.getChildCount();
    
        ....
    
        updateLongList(visibleItemCount, totalItemCount);
    }
    

    调用setAdapter()时,FastScroller会重新检查启用快速滚动的条件。但由于新适配器尚未完全显示,mList.getChildCount()返回零,updateLongList()将无法启用快速滚动

    使用我的修复程序,ListView会收到通知,在新适配器完全连接到视图后,基线数据发生了更改,现在满足了快速滚动的先决条件