FileDocCategorySizeDatePackage
ExpandableListScenario.javaAPI DocAndroid 1.5 API13245Wed May 06 22:42:02 BST 2009com.android.frameworktest.util

ExpandableListScenario

public abstract class ExpandableListScenario extends ListScenario
Utility base class for creating various Expandable List scenarios.

WARNING: A lot of the features are mixed between ListView's expected position (flat list position) and an ExpandableListView's expected position. You must add/change features as you need them.

see
ListScenario

Fields Summary
protected android.widget.ExpandableListAdapter
mAdapter
protected List
mGroups
Constructors Summary
Methods Summary
protected android.widget.ExpandableListAdaptercreateAdapter()

        return new MyAdapter();
    
protected android.widget.ListViewcreateListView()

        return new ExpandableListView(this);
    
protected ParamscreateParams()

        return new ExpandableParams();
    
protected android.view.ViewcreateView(long packedPosition, int flPos, android.view.ViewGroup parent, int desiredHeight)
Create a view for a group or child position.

param
packedPosition The packed position (has type, group pos, and optionally child pos).
param
flPos The flat list position (the position that the ListView goes by).
param
parent The parent view.
param
desiredHeight The desired height.
return
A view.

        TextView result = new TextView(parent.getContext());
        result.setHeight(desiredHeight);
        result.setText(getValueAtPosition(packedPosition));
        final ViewGroup.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        result.setLayoutParams(lp);
        result.setGravity(Gravity.CENTER_VERTICAL);
        result.setPadding(36, 0, 0, 0);
        result.setId(flPos);
        return result;
    
public intfindGroupWithNumChildren(int numChildren, boolean atLeastOneChild)
Returns a group index containing either the number of children or at least one child.

param
numChildren The group must have this amount, or -1 if using atLeastOneChild.
param
atLeastOneChild The group must have at least one child, or false if using numChildren.
return
A group index with the requirements.

        final ExpandableListAdapter adapter = mAdapter;
        
        for (int i = adapter.getGroupCount() - 1; i >= 0; i--) {
            final int curNumChildren = adapter.getChildrenCount(i);
            
            if (numChildren == curNumChildren || atLeastOneChild && curNumChildren > 0) {
                return i;
            }
        }
        
        return -1;
    
public android.widget.ExpandableListAdaptergetAdapter()

        return mAdapter;
    
public android.widget.ExpandableListViewgetExpandableListView()
Get the ExpandableListView widget.

return
The main widget.

        return (ExpandableListView) super.getListView();
    
public java.util.ListgetGroups()

        return mGroups;
    
public final java.lang.StringgetValueAtPosition(long packedPosition)
Gets a string for the value of some item.

param
packedPosition The position of the item.
return
The string.

        final int type = ExpandableListView.getPackedPositionType(packedPosition);
        
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            return mGroups.get(ExpandableListView.getPackedPositionGroup(packedPosition))
                    .children.get(ExpandableListView.getPackedPositionChild(packedPosition))
                    .name;
        } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            return mGroups.get(ExpandableListView.getPackedPositionGroup(packedPosition))
                    .name;
        } else {
            throw new IllegalStateException("packedPosition is not a valid position.");
        }
    
private android.view.ViewgetView(long packedPosition, android.view.View convertView, android.view.ViewGroup parent)
Gets a view for the packed position, possibly reusing the convertView.

param
packedPosition The position to get a view for.
param
convertView Optional view to convert.
param
parent The future parent.
return
A view.

        if (isOutOfBounds(packedPosition)) {
            throw new IllegalStateException("position out of range for adapter!");
        }
        
        final ExpandableListView elv = getExpandableListView();
        final int flPos = elv.getFlatListPosition(packedPosition); 
        
        if (convertView != null) {
            ((TextView) convertView).setText(getValueAtPosition(packedPosition));
            convertView.setId(flPos);
            return convertView;
        }

        int desiredHeight = getHeightForPosition(flPos);
        return createView(packedPosition, flPos, parent, desiredHeight);
    
protected final voidinit(Params params)

        init((ExpandableParams) params);
    
protected abstract voidinit(com.android.frameworktest.util.ExpandableListScenario$ExpandableParams params)

see
ListScenario#init

private booleanisOutOfBounds(long packedPosition)
Whether a particular position is out of bounds.

param
packedPosition The packed position.
return
Whether it's out of bounds.

        final int type = ExpandableListView.getPackedPositionType(packedPosition);
        
        if (type == ExpandableListView.PACKED_POSITION_TYPE_NULL) {
            throw new IllegalStateException("packedPosition is not a valid position.");
        }

        final int group = ExpandableListView.getPackedPositionGroup(packedPosition); 
        if (group >= mGroups.size() || group < 0) {
            return true;
        }
        
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            final int child = ExpandableListView.getPackedPositionChild(packedPosition); 
            if (child >= mGroups.get(group).children.size() || child < 0) {
                return true;
            }
        }
        
        return false;
    
protected voidreadAndValidateParams(Params params)

        ExpandableParams expandableParams = (ExpandableParams) params;
        
        int[] numChildren = expandableParams.mNumChildren;
        
        mGroups = new ArrayList<MyGroup>(numChildren.length);
        for (int i = 0; i < numChildren.length; i++) {
            mGroups.add(new MyGroup(numChildren[i]));
        }
        
        expandableParams.superSetNumItems();
        
        super.readAndValidateParams(params);
    
protected voidsetAdapter(android.widget.ListView listView)

        ((ExpandableListView) listView).setAdapter(mAdapter = createAdapter());