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

GridScenario

public abstract class GridScenario extends android.app.Activity
Utility base class for creating various GridView scenarios. Configurable by the number of items, how tall each item should be (in relation to the screen height), and what item should start with selection.

Fields Summary
private android.widget.GridView
mGridView
private int
mNumItems
private int
mStartingSelectionPosition
private double
mItemScreenSizeFactor
private Map
mOverrideItemScreenSizeFactors
private int
mScreenHeight
private boolean
mStackFromBottom
private int
mColumnWidth
private int
mNumColumns
private int
mStretchMode
private int
mVerticalSpacing
Constructors Summary
Methods Summary
protected android.widget.ListAdaptercreateAdapter()
Override this to provide an different adapter for your scenario

return
The adapter that this scenario will use

        return new MyAdapter();
    
protected android.view.ViewcreateView(int position, android.view.ViewGroup parent, int desiredHeight)
Create a view for a grid item. Override this to create a custom view beyond the simple focusable / unfocusable text view.

param
position The position.
param
parent The parent
param
desiredHeight The height the view should be to respect the desired item to screen height ratio.
return
a view for the grid.

        TextView result = new TextView(parent.getContext());
        result.setHeight(desiredHeight);
        result.setText(getValueAtPosition(position));
        final ViewGroup.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        result.setLayoutParams(lp);
        result.setId(position);
        result.setBackgroundColor(0x55ffffff);
        return result;
    
public intgetDesiredItemHeight()

return
The desired height of 1 item, ignoring overrides

        return (int) (mScreenHeight * mItemScreenSizeFactor);
    
public android.widget.GridViewgetGridView()


       
        return mGridView;
    
protected intgetInitialNumItems()

return
The initial number of items in the grid as specified by the scenario. This number may change over time.

        return mNumItems;
    
protected intgetScreenHeight()

        return mScreenHeight;
    
public final java.lang.StringgetValueAtPosition(int position)

        return "postion " + position;
    
protected abstract voidinit(com.android.frameworktest.util.GridScenario$Params params)
How each scenario customizes its behavior.

param
params

protected voidnothingSelected()
Override this if you want to know that nothing is selected.


    
protected voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);

        // turn off title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        mScreenHeight = getWindowManager().getDefaultDisplay().getHeight();

        final Params params = new Params();
        init(params);

        readAndValidateParams(params);

        mGridView = new GridView(this);
        mGridView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT));
        mGridView.setDrawSelectorOnTop(false);
        if (mNumColumns >= GridView.AUTO_FIT) {
            mGridView.setNumColumns(mNumColumns);
        }
        if (mColumnWidth > 0) {
            mGridView.setColumnWidth(mColumnWidth);
        }
        if (mVerticalSpacing > 0) {
            mGridView.setVerticalSpacing(mVerticalSpacing);
        }
        mGridView.setStretchMode(mStretchMode);
        mGridView.setAdapter(createAdapter());
        if (mStartingSelectionPosition >= 0) {
            mGridView.setSelection(mStartingSelectionPosition);
        }
        mGridView.setPadding(10, 10, 10, 10);
        mGridView.setStackFromBottom(mStackFromBottom);

        mGridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView parent, View v, int position, long id) {
                positionSelected(position);
            }

            public void onNothingSelected(AdapterView parent) {
                nothingSelected();
            }
        });

        setContentView(mGridView);
    
protected voidpositionSelected(int positon)
Override this if you want to know when something has been selected (perhaps more importantly, that {@link android.widget.AdapterView.OnItemSelectedListener} has been triggered).


    
private voidreadAndValidateParams(com.android.frameworktest.util.GridScenario$Params params)
Read in and validate all of the params passed in by the scenario.

param
params

        if (params.mMustFillScreen ) {
            double totalFactor = 0.0;
            for (int i = 0; i < params.mNumItems; i++) {
                if (params.mOverrideItemScreenSizeFactors.containsKey(i)) {
                    totalFactor += params.mOverrideItemScreenSizeFactors.get(i);
                } else {
                    totalFactor += params.mItemScreenSizeFactor;
                }
            }
            if (totalFactor < 1.0) {
                throw new IllegalArgumentException("grid items must combine to be at least " +
                        "the height of the screen.  this is not the case with " + params.mNumItems
                        + " items and " + params.mItemScreenSizeFactor + " screen factor and " +
                        "screen height of " + mScreenHeight);
            }
        }

        mNumItems = params.mNumItems;
        mStartingSelectionPosition = params.mStartingSelectionPosition;
        mItemScreenSizeFactor = params.mItemScreenSizeFactor;

        mOverrideItemScreenSizeFactors.putAll(params.mOverrideItemScreenSizeFactors);

        mStackFromBottom = params.mStackFromBottom;
        mColumnWidth = params.mColumnWidth;
        mNumColumns = params.mNumColumns;
        mStretchMode = params.mStretchMode;
        mVerticalSpacing = params.mVerticalSpacing;