首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ExpandableListAdapter -如何处理getChildrenCount?

ExpandableListAdapter -如何处理getChildrenCount?
EN

Stack Overflow用户
提问于 2014-11-10 21:18:31
回答 1查看 1.3K关注 0票数 1

首先我要说的是,我不理解getChildrenCount,我的意思是,它并没有真正按照文档所建议的方式运行。

首先,我有:

代码语言:javascript
复制
@Override
public int getChildrenCount(int groupPosition) {
    Log.i("LL", "Picking list size for selected component: " + ((LoadList)parentItems.get(groupPosition)).getPickingList().size());
    return ((LoadList)parentItems.get(groupPosition)).getPickingList().size();
}

当输出到LogCat时,它指出返回的.size()是26个子代,这正是我所期望的。然而,在设备上,子项目将(似乎)永远存在…?我们说的是成千上万的孩子?!

因此,对于一个测试,我将其更改为:

代码语言:javascript
复制
@Override
public int getChildrenCount(int groupPosition) {
    Log.i("LL", "Picking list size for selected component: " + ((LoadList)parentItems.get(groupPosition)).getPickingList().size());
    //return ((LoadList)parentItems.get(groupPosition)).getPickingList().size();
    return 1;
}

它现在在设备和LogCat上显示/报告26个孩子。太棒了!但是为什么呢?

根据文档:

代码语言:javascript
复制
public abstract int getChildrenCount (int groupPosition)

Added in API level 1
Gets the number of children in a specified group.

Parameters
groupPosition   the position of the group for which the children count should be returned
Returns
the children count in the specified group

我的问题很简单。怎么一回事?我认为我必须根据我的数组适配器数据手动返回大小,但根据我的测试,如果您指定一个完全随机的值,而不是实际的值,它将正确工作。或者我根本不需要重写,让操作系统来处理它?

EN

回答 1

Stack Overflow用户

发布于 2014-11-10 22:13:09

getChildrenCount(int groupPosition)的工作原理类似于简单列表视图的getCount(),不同之处在于"getChilderenCount“将返回在方法中传递的特定组下的子代数量。

请查看所提供的适配器示例

代码语言:javascript
复制
public class CustomExpandableAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private String[] mQuestionArray = null;
    private String[] mOptionArray = null;
    private int mSelectedPosition = -1;

    public CustomExpandableAdapter(Context context, String[] questionArray, String[][] optionArray) {
        mQuestionArray = questionArray;
        optionArray = optionArray;
        mContext = context;

    }

    @Override
    public int getGroupCount() {
        return mQuestionArray.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mOptionArray[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mQuestionArray[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mOptionArray[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        HeaderViewHolder headerViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.faq_header_text_layout, null);
            headerViewHolder = new HeaderViewHolder(convertView);
            convertView.setTag(headerViewHolder);
        }
        headerViewHolder = (HeaderViewHolder) convertView.getTag();

        headerViewHolder.mGroupHeader.setText(mQuestionArray[groupPosition]);
//        headerViewHolder.mGroupHeader.setTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.text_smallest));

        if (mSelectedPosition != -1 && mSelectedPosition == groupPosition) {
            headerViewHolder.mGroupHeader.setTextColor(mContext.getResources().getColor(R.color.blue_info_app));
        } else {
            headerViewHolder.mGroupHeader.setTextColor(Color.BLACK);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.faq_textview_layout, null);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        }
        childViewHolder = (ChildViewHolder) convertView.getTag();

        childViewHolder.mChildTitle.setText(mOptionArray[groupPosition][childPosition]);
//        childViewHolder.mChildTitle.setTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.text_smallest));
        childViewHolder.mChildTitle.setTextColor(Color.BLACK);
        childViewHolder.mChildTitle.setMovementMethod(LinkMovementMethod.getInstance());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private static class HeaderViewHolder {
        final TextView mGroupHeader;

        private HeaderViewHolder(View group) {
            mGroupHeader = (TextView) group.findViewById(R.id.txv_faq_header_text_layout);
        }
    }

    private static class ChildViewHolder {
        final TextView mChildTitle;

        private ChildViewHolder(View group) {
            mChildTitle = (TextView) group.findViewById(R.id.txv_faq_textview_layout);
        }
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        if (observer != null) {
            super.unregisterDataSetObserver(observer);
        }
    }

    public void setSelectedPosition(int selectedPosition) {
        mSelectedPosition = selectedPosition;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26844790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档