13

我一直在为此挠头很长时间,并没有任何运气地寻找答案!这似乎是微不足道的,但据我所知,它不是。

我在我的 Android 应用程序中使用了一个列表视图,其中每个项目(视图)在加载和显示内容之前显示一个旋转的 ProgressBar(内容是通过 http 调用和 json 检索的,因此可能需要一段时间来处理)。问题是旋转的进度条彼此独立旋转,从而产生了旋转混乱的效果,而不是一排同步的漂亮加载标记。

我已经尝试了我能想出的一切...不在getView()中回收ProgressBar ...只有一个相同ProgressBar的实例...在列表项可见时重置ProgressBars android:progress(通过onScroll () 在活动中)...等等,但是由于它们在创建时间开始旋转(当在适配器中调用 getView 时)它们将永远不会具有相同的周期同步。

在此处输入图像描述

欢迎任何建议!

4

6 回答 6

2

编辑:现在完美运行!- 在第一次重复后插入动画侦听器调用 setStartOffset() 回到 0,这样它就不会保持随机“跳跃”。


我为这个问题找到了一个可行的解决方案,它通过将动画定时到当前系统毫秒来工作。这有点像 hack,因为它使用反射来获取mAnimation. ProgressBar话虽如此,该字段自创建以来一直保留在 Android 源代码中(最高工作于 4.2)。

创建android.widget.SyncedProgressBar类,并使用它而不是ProgressBar在您的 .xml 文件中。它实质上使动画在动画持续时间的开始处开始。您还可以setDuration(500)尝试验证它是否有效(进度轮会很快旋转)。希望这可以帮助!

package android.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

import java.lang.reflect.Field;

/**
* @author Oleg Vaskevich
*
*/
public class SyncedProgressBar extends ProgressBar {

    public SyncedProgressBar(Context context) {
        super(context);
        modifyAnimation();
    }

    public SyncedProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        modifyAnimation();
    }

    public SyncedProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        modifyAnimation();
    }

    @Override
    public void setVisibility(int v) {
        super.setVisibility(v);
        modifyAnimation();
    }

    @SuppressLint("NewApi")
    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        modifyAnimation();
    }

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

    @Override
    public synchronized void setIndeterminate(boolean indeterminate) {
        super.setIndeterminate(indeterminate);
        modifyAnimation();
    }

    public void modifyAnimation() {
        Field mAnim;
        try {
            mAnim = ProgressBar.class.getDeclaredField("mAnimation");
            mAnim.setAccessible(true);
            AlphaAnimation anim = (AlphaAnimation) mAnim.get(this);
            if (anim == null)
                return;

            // set offset to that animations start at same time
            long duration = anim.getDuration();
            long timeOffset = System.currentTimeMillis() % duration;
            anim.setDuration(duration);
            anim.setStartOffset(-timeOffset);
            anim.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    animation.setStartOffset(0);
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                }
            });
        } catch (Exception e) {
            Log.d("SPB", "that didn't work out...", e);
            return;
        }
        postInvalidate();
    }

}
于 2012-11-25T22:20:28.317 回答
1

以下是如何使它工作。只使用一个 AnimationDrawable,复用回调。ProgressBar 并没有真正添加任何东西,您还不如坚持使用 View。创建一个绕过 Drawable 管理的 View 子类。

public class Whirly extends View
{
   Drawable image = null;

   public Whirly(Context context, AttributeSet attr)
   {
      super(context, attr);
   }

   @Override
   public void draw(Canvas canvas)
   {
      if (image!=null)
         image.draw(canvas);
   }

   public void setImageDrawable(Drawable image)
   {
      this.image = image;
      invalidate();
   }
}

然后让你的活动以某种方式跟踪所有的漩涡。

public class Demo extends Activity implements Drawable.Callback
{
   private Handler h;
   private AnimationDrawable a;

   private Whirly w0;
   private Whirly w1;
   private Whirly w2;
   private Whirly w3;

   public void onCreate(Bundle bundle)
   {
      ...

      h = new Handler();

      a = (AnimationDrawable) getResources().getDrawable(R.drawable.mywhirly);
      int width = a.getIntrinsicWidth();
      int height = a.getIntrinsicHeight();
      a.setBounds(0, 0, width, height);

      w0 = (Whirly) findViewById(R.id.whirly0);
      w1 = (Whirly) findViewById(R.id.whirly1);
      w2 = (Whirly) findViewById(R.id.whirly2);
      w3 = (Whirly) findViewById(R.id.whirly3);

      w0.setImageDrawable(a);
      w1.setImageDrawable(a);
      w2.setImageDrawable(a);
      w3.setImageDrawable(a);

      a.setCallback(this);
      a.start();
   }

   public void invalidateDrawable (Drawable who)
   {
      w0.invalidate();
      w1.invalidate();
      w2.invalidate();
      w3.invalidate();
   }

   public void scheduleDrawable (Drawable who, Runnable what, long when)
   {
      h.postAtTime(what, who, when);
   }

   public void unscheduleDrawable (Drawable who, Runnable what)
   {
      h.removeCallbacks(what, who);
   }
}
于 2012-06-20T14:16:07.333 回答
0

在您的适配器 getView() 中禁用您的progressViwe,然后为每个progressView

handler.postAtTime(new Runnable(){

public void run(){
     progressView.setEnabled(true);
}}, someTimeInTheFuture
);

这将做的是同时启用所有的progressViews。这可能有效,我没有测试过。如果这不起作用,那么您可能希望动态添加progressView(不包含在布局中,但通过addView() 添加)。但是在 runnable 中这样做,这里的关键是让它们都被同时添加。

于 2012-06-20T13:11:40.380 回答
0

挖掘源代码,我发现旋转图像是一个可绘制的,因为私有Animation. 没有办法获得视图或动画,因此放弃了我在所有视图上重用一个动画的任何想法。我想说你唯一的选择是 Jug6ernaut 建议的,即使它不是最漂亮的解决方案。

于 2012-06-20T15:26:48.213 回答
0

我不确定这是可能的,因为您的每一行在ListView移出屏幕时都会被回收。当屏幕上出现新行时,ProgressBar所有视图都必须相互重新同步。保持所有行同步将是一个大 PITA。

您是否考虑过在一个请求中加载所有列表数据并将该数据缓存到本地存储?这将更有效率,并带来更好的用户体验。ListView立即加载空行并不是很有用。我宁愿等待列表完全填充。

于 2012-06-22T00:49:32.623 回答
0

1> 为列表行创建布局

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip" >

    <TextView
        android:id="@+id/word_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Word 1" />

    <ProgressBar
        android:id="@+id/row_progress"
        style="@style/Widget.Sherlock.Light.ProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:indeterminate="true" />

    <ImageView
        android:id="@+id/speaker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/speaker_icon" 
        android:visibility="gone"/>
</RelativeLayout>

2> 然后在getView方法中

if(word.getIsWordSynchedLocally() == 1){
                convertView.findViewById(R.id.row_progress).setVisibility(View.GONE);
                convertView.findViewById(R.id.speaker).setVisibility(View.VISIBLE);
            }else{
                convertView.findViewById(R.id.row_progress).setVisibility(View.VISIBLE);
                convertView.findViewById(R.id.speaker).setVisibility(View.GONE);
            }
于 2013-04-23T12:13:45.353 回答