FileDocCategorySizeDatePackage
ListScenario.javaAPI DocAndroid 5.1 API21855Thu Mar 12 22:22:12 GMT 2015android.util

ListScenario

public abstract class ListScenario extends android.app.Activity
Utility base class for creating various List 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.ListView
mListView
private android.widget.TextView
mHeaderTextView
private int
mNumItems
protected boolean
mItemsFocusable
private int
mStartingSelectionPosition
private double
mItemScreenSizeFactor
private Map
mOverrideItemScreenSizeFactors
private int
mScreenHeight
private boolean
mIncludeHeader
private Set
mUnselectableItems
private boolean
mStackFromBottom
private int
mClickedPosition
private int
mLongClickedPosition
private int
mConvertMisses
private int
mHeaderViewCount
private boolean
mHeadersFocusable
private int
mFooterViewCount
private android.widget.LinearLayout
mLinearLayout
Constructors Summary
Methods Summary
public android.view.ViewconvertView(int position, android.view.View convertView, android.view.ViewGroup parent)
Convert a non-null view.

        return ListItemFactory.convertText(convertView, getValueAtPosition(position), position);
    
protected android.widget.ListViewcreateListView()

return
The newly created ListView widget.

        return new ListView(this);
    
protected android.util.ListScenario$ParamscreateParams()

return
The newly created Params object.

        return new Params();
    
protected android.view.ViewcreateView(int position, android.view.ViewGroup parent, int desiredHeight)
Create a view for a list 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 list.

        return ListItemFactory.text(position, parent.getContext(), getValueAtPosition(position),
                desiredHeight);
    
public voidenableLongPress()
Attaches a long press listener. You can find out which views were clicked by calling {@link #getLongClickedPosition()}.

        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView parent, View v, int position, long id) {
                positionLongClicked(position);
                return true;
            }
        });
    
public intgetClickedPosition()

        return mClickedPosition;
    
public intgetConvertMisses()

return
The number of times convertView failed

        return mConvertMisses;
    
public final java.lang.StringgetHeaderValue()

return
The contents of the header above the list.
throws
IllegalArgumentException if there is no header.

        if (!mIncludeHeader) {
            throw new IllegalArgumentException("no header above list");
        }
        return mHeaderTextView.getText().toString();
    
public intgetHeightForPosition(int position)

return
The height that will be set for a particular position.

        int desiredHeight = (int) (mScreenHeight * mItemScreenSizeFactor);
        if (mOverrideItemScreenSizeFactors.containsKey(position)) {
            desiredHeight = (int) (mScreenHeight * mOverrideItemScreenSizeFactors.get(position));
        }
        return desiredHeight;
    
public intgetItemViewType(int position)
Return an item type for the specified position in the adapter. Override if your adapter creates more than one type.

        return 0;
    
public android.widget.ListViewgetListView()


       
        return mListView;
    
protected android.widget.LinearLayoutgetListViewContainer()
Returns the LinearLayout containing the ListView in this scenario.

return
The LinearLayout in which the ListView is held.

        return mLinearLayout;
    
public intgetLongClickedPosition()

        return mLongClickedPosition;
    
protected intgetScreenHeight()

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

        return isItemAtPositionSelectable(position)
                ?
                "position " + position:
                "------- " + position;
    
public intgetViewTypeCount()
Return the number of types created by the adapter. Override if your adapter creates more than one type.

        return 1;
    
protected abstract voidinit(android.util.ListScenario$Params params)
How each scenario customizes its behavior.

param
params

private booleanisItemAtPositionSelectable(int position)
Return whether the item at position is selectable (i.e is a separator). (external users can access this info using the adapter)

        return !mUnselectableItems.contains(position);
    
