ExpandableListView正显示出指标组没有子 [英] ExpandableListView is showing indicator for groups with no child

查看:342
本文介绍了ExpandableListView正显示出指标组没有子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个 ExpandableListView 与数据库中的数据。为此,我使用了一个 CursorTreeAdapter ,我填充它与光标对象,它包含我从检索数据数据库。

我认为,用默认的Andr​​oid会考虑组没有孩子的不能扩展的。

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

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


更新

我研究我的code,我已经看到了这个问题基本上是在设置 groupIndicator 的群体。不过,我已经尝试了很多像创建一个选择器,并设置它的方法绘制真实基于状态和可扩展的,像这样的:

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目安卓state_empty =真
          机器人:state_expanded =假
          机器人:可绘制=@机器人:彩色/透明/>
    <项目机器人:可绘制=@可绘制/ expander_ic_maximized/>
< /选择器>
 

但是,当该集团倒塌它会隐藏所有的群体,包括那些有孩子(因为Android的认可折叠组为空)。

有关儿童设置的指标只针对团体的任何更好的办法?


下面是我的code。

 公共类ContactsExpandableListAdapter扩展CursorTreeAdapter
{

    TextView的mContactNameTextView,mContactNumberTextView;
    光标mChildrenCursor = NULL;

    公共ContactsExpandableListAdapter(光标指针,上下文语境)
    {
        超(光标,背景);
    }

    @覆盖
    受保护的光标getChildrenCursor(光标指针)
    {
        如果(mContactId == -1)
            mContactId = NULL;

        返回mController.getContactById(mContactId);
    }

    公众诠释getChildrenCount(INT groupPosition)
    {
        返回mChildrenCursor.getCount();
    }

    @覆盖
    受保护的视图newGroupView(上下文的背景下,光标光标,布尔paramBoolean,ViewGroup中的ViewGroup)
    {
        查看查看= LayoutInflater.from(上下文).inflate(R.layout.filterbycontact,ViewGroup中,假);

        mContactNameTextView =(TextView中)view.findViewById(R.id.contact_name);

        如果(cursor.getString(cursor.getColumnIndex(CONTACT_NAME))== NULL)mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex(contact_number)));
            其他
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex(CONTACT_NAME)));
            view.setTag(cursor.getString(cursor.getColumnIndex(CONTACT_ID)));
            返回查看;
        }

        @覆盖
        受保护的视图newChildView(上下文的背景下,光标光标,布尔paramBoolean,ViewGroup中的ViewGroup)
        {
            查看查看= LayoutInflater.from(上下文).inflate(R.layout.filterbycontact,ViewGroup中,假);

            如果(cursor.getString(cursor.getColumnIndex(CONTACT_NAME))== NULL)
            {
                mContactNameTextView =(TextView中)view.findViewById(R.id.contact_name);
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex(contact_number)));
            }
            其他
            {
                mContactNumberTextView =(TextView中)view.findViewById(R.id.contact_number);
                mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex(contact_number)));
            }

            view.setTag(cursor.getString(cursor.getColumnIndex(contact_number)));
            返回查看;
        }

        @覆盖
        保护无效bindGroupView(查看视图,上下文paramContext,光标光标,布尔paramBoolean)
        {
            mContactNameTextView =(TextView中)view.findViewById(R.id.contact_name);
            如果(cursor.getString(cursor.getColumnIndex(CONTACT_NAME))== NULL)
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex(contact_number)));
            其他
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex(CONTACT_NAME)));

view.setTag(cursor.getString(cursor.getColumnIndex(CONTACT_ID)));
        }

        @覆盖
        保护无效bindChildView(查看视图,上下文的背景下,光标光标,布尔paramBoolean)
        {
            如果(cursor.getString(cursor.getColumnIndex(CONTACT_NAME))== NULL)
            {
                mContactNameTextView =(TextView中)view.findViewById(R.id.contact_name);
                mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex(contact_number)));
            }
            其他
            {
                mContactNumberTextView =(TextView中)view.findViewById(R.id.contact_number);
                mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex(contact_number)));
            }
        }
    }
 

解决方案

在你的XML中添加如下因素来ExpandableListView:

 安卓groupIndicator =@机器人:彩色/透明
 

比每个组项目查看你的适配器需要它的内部指标。可以例如添加的ImageView右侧在你的.xml

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

  @覆盖
保护无效bindGroupView(查看视图,上下文paramContext,光标光标,布尔paramBoolean)
    ...

    如果(getChildrenCount(groupPosition)大于0){
        viewHolder.indicator.setVisibility(View.VISIBLE);
        viewHolder.indicator.setImageResource(
                isExpanded? R.drawable.indicator_expanded:R.drawable.indicator);
    } 其他 {
        viewHolder.indicator.setVisibility(View.GONE);
    }
}
 

I'm creating an ExpandableListView with data from a database. For that, I'm using a CursorTreeAdapter and I populate it with a Cursor object which contains the data I retrieve from database.

I thought that, by default Android would consider the groups with no child "not expandable".

However it still shows the expand icon on the row, and when I click it, it does nothing. I don't want it to show this icon.

I want only the groups that has childs to show the expand icon, which is not happening. It shows the expand icon for all rows (those with child and with no child).


UPDATE

I've studied my code and I've seen that the problem is basically in setting the groupIndicator for the groups. However I've tried a lot of approaches like creating a selector and setting it's drawable based on state and expandable, like this:

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

But when the group is collapsed it hides for all groups including those with children (because Android recognized collapsed groups as empty).

Any better approach for setting the indicator only for groups with children?


Here is my code.

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

解决方案

In your xml add the folowing to ExpandableListView:

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

Than each Group Item View in your Adapter needs its internal indicator. You can for example add a ImageView on the right side in your .xml.

Then in the Adapter you do the following:

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

这篇关于ExpandableListView正显示出指标组没有子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