ExpandableListView显示没有子项的组的指示符

rog*_*gcg 5 android cursor expandablelistview

我正在ExpandableListView使用数据库创建数据.为此,我正在使用a,CursorTreeAdapter并使用Cursor包含从数据库检索的数据的对象填充它.

我认为,默认情况下Android会认为没有孩子的群体"不可扩展".

但是它仍然显示行上的展开图标,当我单击它时,它什么都不做.我不希望它显示此图标.

我只想让有孩子的组显示扩展图标,但这不会发生.它显示所有行的扩展图标(包含子项且没有子项的行).


UPDATE

我已经研究了我的代码,我已经看到问题主要在于groupIndicator为组设置.但是我尝试了很多方法,比如创建一个选择器并根据状态和可扩展设置它的drawable,如下所示:

<?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>
Run Code Online (Sandbox Code Playgroud)

但是当该组崩溃时,它会隐藏所有组,包括那些有孩子的组(因为Android将崩溃的组识别为空).

是否有更好的方法只为有孩子的群体设置指标?


这是我的代码.

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")));
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Pat*_*oos 8

在你的xml中添加以下的ExpandableListView:

    android:groupIndicator="@android:color/transparent"
Run Code Online (Sandbox Code Playgroud)

比适配器中的每个组项目视图都需要其内部指示器.例如,您可以在.xml的右侧添加ImageView.

然后在适配器中执行以下操作:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)