0

I have ListView with items, each item has 5 TextView's, one of this TextView getting his text from Html.fromHtml function, i use ImageGetter function to handle img tags in HTML, images downloading asynchronously, while images downloading i want to show AnimationDrawable (cycling progress bar).

Here is my ImageGetter implementation:

holder.CommentText.setText(Html.fromHtml(commentObj.getComment(), new ImageGetter() {


                    @Override
                    public Drawable getDrawable(String source) {
                            Drawable  d = null;
                            if(UserSettings.LoadImages)
                            {
                                    if(commentObj.getImages()==null)
                                    {
                                            commentObj.setImages(new HashMap<String, Drawable>());
                                    }

                                    String imageLink=source;
                                    if(!imageLink.contains("http://"))
                                            imageLink="http://rotter.net"+source;
                                    if(commentObj.getImages().containsKey(imageLink))
                                    {
                                            d=commentObj.getImages().get(imageLink);
                                            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                                    }
                                    else
                                    {
                                            AnimationDrawable animated=(AnimationDrawable)context.getResources().getDrawable(R.anim.image_loading);
                                            animated.setBounds(0, 0, animated.getIntrinsicWidth(), animated.getIntrinsicHeight()); 
                                            //animated.start();
                            commentObj.getImages().put(imageLink, animated);
                            DownloadImage downImg=new DownloadImage(commentObj, imageLink,v,adapter,Width,context.getResources());
                            holder.CommentText.onAttachedToWindow();
                            downImg.execute(new String[] { imageLink } );
                            return animated;
                                    }
                            }
                            return d;
                    }
            },null ));

After some reading i created new custom TextView class:

public class NewTextView extends TextView {

    private Handler mHandler=new Handler();

    public NewTextView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
    }

    public NewTextView(Context context,AttributeSet attributeSet) {
            super(context,attributeSet);
            // TODO Auto-generated constructor stub
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        handleAnimationDrawable(true);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        handleAnimationDrawable(false);
    }

    private void handleAnimationDrawable(boolean isPlay) {
        CharSequence text = getText();
        if (text instanceof Spanned) {
            Spanned span = (Spanned) text;
            ImageSpan[] spans = span.getSpans(0, span.length() - 1,
                    ImageSpan.class);
            for (ImageSpan s : spans) {
                Drawable d = s.getDrawable();
                if (d instanceof AnimationDrawable) {
                    AnimationDrawable animationDrawable = (AnimationDrawable) d;
                    if (isPlay) {
                        animationDrawable.setCallback(this);
                        animationDrawable.start();
                    } else {
                        animationDrawable.stop();
                        animationDrawable.setCallback(null);
                    }
                }
            }
        }
    }

    @Override
    public void invalidateDrawable(Drawable dr) {
        invalidate();
    }

    @Override
    public void scheduleDrawable(Drawable who, Runnable what, long when) {
        if (who != null && what != null) {
            mHandler.postAtTime(what, when);
        }
    }

    @Override
    public void unscheduleDrawable(Drawable who, Runnable what) {
        if (who != null && what != null) {
            mHandler.removeCallbacks(what);
        }
    }
 }

NewTextView class responsible for starting and stopping the AnimationDrawable, The result is that some of AnimationDrawables running properly (cycle progress bar) and some AnimationDrawables not run at all, they only show the first Drawable in AnimationDrawable items.

What can be the problem? Why not all AnimationDrawables running properly?

Alex
  • 9,102
  • 3
  • 31
  • 35
  • Without looking at your code I'd recommend that you put in some log outputs to see what is started and what is stopped. Log.d(String tag, String message) – Warpzit May 06 '12 at 13:36
  • @Warpzit I added Log into the code, and i noticed that not all NewTextViews in ListView is enter onAttachedToWindow() function – Alex May 08 '12 at 16:42
  • Well its most likely something to do with how listview recycles its rows. You probably have to redesign how you do your animation. – Warpzit May 08 '12 at 16:57
  • @Warpzit The problem is that AnimationDrawable can be started only after the View was AttachedToWindow, but if some View's not get Attached to window i don't know how i can start AnimationDrawable. – Alex May 08 '12 at 17:16
  • You could start the animation in the getview methode of your adapter or something similar. – Warpzit May 08 '12 at 17:50
  • @Warpzit In getView Starting AnimationDrawable not working at all. – Alex May 09 '12 at 06:37
  • Well I use it so its possible :), I also stop the animation for each view when it is reintroduced and then start a new animation. You can hope someone else picks it up from here because I'm afraid I can't do much more with your approach. – Warpzit May 09 '12 at 06:40
  • I've answered this question here: http://stackoverflow.com/a/22155576/3311526 – Sanders Mar 03 '14 at 19:37

1 Answers1

0

you can check whether the item's method "onAttachedToWindow" is called several times, that cause invalidate every time.

Brian
  • 1
  • 1