android 中没有对话框的进度条
Progressbar without a dialog in android
我想在我的 android 项目中使用进度条,其中用户 select 有多个文件要上传到服务器。
我想显示每个文件上传进度的进度条。我不想使用 ProgressDialog 来完成这项任务。我计划在 recyclerview 中显示所有 selected 文件上传进度(每个进度条连续),以便用户滚动浏览所有进度条。
我没有找到任何好的教程。我曾在 recyclerview 上工作,但整齐地放置进度条在这里很重要。有什么建议吗?
您只需在 recycler_item.xml 上设置一个 ProgressBar
(将 recycler_item 替换为您的实际项目文件名),它将在自定义 Adapter
上膨胀。
然后每次调用 onBindViewHolder()
时,将 ProgressBar
的可见性设置为 VISIBLE
。
快速示例:
ProgressBarAdapter.java(真正的魔法所在)。
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();
}
}
MainActivity.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));
}
}
activity_main.xml
<?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>
recycler_item.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>
结果:
我想在我的 android 项目中使用进度条,其中用户 select 有多个文件要上传到服务器。
我想显示每个文件上传进度的进度条。我不想使用 ProgressDialog 来完成这项任务。我计划在 recyclerview 中显示所有 selected 文件上传进度(每个进度条连续),以便用户滚动浏览所有进度条。
我没有找到任何好的教程。我曾在 recyclerview 上工作,但整齐地放置进度条在这里很重要。有什么建议吗?
您只需在 recycler_item.xml 上设置一个 ProgressBar
(将 recycler_item 替换为您的实际项目文件名),它将在自定义 Adapter
上膨胀。
然后每次调用 onBindViewHolder()
时,将 ProgressBar
的可见性设置为 VISIBLE
。
快速示例:
ProgressBarAdapter.java(真正的魔法所在)。
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();
}
}
MainActivity.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));
}
}
activity_main.xml
<?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>
recycler_item.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>
结果: