多次刷新progress bar
不显示!
我正在刷新数据。这一进展并不准确。我想要的是每次刷新时都能正确显示进度。
多次刷新progress bar
不显示!
我正在刷新数据。这一进展并不准确。我想要的是每次刷新时都能正确显示进度。
@ demo
活性
package com.hjyt.youmi.testdemo;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import androi
d.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView mRecyclerView;
private Button mBtn;
private List<String> mDataList = new ArrayList<>();
private RecyclerViewAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
for (int i = 0; i < 10; i++) {
mDataList.add(String.valueOf(i));
}
mRecyclerView = findViewById(R.id.recycler_view);
mBtn = findViewById(R.id.btn_notify);
mBtn.setOnClickListener(this);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new RecyclerViewAdapter();
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_notify:
mAdapter.notifyDataSetChanged();
break;
}
}
class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ProgressViewHolder> {
@Override
public ProgressViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.item_progress_layout, parent, false);
return new ProgressViewHolder(view);
}
@Override
public void onBindViewHolder(ProgressViewHolder holder, int position) {
setColors(holder.progressBar, Color.WHITE, Color.RED);
java.util.Random r = new java.util.Random();
holder.progressBar.setProgress(r.nextInt(100));
holder.textView.setText("position: " + position + "\n progress: " + holder.progressBar.getProgress());
}
@Override
public int getItemCount() {
return mDataList.size();
}
private void setColors(ProgressBar progressBar, int backgroundColor, int progressColor) {
//Background
ClipDrawable bgClipDrawable = new ClipDrawable(new ColorDrawable(backgroundColor), Gravity.LEFT, ClipDrawable.HORIZONTAL);
bgClipDrawable.setLevel(10000);
//Progress
ClipDrawable progressClip = new ClipDrawable(new ColorDrawable(progressColor), Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Setup LayerDrawable and assign to progressBar
Drawable[] progressDrawables = {bgClipDrawable, progressClip, progressClip};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
progressLayerDrawable.setId(0, android.R.id.background);
progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
progressLayerDrawable.setId(2, android.R.id.progress);
progressBar.setProgressDrawable(progressLayerDrawable);
}
class ProgressViewHolder extends RecyclerView.ViewHolder {
ProgressBar progressBar;
TextView textView;
ProgressViewHolder(View itemView) {
super(itemView);
progressBar = itemView.findViewById(R.id.progress);
textView = itemView.findViewById(R.id.tv_desc);
}
}
}
}
活动xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hjyt.youmi.testdemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/btn_notify"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/btn_notify"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="更新数据"
android:textColor="@android:color/white"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
adapter xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ProgressBar
android:id="@+id/progress"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="0dp"
android:layout_height="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:max="100"
android:progress="50"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_desc"/>
<View
android:layout_width="0dp"
android:layout_height="1px"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@android:color/darker_gray"
app:layout_constraintTop_toBottomOf="@+id/progress"/>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
您可以使用以下方法显示和隐藏进度条:
public static void showProgress(String message, Context context, boolean cancellable) {
if (context == null)
return;
if (checkProgressOpen())
return;
dialog = new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage(message == null ? "Please wait..." : (message));
dialog.setCancelable(cancellable);
try {
dialog.show();
} catch (Exception e) {
// catch exception for activity paused and dialog is going to be
// show.
}
}
public static boolean checkProgressOpen() {
if (dialog != null && dialog.isShowing())
return true;
else
return false;
}
public static void cancelProgress() {
if (checkProgressOpen()) {
try {
dialog.dismiss();
} catch (Exception e) {
}
dialog = null;
}
}
将此代码放在实用程序文件中,并在需要的地方使用它。您也可以在参数中传递消息
答案 1 :(得分:0)
我自己解决了这个问题,而且我不需要进度条来使用View和背景。
private void setColors(View viewBg, View view, int progressColor, int progress) {
int width = viewBg.getWidth();
int progress1 = width / 100;
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams();
params.width = progress1 * progress;
view.setLayoutParams(params);
view.setBackgroundColor(progressColor);
}