在点击事件中,根据当前下载状态执行相应的操作。
具体的下载流程如下:
- 点击进度条时,会检查当前下载状态,如果没有完成下载,则进入相应的逻辑判断。
- 如果没有安装或等待中,则开始下载并显示进度条和进度文本。
- 如果已经在下载中,则暂停下载,并更新按钮文本为"继续"。
- 如果下载已经完成,显示提示信息。
在下载过程中,通过使用
另外,点击"取消"按钮会重置所有下载状态,并将进度条和文本恢复到初始状态。
请注意,这只是一个简单的示例,并不包含真正的网络下载功能,而是通过随机数来模拟下载进度和延迟操作。你可以根据自己的需求进行修改和扩展。
下载(更新)逻辑
布局文件内容
<ProgressBar android:id="@+id/progressBar" style="@style/CustomProgressBarStyle" android:layout_width="200dp" android:layout_height="50dp" android:layout_alignParentStart="true" android:layout_alignParentBottom="true" android:layout_marginStart="105dp" android:layout_marginBottom="402dp" android:max="100" android:min="0" /> ? <TextView android:id="@+id/textView" android:layout_width="50dp" android:layout_height="wrap_content" android:layout_alignStart="@+id/progressBar" android:layout_alignTop="@+id/progressBar" android:layout_alignEnd="@+id/progressBar" android:layout_alignBottom="@+id/progressBar" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:gravity="center" android:text="下载" android:textColor="@color/black" android:textSize="20dp" /> ? <ImageButton android:id="@+id/btn_cancel" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentBottom="true" android:layout_marginStart="16dp" android:layout_marginBottom="417dp" android:layout_toEndOf="@+id/progressBar" android:visibility="invisible" android:background="@mipmap/cancel" />
下载逻辑部分
下载中操作
进度条变化设置
progressBar.setProgress(allProgress); textView.setText(allProgress + "%"); btnCancel.setVisibility(View.VISIBLE); handler.postDelayed(runnable, getRandomIndex());
下载过程中再次点击暂停
textView.setText("继续"); handler.removeCallbacks(runnable);
下载过程中点击取消
btnCancel.setOnClickListener(v -> { isDowning = false; installedSucceed = false; isInstalling = false; isWaiting = false; isFinishedDownloading = false; allProgress = 0; btnCancel.setVisibility(View.INVISIBLE); handler.removeCallbacks(runnable); progressBar.setProgress(0); textView.setText("下载"); });
下载完成操作
进入等待状态
progressBar.setProgress(0); allProgress = 0; btnCancel.setVisibility(View.INVISIBLE); textView.setText("等待中"); isWaiting = true;
进入安装状态
textView.setText("安装中"); isWaiting = false; isInstalling = true;
安装完成
if (installedSucceed) { textView.setText("打开"); Toast.makeText(this, "xxx安装完成", Toast.LENGTH_SHORT).show(); isFinishedDownloading = true; } else { Toast.makeText(this, "xxx安装失败,请检查网络后重试", Toast.LENGTH_SHORT).show(); textView.setText("重试"); btnCancel.setVisibility(View.VISIBLE); isDowning = !isDowning; }
其他
模拟下载进度
private int getRandomIndex() { Random random = new Random(); return random.nextInt(1001) + 100; } ? private int getRandomProgress() { Random random = new Random(); return random.nextInt(10) + 1; }
全部代码
private void downLoading() { progressBar.setOnClickListener(v -> { Log.d(TAG, "downLoading: "); btnCancel.setVisibility(View.INVISIBLE); if (!isFinishedDownloading) { if (!isInstalling && !isWaiting) { textView.setText(allProgress + "%"); if (!isDowning) { runnable = () -> { allProgress += getRandomProgress(); if (allProgress <= FINISHED) { progressBar.setProgress(allProgress); textView.setText(allProgress + "%"); btnCancel.setVisibility(View.VISIBLE); handler.postDelayed(runnable, getRandomIndex()); } else { progressBar.setProgress(0); allProgress = 0; btnCancel.setVisibility(View.INVISIBLE); textView.setText("等待中"); isWaiting = true; new Handler().postDelayed(() -> { textView.setText("安装中"); isWaiting = false; isInstalling = true; new Handler().postDelayed(() -> { isInstalling = false; if (installedSucceed) { textView.setText("打开"); Toast.makeText(this, "xxx安装完成", Toast.LENGTH_SHORT).show(); isFinishedDownloading = true; } else { Toast.makeText(this, "xxx安装失败,请检查网络后重试", Toast.LENGTH_SHORT).show(); textView.setText("重试"); btnCancel.setVisibility(View.VISIBLE); isDowning = !isDowning; } installedSucceed = !installedSucceed; }, 2000); }, 3000); } }; handler.postDelayed(runnable, getRandomIndex()); } else { textView.setText("继续"); handler.removeCallbacks(runnable); } isDowning = !isDowning; } } else { Toast.makeText(this, "xxx安装完成", Toast.LENGTH_SHORT).show(); } }); btnCancel.setOnClickListener(v -> { isDowning = false; installedSucceed = false; isInstalling = false; isWaiting = false; isFinishedDownloading = false; allProgress = 0; btnCancel.setVisibility(View.INVISIBLE); handler.removeCallbacks(runnable); progressBar.setProgress(0); textView.setText("下载"); }); }