ProgressBar 动画只显示一次

ProgressBar animation only showing once

在我的 MainActivity's xml 文件中,activity_main.xml 我有一个 FrameLayout 显示预订列表:

    <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" android:layout_marginTop="16dp"
        android:visibility="gone"
        android:layout_gravity="center"/>

    <FrameLayout android:id="@+id/home_page_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

当用户想要通过点击取消一项预订时,必须更新列表,因此我使用这种方法调用网络:

public void cancelReservation(ReservationResponse rR){
    if (mNetwork != null) {return;}
    if (isNetworkAvailable()) {
        showProgress(true);
        mNetwork = new NetworkTask(this, "cancel_reservation", rR);
        mNetwork.execute((Void) null);
    }else{
        // display error
        Log.d("LOG_TAG", "No network available!");
        Toast.makeText(this, R.string.no_network_dialog ,Toast.LENGTH_LONG).show();
    }
}

更新 AppEngine 预订,returns 更新列表返回 Activity。 在执行 AsyncTask 之前,我调用方法 showProgress,开始这个简单的动画:

该方法有一个恒定的持续时间,在此期间 FrameLayout 被隐藏以支持 ProgressBar;然后在 ProgressBaronAnimationEnd 方法中恢复 FrameLayout 并将动画设置为 View.GONE,最后显示更新的预订列表:

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void showProgress(final boolean showProgr) {
    final FrameLayout home_scroll = (FrameLayout) findViewById(R.id.home_page_frame);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        mProgressView.destroyDrawingCache();

        int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
        home_scroll.setVisibility(showProgr ? View.GONE : View.VISIBLE);
        mProgressView.setVisibility(showProgr ? View.VISIBLE : View.GONE);
        mProgressView.animate().setDuration(shortAnimTime*9).alpha(
                showProgr ? 0 : 1).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mProgressView.setVisibility(showProgr ? View.GONE : View.VISIBLE);
                    home_scroll.setVisibility(showProgr ? View.VISIBLE : View.GONE);
                }
        });

    } else {
        home_scroll.setVisibility(showProgr ? View.GONE : View.VISIBLE);
    }
}

My quirky problem is that while the first time I cancel a reservation the animation is displayed, from the second time onwards every time I cancel a reservation the animation does not appear. How come?

我试过调用这个方法:

        mProgressView.destroyDrawingCache();

但是没用....

我已经通过修改我使用方法的方式解决了它showProgress()

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void showProgress(final boolean showProgr) {
    // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
    // for very easy animations. If available, use these APIs to fade-in
    // the progress spinner.
    final FrameLayout home_scroll = (FrameLayout) findViewById(R.id.home_page_frame);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

        mProgressView.refreshDrawableState();//setVisibility(View.GONE);
        if(showProgr) {
            home_scroll.setVisibility(showProgr ? View.GONE : View.VISIBLE);
            mProgressView.setVisibility(showProgr ? View.VISIBLE : View.GONE);
            mProgressView.animate();

        }else{
            mProgressView.setVisibility(showProgr ? View.VISIBLE : View.GONE);
            mProgressView.invalidate();
            mProgressView.clearAnimation();
            mProgressView.setActivated(false);
            home_scroll.setVisibility(showProgr ? View.GONE : View.VISIBLE);
        }
    } else { //TODO debug <= HONEYCOMB
        home_scroll.setVisibility(showProgr ? View.GONE : View.VISIBLE);
    }
}

之前我是通过设置一个短动画并等待它自己完成来使用该方法。 Insead,通过不设置计时器并调用 showProgress 方法两次,第一次当动画必须以 showProgress(true) 开始时,第二次当它必须以 showProgress(false) 结束时,它然后所有效果很好。