是否可以在运行时为 ListView 添加不同的视图?
我遇到了一种情况,我需要使 ListView
的每个项目都有不同的视图。例如, 第一个项目可能有一个 TextView
和一个 ImageView
,第二个项目可能有一个 Button
和一个 TextView
.
我最初的尝试是使用嵌套在项目适配器内的表格布局,但它不起作用。
public View getView(int position, View convertView, ViewGroup parent) {
ItemViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.user_activity_item, null);
holder = new ItemViewHolder();
holder.textViewText = (TextView) convertView.findViewById(R.id.user_activity_item_xml_textview_user_name);
holder.textViewText2 = (TextView) convertView.findViewById(R.id.user_activity_item_xml_textview_user_where);
holder.table = (TableLayout) convertView.findViewById(R.id.user_activity_item_xml_tablelayout_activity_table);
convertView.setTag(holder);
}
else {
holder = (ItemViewHolder) convertView.getTag();
}
holder.textViewText.setText(items.get(position));
holder.textViewText2.setText(items.get(position));
// add a new row
TableRow row = new TableRow(context);
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
ImageView i = new ImageView(context);
TextView t = new TextView(context);
t.setText("Some texts");
row.addView(i);
row.addView(t);
holder.table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
return convertView;
}
每次加载列表时,表都会相应地修改。所以物品的数量不断增加。所以我的问题是,有没有办法处理这种情况?我可以做什么来制作一个动态 ListView
,在其中我可以在运行时为每个项目添加另一个小部件?任何建议将不胜感激。
I ran into a situation where I need to make each item of a ListView
have different views. For example,
the first item might have a TextView
and an ImageView
, where the second item might have a Button
and a TextView
.
My initial attempt was using a table layout nested inside an item adapter, but it doesn't work.
public View getView(int position, View convertView, ViewGroup parent) {
ItemViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.user_activity_item, null);
holder = new ItemViewHolder();
holder.textViewText = (TextView) convertView.findViewById(R.id.user_activity_item_xml_textview_user_name);
holder.textViewText2 = (TextView) convertView.findViewById(R.id.user_activity_item_xml_textview_user_where);
holder.table = (TableLayout) convertView.findViewById(R.id.user_activity_item_xml_tablelayout_activity_table);
convertView.setTag(holder);
}
else {
holder = (ItemViewHolder) convertView.getTag();
}
holder.textViewText.setText(items.get(position));
holder.textViewText2.setText(items.get(position));
// add a new row
TableRow row = new TableRow(context);
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
ImageView i = new ImageView(context);
TextView t = new TextView(context);
t.setText("Some texts");
row.addView(i);
row.addView(t);
holder.table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
return convertView;
}
Each time the list got loaded, the table got modified accordingly. So the number of items keeps increasing. So my question is, is there a way to handle this situation? What can I do to make a dynamic ListView
in which I can add another widget at runtime for each item? Any suggestion would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在方法开始时设置convertView = null。它会起作用的。
Set convertView = null at the start of method. it will work.