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?