shantanu shantanu - 14 days ago 5
Android Question

No child is visible in nested ExpandableListView in android

I am trying to create a nested

expandable list
(3 layer). But sub category list(3rd layer) is not visible using following code.
getChildCount is > 1
but
getChildview
never called in
SecondLevelAdapter
.

-> menu
-> Category
-> sub category


Data structure:



class Menu{
List<Category> categories;
}

class Category{
List<SubCategory> subcategories;
}

public class ParentLevelAdapter extends BaseExpandableListAdapter {
private final Context mContext;
private final List<NavigationMenu> mListDataHeader;

public ParentLevelAdapter(Context mContext, List<NavigationMenu> mListDataHeader) {
this.mContext = mContext;
this.mListDataHeader = mListDataHeader;
}

@Override
public NavCategory getChild(int groupPosition, int childPosition) {
return mListDataHeader.get(groupPosition).categories.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ExpandableListView secondLevelExpListView = new ExpandableListView(this.mContext);
secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, getChild(groupPosition,childPosition)));
return secondLevelExpListView;
}

@Override
public int getChildrenCount(int groupPosition) {
return mListDataHeader.get(groupPosition).categories.size();
}

@Override
public NavigationMenu getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
return this.mListDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

class ViewHolder{
ImageView icon;
TextView name;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.nav_menu_item_view, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.name = (TextView) convertView.findViewById(R.id.menu_name);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(getGroup(groupPosition).name);
holder.icon.setImageResource(getGroup(groupPosition).iconResoluteID);
return convertView;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

public class SecondLevelAdapter extends BaseExpandableListAdapter {
private final Context mContext;
private final List<NavSubcategory> mListDataHeader;
private final NavCategory navCategory;

public SecondLevelAdapter(Context mContext, NavCategory navCategory) {
this.mContext = mContext;
this.mListDataHeader = navCategory.sub_categories;
this.navCategory = navCategory;
}

@Override
public NavSubcategory getChild(int groupPosition, int childPosition) { //ignore group
return mListDataHeader.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return false;
}

class ChildViewHolder{
TextView name;
}

class GroupViewHolder{
TextView name;
}


@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.nav_menu_second_item, parent, false);
holder = new ChildViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.menu_name);
convertView.setTag(holder);
}
else{
holder = (ChildViewHolder) convertView.getTag();
}
holder.name.setText(getChild(groupPosition,childPosition).sub_category_name);
return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}

@Override
public int getChildrenCount(int groupPosition) {
return mListDataHeader.size();
}

@Override
public NavCategory getGroup(int groupPosition) {
return navCategory;
}

@Override
public int getGroupCount() {
return 1;
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

GroupViewHolder groupViewHolder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.nav_menu_second_group_item, parent, false);
groupViewHolder = new GroupViewHolder();
groupViewHolder.name = (TextView) convertView.findViewById(R.id.menu_name);
convertView.setTag(groupViewHolder);
}
else{
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.name.setText(getGroup(groupPosition).category_name);
return convertView;
}
}

Answer

In getChildView of your ParentLevelAdapter replace

final ExpandableListView secondLevelExpListView = new ExpandableListView(this.mContext);

with

final CustomExpandableListView customExpandableListView = new CustomExpandableListView(this.mContext);

where CustomExpandableListView is:

public class CustomExpandableListView extends ExpandableListView {

    Context context;

    public CustomExpandableListView(Context _context)  {
        super(_context);
        context = _context;
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int screenWidth = (int)(metrics.widthPixels); //or whatever you need here for width of the row

        widthMeasureSpec = MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.AT_MOST);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(20000, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
Comments