我在一个单独的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里面,但它没有立即显示出来.我想立即显示,以防止用户再次点击按钮,并等待如果播放过程完全开始…我会错过什么考虑?

推荐答案

试试这个:

 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();
        }
    });

Java相关问答推荐

表格栏上的事件过滤器在PFA中不起作用

Java在模块化jar文件中找不到类,但是javap可以

使用包私有构造函数强制子类Java类

我无法将附件发送到NetBeans之外

将关键字与正文中的_Allowed匹配,但带有__Signing可选后缀

为什么使用JDK21获取锁定锁比使用JDK11慢

为什么S的文档中说常量方法句柄不能在类的常量池中表示?

如何在不删除Java中已有内容的情况下覆盖文件内容?

有没有可能在时间范围内得到多种解决方案?

如何使用MapStrCut转换双向链接

try 使用预准备语句占位符获取信息时出现Try-With-Resources错误

当我在Java中有一个Synchronized块来递增int时,必须声明一个变量Volatile吗?

循环不起作用只有第一个元素重复

Java集合:NPE,即使没有添加空值

根据应用程序 Select 的语言检索数据

在WHILE()循环初始化部分中声明和初始化变量的Java语法?

设置背景时缺少Android编辑文本下划线

原始和参数化之间的差异调用orElseGet时可选(供应商)

JavaFX中ListView中的问题

";重复键的值提示唯一约束«;livre_genre_pkey»";例外