ExpandableListAdapter - 如何处理getChildrenCount?

时间:2014-11-10 13:18:31

标签: android expandablelistadapter

首先我要说的是,我并不理解getChildrenCount,而我的意思是,它并没有像文件所说的那样真实地发挥作用。

首先,我有:

@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个孩子,这正是我所期待的。然而,在设备上,儿童用品将继续(看似)永远...?我们正在谈论成千上万的孩子?!

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

@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个孩子。奇妙!但为什么呢?

根据文件:

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

我的问题很简单......发生了什么事?我认为我必须根据我的数组适配器数据手动返回大小,但根据我的测试,如果你指定一个完全随机的值,而不是实际值,它可以正常工作吗?或者我根本不必覆盖,只是让操作系统处理它?<​​/ p>

1 个答案:

答案 0 :(得分:0)

getChildrenCount(int groupPosition)就像简单列表视图的getCount()一样,不同之处在于“getChilderenCount”将返回特定组下的子节点数,该位置在方法中通过。

请查看提供的适配器示例

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