0

I want to make letters in my Views in ListView gradually disappear. I created a method like this inside my View class to change the alpha to 0 on every letter with delay using the Handler:

public void clearLetter(final int textPosition, final BaseFragmentAdapter adapter) {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            ...
            text.setSpan(new ForegroundColorSpan(Color.alpha(0)), 0, textPosition, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            adapter.notifyDataSetChanged();
            if (hasMoreLetters) {
                clearLetter(textPosition + 1, adapter);
            }
        }
    }, 300);
}

Everything works fine until I start scrolling too fast: I get IndexOutOfBoundsException. I understand that the views get recycled and the text changes so the job in an already launched Handler is trying to get the previous text, thus giving the exception.

But I'm not sure how to overcome this problem. I want text in the views to disappear anyway, whether the views, that hold the text, are inside the visible zone or not. I tried not to recycle the views and all worked fine except for performance, so it's not an option.

Thank you in advance for any tips that might help.

serg66
  • 1,148
  • 1
  • 17
  • 31
  • Is this happening inside your AdapterView? Where is this method defined and how is it called? – kha Mar 19 '15 at 08:28
  • It's defined inside my custom `View` class. I call this method in the `getView` method of the adapter. Do I understand correctly, if my screen allows only 10 items of `ListView` to display, there cannot be more than 10 instances of my `View` class if I use the recycling? What would be the way to make the letters disappear in an item that is out of the screen and there's, in fact, no instance of this item? Create a separate `Handler` for every `View`, but outside the view itself (in the adapter)? Actually, while I was typing it, I started having some ideas, so thank you for your questions :) – serg66 Mar 19 '15 at 10:54

0 Answers0