简体   繁体   中英

is it possible to impliment expandablelistview in which each child has different item/layouts

First of all, I'm new to programming. Is it possible to create custom expandable list in each child that has a different layout? If yes, please help me out!

  1. There are three parents, each having a textview and a switch.

    1. The child of first parent has:

      1. textview
      2. Seekbar
      3. edittext
    2. The child of second parent has:

      1. textview
      2. timepicker
      3. textview
      4. edittext
    3. The child of third parent has:

      1. textview
      2. edittext

Please check this photoshop mockup, which is want I'd like to impliment:

https://www.dropbox.com/s/8bqii0d4vt43wa8/changed%20layout.jpg

  • Should I create three different XML for each child, or is it the adapter who plays a role here?
  • If not possible with expandable list, HOW can I implement this ?
  • Any suggestion to guide, tutorial is appreciated. :)

You can in a custom adapter but you have to create the respective layouts. Use something like the following in your getView() method.

switch (groupPosition) {
     case 0:
         convertView = inflater.inflate(R.layout.group1, null);
         break;
     case 1:
         convertView = inflater.inflate(R.layout.group2, null);
         break;
}

As you are using two types of views of children items of the group. So you have to override the following two methods

final int CHILDVIEW_ONE = 0;
final int CHILDVIEW_TWO = 1;
final int CHILDVIEW_TYPE_COUNT = CHILDVIEW_TWO + 1;
@Override
public int getChildTypeCount() {
    return CHILDVIEW_TYPE_COUNT;
}


@Override
public int getChildType(int groupPosition, int childPosition)
{
      if(childObject instanceOf item1)
         return CHILDVIEW_ONE;
      else if (childObject instance item2)
         return CHILDVIEW_TWO;
return super.getChildType(int groupPosition, int childPosition);
}

Then in your getChildView() Do the following:

public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
     ....
     if(childObject instanceOf item1){
         //handle the first child view
     }else if (childObject instanceOf item2){
         //handle the second child view
     }
     ....
}

You have to override both getChildType and getChildTypeCount in order to use the concept of viewholders which optimizes the Memory peroformance drawing a scrollview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM