每个 GridView 元素的进度条
Progress bar at every GridView element
如何将 ProgressBar
添加到 Android 应用程序中 GridView
的每一项?我需要在 GridView
元素的右上角显示进度条。
您需要创建一个自定义适配器,它会扩充包含 ProgressBar 的视图。然后在运行时你需要更新 ProgressBar 的进度。这是一些非常基本的示例,可以帮助您入门。
row_grid_view
只是一个包含进度条的布局。您将不得不尝试一下可用的布局,看看什么适合您的需要。一个友好的警告:如果布局是适配器的一部分,不要使用 RelativeLayout。如果您不知道自己在做什么,它们的使用成本可能会非常昂贵:)
ProgressBarAdapter
是一个显示 ProgressItem
列表的适配器。这些项目仅包含它们自己的进度,因此它们可用于更新每个 ProgressBar 的进度。
row_grid_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:max="100"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
</FrameLayout>
进度条适配器
public class ProgressShowingAdapter extends BaseAdapter {
private ArrayList<ProgressItem> mData;
private LayoutInflater mInflater;
public ProgressShowingAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public ProgressItem getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
// if your items have any unique ids, return that instead
return position;
}
public void setData(List<ProgressItem> newData) {
this.mData.clear();
if (newData != null && !newData.isEmpty()) {
mData.addAll(newData);
}
}
private static class ViewHolder {
private ProgressBar mProgress;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// view holder pattern
ViewHolder vh = null;
if (convertView == null) {
vh = new ViewHolder();
convertView = mInflater.inflate(R.layout.row_grid_view, parent, false);
vh.mProgress = (ProgressBar) convertView.findViewById(R.id.progress);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
ProgressItem mItem = getItem(position);
vh.mProgress.setProgress(mItem.getProgress());
// do the remaining of the stuff here
return convertView;
}
}
进度项
public class ProgressItem {
private int mProgress;
public ProgressItem(int mProgress) {
this.mProgress = mProgress;
}
public int getProgress() {
return mProgress;
}
}
你可以使用这个教程
http://www.tutorialspoint.com/android/android_grid_view.htm
我认为这对你有用
如何将 ProgressBar
添加到 Android 应用程序中 GridView
的每一项?我需要在 GridView
元素的右上角显示进度条。
您需要创建一个自定义适配器,它会扩充包含 ProgressBar 的视图。然后在运行时你需要更新 ProgressBar 的进度。这是一些非常基本的示例,可以帮助您入门。
row_grid_view
只是一个包含进度条的布局。您将不得不尝试一下可用的布局,看看什么适合您的需要。一个友好的警告:如果布局是适配器的一部分,不要使用 RelativeLayout。如果您不知道自己在做什么,它们的使用成本可能会非常昂贵:)
ProgressBarAdapter
是一个显示 ProgressItem
列表的适配器。这些项目仅包含它们自己的进度,因此它们可用于更新每个 ProgressBar 的进度。
row_grid_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:max="100"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
</FrameLayout>
进度条适配器
public class ProgressShowingAdapter extends BaseAdapter {
private ArrayList<ProgressItem> mData;
private LayoutInflater mInflater;
public ProgressShowingAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public ProgressItem getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
// if your items have any unique ids, return that instead
return position;
}
public void setData(List<ProgressItem> newData) {
this.mData.clear();
if (newData != null && !newData.isEmpty()) {
mData.addAll(newData);
}
}
private static class ViewHolder {
private ProgressBar mProgress;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// view holder pattern
ViewHolder vh = null;
if (convertView == null) {
vh = new ViewHolder();
convertView = mInflater.inflate(R.layout.row_grid_view, parent, false);
vh.mProgress = (ProgressBar) convertView.findViewById(R.id.progress);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
ProgressItem mItem = getItem(position);
vh.mProgress.setProgress(mItem.getProgress());
// do the remaining of the stuff here
return convertView;
}
}
进度项
public class ProgressItem {
private int mProgress;
public ProgressItem(int mProgress) {
this.mProgress = mProgress;
}
public int getProgress() {
return mProgress;
}
}
你可以使用这个教程 http://www.tutorialspoint.com/android/android_grid_view.htm 我认为这对你有用