Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with Fast Scroll in Android 4.4

I wrote an app which contains a ListView in its layout that supports fast scroll (i.e. dragging the scroll bar slider allows you to quickly scroll up or down the list). This worked perfectly in all version of Jelly Bean (4.1-4.3). However, after I updated to Kit Kat, fast scroll no longer works, and my scroll bar is just a normal scroll bar. However, if I switch out of my app and back in, fast scroll appears. How do I get fast scroll to consistently work in Kit Kat? I;ve copied and pasted my list view adapter code below:

// 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;
    }
}
like image 217
Tim Avatar asked Dec 08 '13 00:12

Tim


2 Answers

Having the same issue here, it's not a good solution, but for now you can do :

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

This will keep your fast scroller on all the time.

like image 137
Talihawk Avatar answered Oct 03 '22 16:10

Talihawk


Here's a slightly improved work-around which is similar to mikejonesguy's answer

Basically you set fast scroll always visible when the list scroll is not idle. When the list stops scrolling you start a timer and turn fast scroll off after a period of time. This duplicates the way it used to work on older versions of android.

Hope it helps

//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) 
    {
    }
});
like image 2
JeffG Avatar answered Oct 03 '22 16:10

JeffG