ExpandableListView是显示指示器组没有子(ExpandableListView is

2019-06-26 00:20发布

我创建一个ExpandableListView与数据库中的数据。 对于这一点,我使用的是CursorTreeAdapter ,我用填充它Cursor其中包含我从数据库中检索数据对象。

我认为,由默认的Android将考虑组没有子“不扩张”。

但是它仍然显示该行展开图标,当我点击它,它什么都不做。 我不希望它显示该图标。

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


UPDATE

我研究我的代码,我已经看到了这个问题基本上是在设置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")));
            }
        }
    }

Answer 1:

在你的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);
    }
}


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


Answer 3:

为什么不离开组没有孩子了呢?

更改您的查询,让您只将有儿童群体。



文章来源: ExpandableListView is showing indicator for groups with no child