2014-02-15 45 views
2

我有一个问题,我想添加不同的元素到可扩展listView中的一个组,如图片。有可能的? enter image description here我知道,我必须在适配器中使用getChildView方法吗?可扩展列表中的组中的不同元素ListView

case 2: 
        if (childPosition==0){ 
         infalInflater = (LayoutInflater) this._context 
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

         convertView = infalInflater.inflate(R.layout.group_item, null); 
         txtListChild=(TextView)convertView 
           .findViewById(R.id.tv_group_name) ; 
         txtListChild.setText("Вот они, родненькие условия использования)"); 
        } 
        else{ 
        c = db.getAccamulativeListOfCompany(companyID); 
        infalInflater = (LayoutInflater) this._context 
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

        convertView = infalInflater.inflate(R.layout.item_accumulative_list, null); 
        txtListChild = (TextView) convertView 
          .findViewById(R.id.lblListItem); 
        Log.d("myLogs", "group_pos=" + groupPosition + "," + childPosition); 
        TextView tvDiscount = (TextView) convertView.findViewById(R.id.tv_accumulative_discount); 
        ImageView ivAccamulative = (ImageView) convertView.findViewById(R.id.iv_accamulative_item); 

        if (c != null) { 
         if (c.moveToFirst()) { 
          c.moveToPosition(childPosition-1); 


          txtListChild.setText(c.getString(1) + " руб."); 

          tvDiscount.setText(c.getString(2) + "%"); 
          File imgFile = new File(c.getString(4)); 
          if (imgFile.exists()) { 
           Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
           ivAccamulative.setImageBitmap(myBitmap); 
          } 
         } 

        } 
        } 
        break; 

回答

4

是的,我假设这些是你想要不同类型的子视图。

在这种情况下,覆盖下面的函数,返回您希望在getChildTypeCount中生成的不同子布局的数量,以及使用组和孩子分配与每个布局对应的唯一整数(1为count)的逻辑定位为getChildType中的参数。

在getChildView方法中,根据getChildType方法的逻辑,您将膨胀不同的布局。在这里调用它可能是一个好主意,并使用返回值切换来确定哪个布局膨胀。

如果您这样做,您不必在每次回收视图时覆盖视图布局,ExpandedListView将负责确保回收视图时,您只会获得与视图类型匹配的视图类型您的getChildType方法中的逻辑。

简单的选择是在getView/getChildView方法中简单地夸大任何您需要的视图,但这当然不是非常有效。

public class MyAwesomeAdapter extends BaseExpandableListAdapter { 


@Override 
public int getChildType(int groupPosition, int childPosition) { 

      // Return a number here, 1 to whatever you return in getChildTypeCount. 
      // Each number should correspond to a particular layout, using group 
      // and child position to determine which layout to produce. 

    return super.getChildType(groupPosition, childPosition); 
} 

@Override 
public int getChildTypeCount() { 

      // Return the number of distinct layouts you expect to create 

    return super.getChildTypeCount(); 
} 

@Override 
public View getChildView(int gp, int cp, boolean arg2, View view, ViewGroup arg4) { 

    if(view == null){ 

     switch(getChildType(gp, cp)){ 

     case 1: 
      //inflate type 1 
      break; 


     case 2: 
      //inflate type 2 
      break; 

     .... 


     } 
} 

} }

+0

我尝试,但我已经问题,我有1个项目(文本),并在光标4项,在结果我有:第一项-text和3下一个是从光标。我从光标中松开1项。 –

+0

我在 –

+0

以下添加了我的代码,调整你的孩子数量以便给你一个额外的孩子?由于您要添加一个不在光标中的项目。看起来你已经相应地移动了你的光标的索引。 – NameSpace