ExpandableListView is showing indicator for groups

2019-02-17 12:59发布

问题:

I'm creating an ExpandableListView with data from a database. For that, I'm using a CursorTreeAdapter and I populate it with a Cursor object which contains the data I retrieve from database.

I thought that, by default Android would consider the groups with no child "not expandable".

However it still shows the expand icon on the row, and when I click it, it does nothing. I don't want it to show this icon.

I want only the groups that has childs to show the expand icon, which is not happening. It shows the expand icon for all rows (those with child and with no child).


UPDATE

I've studied my code and I've seen that the problem is basically in setting the groupIndicator for the groups. However I've tried a lot of approaches like creating a selector and setting it's drawable based on state and expandable, like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_empty="true"
          android:state_expanded="false"
          android:drawable="@android:color/transparent"/>
    <item android:drawable="@drawable/expander_ic_maximized" />
</selector>

But when the group is collapsed it hides for all groups including those with children (because Android recognized collapsed groups as empty).

Any better approach for setting the indicator only for groups with children?


Here is my code.

public class ContactsExpandableListAdapter extends CursorTreeAdapter 
{

    TextView mContactNameTextView, mContactNumberTextView;
    Cursor mChildrenCursor = null;

    public ContactsExpandableListAdapter(Cursor cursor, Context context) 
    {
        super(cursor, context);
    }

    @Override
    protected Cursor getChildrenCursor(Cursor cursor) 
    {
        if(mContactId == -1)
            mContactId = null;

        return mController.getContactById(mContactId);
    }

    public int getChildrenCount(int groupPosition)
    {
        return mChildrenCursor.getCount();
    }

    @Override
    protected View newGroupView(Context context, Cursor cursor, boolean paramBoolean, ViewGroup viewGroup) 
    {
        View view = LayoutInflater.from(context).inflate(R.layout.filterbycontact, viewGroup, false);

        mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);

        if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)                   mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
            else
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_name")));
            view.setTag(cursor.getString(cursor.getColumnIndex("contact_id")));
            return view;
        }

        @Override
        protected View newChildView(Context context, Cursor cursor, boolean paramBoolean, ViewGroup viewGroup) 
        {
            View view = LayoutInflater.from(context).inflate(R.layout.filterbycontact, viewGroup, false);

            if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
            {
                mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
            }
            else
            {
                mContactNumberTextView = (TextView) view.findViewById(R.id.contact_number);
                mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
            }

            view.setTag(cursor.getString(cursor.getColumnIndex("contact_number")));
            return view;
        }

        @Override
        protected void bindGroupView(View view, Context paramContext, Cursor cursor, boolean paramBoolean) 
        {
            mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
            if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
            else
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_name")));

view.setTag(cursor.getString(cursor.getColumnIndex("contact_id")));
        }

        @Override
        protected void bindChildView(View view, Context context, Cursor cursor, boolean paramBoolean) 
        {
            if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
            {
                mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
            }
            else
            {
                mContactNumberTextView = (TextView) view.findViewById(R.id.contact_number);
                mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
            }
        }
    }

回答1:

In your xml add the folowing to ExpandableListView:

    android:groupIndicator="@android:color/transparent"

Than each Group Item View in your Adapter needs its internal indicator. You can for example add a ImageView on the right side in your .xml.

Then in the Adapter you do the following:

@Override
protected void bindGroupView(View view, Context paramContext, Cursor cursor, boolean paramBoolean) 
    ...

    if (getChildrenCount(groupPosition) > 0) {
        viewHolder.indicator.setVisibility(View.VISIBLE);
        viewHolder.indicator.setImageResource(
                isExpanded ? R.drawable.indicator_expanded : R.drawable.indicator);
    } else {
        viewHolder.indicator.setVisibility(View.GONE);
    }
}


回答2:

In your adapter class use:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) 
{
    if (getChildrenCount(groupPosition) == 0 ) {
           indicator.setVisibility( View.INVISIBLE );
    } 
    else {
           indicator.setVisibility( View.VISIBLE );
           indicator.setImageResource( isExpanded ? R.drawable.group_expanded : R.drawable.group_closed );
    }
}


回答3:

Why not leave the groups with no children out?

Change your query to give you only groups that will have children.