我想创建一个可滚动的进度条列表。进度条的数量和每个进度条的内容是动态决定的并且在运行时决定。 是否可以在 ListView 中创建并放置进度条?我在互联网上找不到任何相关资源,因此想确认这在当前的 android 框架中是否可行。
最佳答案
是的,这是可能的,您必须创建一个自定义listView,并且可以在您想要添加时动态添加项目
创建一个包含 listView 的布局
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
创建一个项目布局来代表listView的单个项目,并创建一个listViewAdapter来为列表中的每个项目分配值
公共(public)类ListAdapter扩展ArrayAdapter{
private final Context context;
private int layoutResourceID;
private ArrayList<YourDataModel> objects;
public ListAdapter(Context context, int layoutResourceID, ArrayList<YourDataModel> objects) {
super(context, layoutResourceID, objects);
this.context = context;
this.layoutResourceID = layoutResourceID;
this.objects = objects;
}
@Override
public View getView(int position, final View convertView, ViewGroup parent) {
View row = convertView;
final ListHolder listHolder;
if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceID, parent, false);
itemProgressBar = (ProgressBar) row.findViewById(R.id.item_progressbar);
row.setTag(listHolder);
} else {
listHolder = (ListHolder) row.getTag();
}
final DataCategory list = objects.get(position); // get your data object
// assign values to your items of listView
return row;
}
class ListHolder {
ProgressBar itemProgressBar;
}
}
在上面的代码 YourDataModel 中,您的 java 类包含您的数据 最后在你的主集中设置所有使其工作的东西
ListAdapterHome listAdapter = new ListAdapter(YourActivity.this,
R.layout.item, ArrayListOfYourDataModel); // pass the arrayList of your dataModel there
ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(listAdapterHome);
关于android - android中进度条的ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33779107/