5

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

我认为,默认情况下,Android 会考虑没有孩子的组"不可扩展"

但是它仍然在行上显示展开图标,当我单击它时,它什么也不做。我不希望它显示这个图标。

我只希望有孩子的组显示展开图标,这不会发生。它显示所有行的展开图标(有子行和没有子行的行)。


更新

我研究了我的代码,我发现问题基本上在于groupIndicator为组设置。但是我尝试了很多方法,例如创建选择器并根据状态设置它的可绘制和可扩展,如下所示:

<?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>

但是,当组折叠时,它会隐藏所有组,包括有孩子的组(因为 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")));
            }
        }
    }
4

3 回答 3

8

在您的 xml 中将以下内容添加到 ExpandableListView:

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

适配器中的每个组项视图都需要其内部指示器。例如,您可以在 .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);
    }
}
于 2012-07-04T03:44:52.460 回答
2

在您的适配器类中使用:

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 );
    }
}
于 2014-03-03T10:06:35.960 回答
0

为什么不把没有孩子的团体排除在外呢?

更改您的查询,只为您提供将有孩子的组。

于 2012-07-03T02:11:04.093 回答