是的,这是可能的,您必须创建一个自定义列表视图,并且可以在您想要添加时动态添加项目
创建一个包含您的 listView 的布局
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
创建一个项目布局,它将代表您的列表视图中的单个项目,并创建一个列表视图适配器,它将为您的列表中的每个项目分配您的值
公共类 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);