0
是否有可能通过这样做动态添加观点:我们如何在listview中的已经充满膨胀的视图中动态添加视图?
getView():
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HeaderViewHolder viewHolder = null;
Map.Entry<String, List<FindTopicFragment.MaintainTopicModel>> hashMap = getItem(position);
if (null == convertView) {
convertView = inflater.inflate(R.layout.post_group_list_header, null);
viewHolder = new HeaderViewHolder();
viewHolder.header_text = (TextView) convertView.findViewById(R.id.header_text);
viewHolder.linearLayout = (LinearLayout) convertView.findViewById(R.id.linearItems);
convertView.setTag(viewHolder);
setItems(hashMap.getValue().get(position).topicModels, position, viewHolder);
} else {
viewHolder = (HeaderViewHolder) convertView.getTag();
}
viewHolder.header_text.setText("" + hashMap.getKey());
return convertView;
}
添加视图在已充气视图列表视图:
private void setItems(List<FindTopicFragment.MaintainTopicModel> modelList, int position, HeaderViewHolder holder) {
holder.linearLayout.removeAllViews();
System.out.println("===============================");
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.post_group_list_row, null);
TextView group_list_header = (TextView) view.findViewById(R.id.group_list_header);
for (FindTopicFragment.MaintainTopicModel model : modelList) {
System.out.println(model.topicKey);
group_list_header.setText("" + model.topicKey);
holder.linearLayout.addView(group_list_header);
}
System.out.println("===============================");
}
我已经解决了它这样做。在for循环中填充一个视图,每次都会创建一个新行。
private void setItems(List<FindTopicFragment.MaintainTopicModel> modelList, int position, HeaderViewHolder holder) {
holder.linearLayout.removeAllViews();
System.out.println("===============================");
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (FindTopicFragment.MaintainTopicModel model : modelList) {
System.out.println(model.topicKey);
View view = inflater.inflate(R.layout.post_group_list_row, null);
TextView group_list_header = (TextView) view.findViewById(R.id.group_list_header);
group_list_header.setText("" + model.topicKey);
holder.linearLayout.addView(group_list_header);
}
System.out.println("===============================");
}
谢谢你们的宝贵建议。
您是否面临任何问题? –
是的,这里是: java.lang.IllegalStateException:指定的子项已经有父项。您必须先调用子对象的父对象的removeView()。 –
尝试使用, convertView = inflater.inflate(R.layout.post_group_list_header,parent,false);而不是convertView = inflater.inflate(R.layout.post_group_list_header,null); –