我在一个单独的Java文件中创建了一个进度栏对话框:
public class AppUtil {
// progress bar handling
public static Dialog showProgress(Activity activity) {
Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(0));
dialog.setContentView(R.layout.dialog_progress);
ProgressBar progressBar = dialog.findViewById(R.id.progressBar);
progressBar.getIndeterminateDrawable().setColorFilter(activity.getResources().getColor(R.color.fore_color), PorterDuff.Mode.MULTIPLY);
dialog.setCancelable(false);
dialog.show();
return dialog;
}
}
并且它有自己的RelativeLayout
个XML资源布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_progress"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
但如果我在播放按钮中调用它,进度条不会立即显示,而是只有在onClick
按钮完成时才会显示:
ImageButton buttonPlay = (ImageButton) currentItemView.findViewById(R.id.play);
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// play streaming
SingleAudio sd = getItem(position);
if (sd == null) {
return;
}
dialog = AppUtil.showProgress(adapterActivity);
sd.playerInstance.setDialog(dialog);
if (sd.playerInstance.inPlay()) {
sd.playerInstance.stop();
} else {
sd.playerInstance.play();
}
}
});
其中,在我调用showProgress()
之后,我将对话框传递给播放器实例,以便在开始播放歌曲并将按钮Play更改为停止播放后关闭该对话框.我也试着把showProgress()
放在adapterActivity.runOnUiThread
里面,但它没有立即显示出来.我想立即显示,以防止用户再次点击按钮,并等待如果播放过程完全开始…我会错过什么考虑?