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!
There are three parents, each having a textview and a switch.
The child of first parent has:
The child of second parent has:
The child of third parent has:
Please check this photoshop mockup, which is want I'd like to impliment:
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.