可扩展列表视图显示没有子项的组的指示器

5
我正在使用来自数据库的数据创建一个ExpandableListView。为此,我使用了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")));
            }
        }
    }
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);
    }
}

1
我该如何在void方法中返回一个视图呢?嘿嘿,也许应该在newGroupView()方法中实现,对吧?顺便问一下,什么是viewHolderindicator - rogcg
抱歉,返回错误,正确的做法是在CursorAdapter中不需要返回视图:D。关于ViewHolder(用于加速列表的模式):http://www.youtube.com/watch?v=wDBM6wVEO70&feature=player_detailpage#t=511s - Patrick Boos
在上面的链接中,他正在逐步介绍ViewHolder。然后他在http://www.youtube.com/watch?v=wDBM6wVEO70&feature=player_detailpage#t=511s中进行了解释。 - Patrick Boos
如果ViewHolder现在太复杂了,就像以前一样使用view.findViewById(...)吧 ;-) - Patrick Boos

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 );
    }
}

1
这里的指标是什么? - Akshay kumar
指示器是 ExpandableListView 右侧的图标,它看起来像一个 V。它显示列表视图的状态,如展开或折叠。 - Raj Kumar

0

为什么不把没有子元素的组排除在外呢?

修改您的查询,只获取将拥有子元素的组。


但是我想展示那些没有子项的条目,只显示联系电话,仅一个不展开的单行。 - rogcg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接