【问题标题】:Issues with Fast Scroll in Android 4.4Android 4.4 中的快速滚动问题
【发布时间】:2013-12-08 00:21:26
【问题描述】:

我写了一个应用程序,它的布局中包含一个支持快速滚动的 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;
    }
}

【问题讨论】:

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


【解决方案1】:

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

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

这将使您的快速滚动条始终保持开启状态。

【讨论】:

  • 这也对我有用。我怀疑这是 KitKat 中的错误。它在我的果冻豆设备中正常工作。
  • 似乎对于 KitKat 设备,这是唯一真正解决问题的方法。您应该考虑对版本进行相等性检查:因为它在以后的版本中已修复:code.google.com/p/android/issues/detail?id=63545
【解决方案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) 
    {
    }
});

【讨论】:

  • 像 mikejonesguy 的解决方案一样,这个解决方案在使用时隐藏了快速滚动条(只是有一点延迟)。您的代码可能有一些错误?此外,您需要先移动列表,然后才能使用快速滚动条。
【解决方案3】:

这是一个已知问题(报告了 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);
    }

【讨论】:

  • 这不是真正的解决方法,因为它只适用于第一次(直到您将另一个适配器设置为 ListView)。尽管如此,它还是试图从根本上解决问题。
  • @david.schreiber 我知道。我不知道如何完全解决这个问题。如果你找到方法,你能告诉我吗?也许他们已经在 4.4.3 上修复了它?
猜你喜欢
  • 2012-02-29
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
相关资源
最近更新 更多