我在一个单独的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()
后,我将对话框传递给播放器实例,以便在歌曲播放开始后关闭对话框,并且按钮播放更改为停止播放....我还尝试将 showProgress()
放入其中也有一个 adapterActivity.runOnUiThread
,但它不会立即显示...我想立即显示以防止用户再次单击按钮并等待播放过程完全开始....我是否错过了需要考虑的事情?
试试这个:
ImageButton buttonPlay = (ImageButton) currentItemView.findViewById(R.id.play);
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Disable the button to prevent multiple clicks
buttonPlay.setEnabled(false);
// Show the progress dialog immediately
dialog = AppUtil.showProgress(adapterActivity);
// Start a background thread for audio playback
new Thread(new Runnable() {
@Override
public void run() {
// Play streaming on the background thread
SingleAudio sd = getItem(position);
if (sd != null) {
sd.playerInstance.setDialog(dialog);
if (sd.playerInstance.inPlay()) {
sd.playerInstance.stop();
} else {
sd.playerInstance.play();
}
}
// Dismiss the progress dialog on the main UI thread
adapterActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
// Re-enable the button when the operation is finished
buttonPlay.setEnabled(true);
}
});
}
}).start();
}
});