Frage

i have a problem, i want to add different elements to one Group in expandable listView, like in a picture. It is possible?enter image description here I know, that i must to cgange my getChildView method in adapter?

 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;
War es hilfreich?

Lösung

yes, I assume those are child views you want different types of.

In that case, override the functions below, returning the number of different child layouts you expect to produce in getChildTypeCount, and the logic for assigning a unique integer (1 to count), corresponding to each layout, using group and child position as parameters, in getChildType.

In the getChildView method, then you will inflate your different layouts, according to the logic of your getChildType method. Calling it here is probably a good idea, and using switch on the return value to determine which layout to inflate.

You don't have to overwrite the layout of views each time they are recycled if you do this, the ExpandedListView will take care of ensuring that when the views are recycled, you will only get the type of view that matches the logic in your getChildType method.

The uncomplicated alternative is to simply inflate whatever view you need in the getView / getChildView methods, but of course that's not terribly efficient.

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;

       ....


       }
}

} }

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top