Android 4.4中快速滚动的问题

时间:2013-12-08 00:21:27

标签: android android-listview android-4.4-kitkat

我编写了一个应用程序,其布局中包含一个支持快速滚动的ListView(即拖动滚动条滑块可以让您快速向上或向下滚动列表)。这适用于所有版本的Jelly Bean(4.1-4.3)。但是,在我更新到Kit Kat后,快速滚动不再有效,我的滚动条只是一个普通的滚动条。但是,如果我退出我的应用程序并重新登录,则会出现快速滚动。如何快速滚动以在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;
    }
}

3 个答案:

答案 0 :(得分:13)

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

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

这将使您的快速卷轴始终保持开启状态。

答案 1 :(得分:2)

这是一个稍微改进的解决方法,类似于mikejonesguy的回答

基本上,当列表滚动不空闲时,您设置快速滚动始终可见。 当列表停止滚动时,您启动计时器并在一段时间后快速滚动关闭。这复制了以前用于旧版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) 
    {
    }
});

答案 2 :(得分:1)

这是一个已知问题(报告为herehere),并且自4.4.3起修复。

这是一种解决方法:

  public static void setFastScrolledEnabled(final AdapterView<?> adapterView,final boolean enable)
    {
    final GridView gridView;
    final ListView listView;
    if(adapterView instanceof GridView)
      {
      gridView=(GridView)adapterView;
      listView=null;
      }
    else if(adapterView instanceof ListView)
      {
      listView=(ListView)adapterView;
      gridView=null;
      }
    else throw new UnsupportedOperationException("setFastScrolledEnabled is only available for gridView/listView");
    if(Build.VERSION.SDK_INT==VERSION_CODES.KITKAT)
      adapterView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
        {
          @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
          @Override
          public void onGlobalLayout()
            {
            if(gridView!=null)
              gridView.setFastScrollEnabled(enable);
            else if(listView!=null)
              listView.setFastScrollEnabled(enable);
            adapterView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    else if(gridView!=null)
      gridView.setFastScrollEnabled(enable);
    else if(listView!=null)
      listView.setFastScrollEnabled(enable);
    }