Starting AnimationDrawable in ListView items. When are the items attached?

夙愿已清 提交于 2019-12-02 06:29:03

问题


So I've been doing some reading for the past few hours and I understand that calling start() on an AnimationDrawable before the Drawable/ImageView is fully attached will not start the animation. This seems pretty consistent with the usual UI initialization process (like view's dimensions being returned as 0 right after adding the view).

I imagine that this same issue is at fault when I try to start the animation from the getView() method of an Adapter. Using a delayed Runnable that performs the start() call solves this problem but is clearly not a desirable solution.

Is there any way to receive a callback once ListView's items are "fully attached"?


回答1:


According to the documentation, you must wait until the View is attached to the window before starting animation. Therefor, you should add an OnAttachStateChangeListener to the view that will execute when it has been attached, and start the animation from there.

ImageView loadingImg = (ImageView)v.findViewById(R.id.image);
loadingImg.setBackgroundResource(R.drawable.progressdialog);
loadingImg.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
  @Override
  public void onViewAttachedToWindow(View v) {
    AnimationDrawable loadingAnimation = (AnimationDrawable) v.getBackground();
    loadingAnimation.start();
  }

  @Override
  public void onViewDetachedFromWindow(View v) {
  }
});

I've tried starting the animation in a Runnable in the View's post() method, and that didn't work. The above method is the only way I've reliably been to have animation start in a ListView.



来源:https://stackoverflow.com/questions/9780643/starting-animationdrawable-in-listview-items-when-are-the-items-attached

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!