ExpandableListView is showing indicator for groups with no child

前端 未结 3 609
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 02:12

I\'m creating an ExpandableListView with data from a database. For that, I\'m using a CursorTreeAdapter and I populate it with a Cursor

相关标签:
3条回答
  • 2021-01-13 02:23

    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 );
        }
    }
    
    0 讨论(0)
  • 2021-01-13 02:31

    Why not leave the groups with no children out?

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

    0 讨论(0)
  • 2021-01-13 02:39

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题