2016-02-12 71 views
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("==============================="); 
     } 

謝謝你們的寶貴建議。

+0

您是否面臨任何問題? –

+0

是的,這裏是: java.lang.IllegalStateException:指定的子項已經有父項。您必須先調用子對象的父對象的removeView()。 –

+0

嘗試使用, convertView = inflater.inflate(R.layout.post_group_list_header,parent,false);而不是convertView = inflater.inflate(R.layout.post_group_list_header,null); –

回答

0

group_list_header已經有父母,這是查看。因此,將此添加到另一個視圖組,holder.linearLayout正在給出錯誤。我相信正確的行應該是

holder.linearLayout.addView(view);