android - ExpandableListAdapter中getChild和getGroup调用时间?
黄舟
黄舟 2017-04-17 14:43:50
0
1
720

在ExpandableListAdapter的函数getChild()getGroup() getChildrenCount()添加Log

结果没有打出

getChildId()getChildView() getGroupId() getGroupView() getGroupCount()都被调用 并打出了log

想问问未打出log的3个函数分别在什么时候进行调用以及没有打log的原因

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
黄舟

About getChildrenCount

When you click to expand the GroupItem, getChildrenCount() is called to return the number of children of this Group (to avoid array out-of-bounds errors); then the adapter will call getChildView().

@Override
public int getChildrenCount(int groupPosition) {
    Log.d(TAG, "getChildrenCount: groupPosition " + groupPosition);
    return count;
}

About getChildView(), getChild(), getGroup()

We need to overwrite getChildView() to fill the view, so we must first get the data, and the purpose of getChild() is to get the data stored in the corresponding location in the adapter.
Of course, if you maintain a child data list outside of the adapter, you can also fetch data directly from this list.
But getChild() doesn’t it look clearer? The same is true for
getGroup(). When executing getGroupView() to populate the group view, you can easily obtain the corresponding data.

private List mChildItems;

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
    Log.d(TAG, "    getChildView: groupPosition" + groupPosition + ", childPosition " + childPosition);
    
    // 可以这样:
    HashMap<String, String> child = (HashMap<String, String>)getChild(groupPosition, childPosition);
    Log.d(TAG, "    child data from getChild():" + child.get("Sub Item"));
    
    // 也可以这样:
    Log.d(TAG, "    child data from my list:" + ((List)mChildItems.get(groupPosition)).get(childPosition));
    
    return view;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    Log.d(TAG, "    getChild: groupPosition" + groupPosition + ", childPosition " + childPosition);
    return super.getChild(groupPosition, childPosition);
}

Log of some method calls:

11-05 12:37:50.322: D/Exp(24030): getChildrenCount: groupPosition 1
11-05 12:37:50.325: D/Exp(24030):   getChildView: groupPosition1, childPosition 0
11-05 12:37:50.325: D/Exp(24030):     getChild: groupPosition1, childPosition 0
11-05 12:37:50.326: D/Exp(24030):     child data from getChild():Sub Item 0
11-05 12:37:50.326: D/Exp(24030):     child data from my list:{Sub Item=Sub Item 0}
11-05 12:37:50.329: D/Exp(24030):   getChildView: groupPosition1, childPosition 1
11-05 12:37:50.329: D/Exp(24030):     getChild: groupPosition1, childPosition 1
11-05 12:37:50.329: D/Exp(24030):     child data from getChild():Sub Item 1
11-05 12:37:50.329: D/Exp(24030):     child data from my list:{Sub Item=Sub Item 1}
11-05 12:37:50.331: D/Exp(24030):   getChildView: groupPosition1, childPosition 2
11-05 12:37:50.331: D/Exp(24030):     getChild: groupPosition1, childPosition 2
11-05 12:37:50.331: D/Exp(24030):     child data from getChild():Sub Item 2
11-05 12:37:50.331: D/Exp(24030):     child data from my list:{Sub Item=Sub Item 2}

Essentially, ExpandableListAdapter's getChild(), getGroup() and android.widget.Adapter's Object getItem(int position) are the same thing: "Get the data item associated with the specified position in the data set."

Reference:

  • A project found to answer this question: Expandable ListView in ANDROID

  • This answer is great stackoverflow - What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?

  • android.widget.ExpandableListAdapter source code link

  • stackoverflow - Android ExpandableListView - Looking for a tutorial

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template