Android 4.4中快速滚动的问题

Android 4.4中快速滚动的问题,android,android-listview,android-4.4-kitkat,Android,Android Listview,Android 4.4 Kitkat,我编写了一个应用程序,它的布局中包含一个支持快速滚动的列表视图(即拖动滚动条滑块可以快速向上或向下滚动列表)。这在所有版本的Jelly Bean(4.1-4.3)中都非常有效。然而,在我更新到Kit Kat之后,快速滚动不再工作,我的滚动条只是一个普通的滚动条。但是,如果我退出应用程序并返回,则会出现快速滚动。如何使快速滚动始终在Kit Kat中工作?我我已在下面复制并粘贴了我的列表视图适配器代码: // Adds section popup for fast scroll class Name

我编写了一个应用程序,它的布局中包含一个支持快速滚动的列表视图(即拖动滚动条滑块可以快速向上或向下滚动列表)。这在所有版本的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;
    }
}
//为快速滚动添加节弹出窗口
类NameListAdapter扩展SimpleAdapter实现SectionIndexer{
HashMap字母索引器;
私有字符串[]节;
私有ArrayList部分列表;
私有列表数据=null;
公共名称列表适配器(上下文、列表数据、,
int资源,字符串[]从,int[]到){
超级(上下文、数据、资源、从、到);
alphaIndexer=新HashMap();
sectionList=新的ArrayList();
这个数据=数据;
int size=data.size();
对于(int i=0;i=节.长度){
返回getCount()-1;
}
返回alphaIndexer.get(sections[section]);
}
@凌驾
公共int getSectionForPosition(int pos){
如果(位置>数据大小()){
返回0;
}
HashMap myData=(HashMap)data.get(pos);
字符串ch=myData.get(“name”).substring(0,1);
//将字符转换为大写
ch=ch.toUpperCase();
对于(int i=0;i
这里有同样的问题,这不是一个好的解决方案,但现在你可以做:

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

这将使您的快速滚动条始终处于打开状态。

这里有一个稍微改进的工作,与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) 
    {
    }
});
这是一个已知的问题(已报告和),从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);
    }
public static void setFastScrolleEnabled(最终AdapterView AdapterView,最终布尔启用)
{
最终GridView GridView;
最终列表视图列表视图;
if(GridView的adapterView实例)
{
gridView=(gridView)adapterView;
listView=null;
}
else if(ListView的adapterView实例)
{
listView=(listView)adapterView;
gridView=null;
}
else抛出新的UnsupportedOperationException(“SetFastScrolleEnabled仅适用于gridView/listView”);
if(Build.VERSION.SDK\u INT==VERSION\u CODES.KITKAT)
adapterView.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener())
{
@TargetApi(Build.VERSION\u code.JELLY\u BEAN)
@凌驾
公共图书馆
{
如果(gridView!=null)
gridView.setFastScrollEnabled(启用);
else if(listView!=null)
listView.setFastScrollEnabled(启用);
adapterView.getViewTreeObserver();
}
});
else if(gridView!=null)
gridView.setFastScrollEnabled(启用);
else if(listView!=null)
listView.setFastScrollEnabled(启用);
}

我打开了一个关于这个的bug,所以请随意启动它。这似乎在Android v4.4.3中得到了解决。这对我来说也很有效。我怀疑这是KitKat的一只虫子。它在我的Jelly Bean设备中正常工作。对于KitKat设备来说,这似乎是唯一真正解决问题的方法。您应该考虑对版本进行平等检查:因为它被固定在以后的版本中:这不是真正的解决方案,因为它只在第一次工作(直到您将另一适配器设置为ListVIEW)。尽管如此,它试图触及问题的根源。@david.schreiber我知道。我不知道如何彻底解决这个问题。如果你有办法,能告诉我吗?也许他们已经在4.4.3上修复了它?似乎他们已经修复了:就像mikejonesguy的解决方案一样,这个解决方案在使用快速滚动条时隐藏了它(只是有一点延迟)。您的代码可能有错误?此外,您还需要先移动列表,然后才能使用快速滚动器。