How to disable expanding for some items in BaseExpandableListAdapter

2.3k views Asked by At

I'd like to disable expandability for some items in the ExpandableListView.

So far I know it can be done form ExpandableListView but I think it's not good to control what to show/why/where NOT from ListView - this task belongs to adapter and there I want to disable expandability.

So how to do it from a class which extends BaseExpandableListAdapter?

So far I've tried to set convertView visibility to View.GONE but it does not work (i.e. it shows some weird View, empty but not View.GONE)

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if (null == convertView) {
        convertView = inflater.inflate(R.layout.system_status_child, parent, false);
    }

    Item item = systemStatus.get(groupPosition);

    if(item.isStatusOk()){
        convertView.setVisibility(View.GONE);
    }
    else{
        convertView.setVisibility(View.VISIBLE);
        ((TextViewMuseo)convertView).setText(item.getChild_text());
    }

    return convertView;
}

system_status_child.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/system_status_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="some text" />
1

There are 1 answers

0
Simas On BEST ANSWER

Just return 0 as the children count for those groups:

@Override
public int getChildrenCount(int groupPosition) {
    if (groupPosition == 0 || groupPosition == 1) {
        return 0;
    } else {
        // return normal children count
    }
}