在Android 4.4中快速滚动的问题该怎么解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (25)

如果我切换出我的应用程序并返回,将出现快速滚动。我如何快速滚动到Kit Kat一直工作?我已经复制并粘贴我的列表视图适配器代码如下:

// Adds section popup for fast scroll
class NameListAdapter extends  SimpleAdapter implements SectionIndexer  {
    HashMap<String, Integer> alphaIndexer;
    private String[] sections;
    private ArrayList<String> sectionList;
    private List<HashMap<String, String>> data = null;

    public NameListAdapter (Context context, List<HashMap<String, String>> data, 
            int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        alphaIndexer = new HashMap<String, Integer>();
        sectionList = new ArrayList<String>();
        this.data = data;
        int size = data.size();

        for (int i = 0; i < size; i++) {
            HashMap<String, String> myData = (HashMap <String, String>) data.get(i);
            // Get first character
            String ch = myData.get("name").substring(0, 1);
            // Convert character to upper case
            ch = ch.toUpperCase();

            // Put first char/index into our HashMap
            if (!alphaIndexer.containsKey(ch)) {
                alphaIndexer.put(ch, i);
                sectionList.add(ch);
            }
        }
        sections = new String[sectionList.size()];
        sectionList.toArray(sections);
    }

    @Override
    public int getPositionForSection(int section) {
        if (section >= sections.length) {
            return getCount() - 1;
        }

        return alphaIndexer.get(sections[section]);
    }

    @Override
    public int getSectionForPosition(int pos) {

        if (pos > data.size()) {
            return 0;
        }

        HashMap<String, String> myData = (HashMap <String, String>) data.get(pos);
        String ch = myData.get("name").substring(0, 1);
        // Convert character to upper case
        ch = ch.toUpperCase();

        for (int i = 0; i < sectionList.size(); i++) {
            if (sectionList.get(i).equals(ch)) {
                return i;
            }
        }
        return 0;

    }

    @Override
    public Object[] getSections() {
        return sections;
    }
}
提问于
用户回答回答于

当列表滚动不空闲时,快速滚动将始终可见。当列表停止滚动时,启动计时器并在一段时间后关闭快速滚动。这复制了它用于旧版android的方式。

希望能帮助到你:

//fix for kitkat bug in fast scroll
Runnable delayedFastScrollFixRunnable = new Runnable()
{
    @Override
    public void run()
    {
        if (listView != null && fastScrollAlwaysVisible)
        {
            listView.setFastScrollAlwaysVisible(false);
            fastScrollAlwaysVisible = false;
        }
    }
};
Handler delayedFastScrollFixHandler = new Handler();
boolean fastScrollAlwaysVisible = false;

mList.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) 
    {
       //fix an issue with 4.4 not showing fast scroll
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    {
        if (scrollState == SCROLL_STATE_IDLE)
        {
            if (fastScrollAlwaysVisible)
            {
                delayedFastScrollFixHandler.postDelayed(delayedFastScrollFixRunnable, 750);
            }
        }
        else
        {
            if (!fastScrollAlwaysVisible)
            {
                delayedFastScrollFixHandler.removeCallbacks(delayedFastScrollFixRunnable);
                listView.setFastScrollAlwaysVisible(true);
                fastScrollAlwaysVisible = true;
            }
        }
    }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
    {
    }
});
用户回答回答于

在这里有同样的问题,这不是一个好的解决方案,但现在你可以做到:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    view.setFastScrollAlwaysVisible(true);
}

扫码关注云+社区