protected voidnothingSelected()
Override this if you want to know that nothing is selected.

    
protected voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);

        // for test stability, turn off title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        

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

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

        readAndValidateParams(params);


        mListView = createListView();
        mListView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mListView.setDrawSelectorOnTop(false);

        for (int i=0; i<mHeaderViewCount; i++) {
            TextView header = mHeadersFocusable ?
                    new EditText(this) :
                    new TextView(this);
            header.setText("Header: " + i);
            mListView.addHeaderView(header);
        }
        
        for (int i=0; i<mFooterViewCount; i++) {
            TextView header = new TextView(this);
            header.setText("Footer: " + i);
            mListView.addFooterView(header);
        }

        if (params.mConnectAdapter) {
            setAdapter(mListView);
        }
        
        mListView.setItemsCanFocus(mItemsFocusable);
        if (mStartingSelectionPosition >= 0) {
            mListView.setSelection(mStartingSelectionPosition);
        }
        mListView.setPadding(0, 0, 0, 0);
        mListView.setStackFromBottom(mStackFromBottom);
        mListView.setDivider(null);

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

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

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                positionClicked(position);
            }
        });
        
        // set the fading edge length porportionally to the screen
        // height for test stability
        if (params.mFadingEdgeScreenSizeFactor != null) {
            mListView.setFadingEdgeLength((int) (params.mFadingEdgeScreenSizeFactor * mScreenHeight));            
        } else {
            mListView.setFadingEdgeLength((int) ((64.0 / 480) * mScreenHeight));
        }

        if (mIncludeHeader) {
            mLinearLayout = new LinearLayout(this);

            mHeaderTextView = new TextView(this);
            mHeaderTextView.setText("hi");
            mHeaderTextView.setLayoutParams(new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            mLinearLayout.addView(mHeaderTextView);

            mLinearLayout.setOrientation(LinearLayout.VERTICAL);
            mLinearLayout.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            mListView.setLayoutParams((new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    0,
                    1f)));

            mLinearLayout.addView(mListView);
            setContentView(mLinearLayout);
        } else {
            mLinearLayout = new LinearLayout(this);
            mLinearLayout.setOrientation(LinearLayout.VERTICAL);
            mLinearLayout.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            mListView.setLayoutParams((new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    0,
                    1f)));
            mLinearLayout.addView(mListView);
            setContentView(mLinearLayout);
        }
    
protected voidpositionClicked(int position)
Override this if you want to know when something has been clicked (perhaps more importantly, that {@link android.widget.AdapterView.OnItemClickListener} has been triggered).

        setClickedPosition(position);
    
protected voidpositionLongClicked(int position)
Override this if you want to know when something has been long clicked (perhaps more importantly, that {@link android.widget.AdapterView.OnItemLongClickListener} has been triggered).

        setLongClickedPosition(position);
    
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).

    
protected voidreadAndValidateParams(android.util.ListScenario$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("list 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;
        mItemsFocusable = params.mItemsFocusable;
        mStartingSelectionPosition = params.mStartingSelectionPosition;
        mItemScreenSizeFactor = params.mItemScreenSizeFactor;

        mOverrideItemScreenSizeFactors.putAll(params.mOverrideItemScreenSizeFactors);

        mUnselectableItems.addAll(params.mUnselectableItems);
        mIncludeHeader = params.mIncludeHeader;
        mStackFromBottom = params.mStackFromBottom;
        mHeaderViewCount = params.mHeaderViewCount;
        mHeadersFocusable = params.mHeaderFocusable;
        mFooterViewCount = params.mFooterViewCount;
    
public voidrequestRectangleOnScreen(int childIndex, android.graphics.Rect rect)
Have a child of the list view call {@link View#requestRectangleOnScreen(android.graphics.Rect)}.

param
childIndex The index into the viewgroup children (i.e the children that are currently visible).
param
rect The rectangle, in the child's coordinates.

        final View child = getListView().getChildAt(childIndex);

        child.post(new Runnable() {
            public void run() {
                child.requestRectangleOnScreen(rect);
            }
        });
    
protected voidsetAdapter(android.widget.ListView listView)
Sets an adapter on a ListView.

param
listView The ListView to set the adapter on.

        listView.setAdapter(new MyAdapter());
    
public voidsetClickedPosition(int clickedPosition)

        mClickedPosition = clickedPosition;
    
protected final voidsetHeaderValue(java.lang.String value)

param
value What to put in the header text view
throws
IllegalArgumentException if there is no header.

        if (!mIncludeHeader) {
            throw new IllegalArgumentException("no header above list");
        }
        mHeaderTextView.setText(value);        
    
public voidsetLongClickedPosition(int longClickedPosition)

        mLongClickedPosition = longClickedPosition;