我正在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")));
}
}
}