我想在我的android项目中使用progressbar,用户可以选择多个文件上传到服务器。
我想显示每个文件上传进度的进度条。我不想使用ProgressDialog来完成这项任务。我打算在recyclerview中显示所有选定的文件上传进度(连续的每个进度条),以便用户滚动浏览所有进度条。
我没有找到任何好的教程。我一直在使用recyclerview,但是整齐地放置一个进度条在这里很重要。有什么建议吗?
答案 0 :(得分:1)
您只需在recycler_item.xml上设置ProgressBar
(将recycler_item替换为您的实际项目文件名),该Adapter
将在自定义ProgressBar
上充气。
每次调用VISIBLE
时,将onBindViewHolder()
的可见性设置为class ProgressBarAdapter extends RecyclerView.Adapter<ProgressBarAdapter.ViewHolder>{
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ProgressBar progressBar;
public ViewHolder(View itemView){
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text1);
progressBar = (ProgressBar) itemView.findViewById(R.id.progress1);
}
}
private List<String> mFiles;
private Context mContext;
public ProgressBarAdapter(List<String> files){
mFiles = files;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(mContext);
View viewInflated = inflater.inflate(R.layout.recycler_item, parent, false);
return new ViewHolder(viewInflated);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String file = mFiles.get(position);
TextView textView = holder.textView;
textView.setText(file);
holder.progressBar.setVisibility(View.VISIBLE);
holder.progressBar.setIndeterminate(true);
}
@Override
public int getItemCount() {
return mFiles.size();
}
}
。
快速举例:
ProgressBarAdapter.java (真正的神奇之处)。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> files = new ArrayList<>();
files.add("File 1");
files.add("File 2");
files.add("File 3");
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler1);
recyclerView.setAdapter(new ProgressBarAdapter(files));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
<强> MainActivity.java 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- This ProgressBar will get inflated for each ReyclerView item -->
<ProgressBar
android:id="@+id/progress1"
android:visibility="gone"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<强> recycler_item.xml 强>
<expression-component doc:name="Expression">
<![CDATA[
String input = payload.replaceAll("=", ":");
input = input.replaceAll(",$", "");
payload = new org.json.JSONArray("[" + input + "]");]]>
</expression-component>
结果: