Example usage for android.widget ListAdapter getCount

List of usage examples for android.widget ListAdapter getCount

Introduction

In this page you can find the example usage for android.widget ListAdapter getCount.

Prototype

int getCount();

Source Link

Document

How many items are in the data set represented by this Adapter.

Usage

From source file:com.artifex.mupdflib.TwoWayView.java

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    if (gainFocus && mSelectedPosition < 0 && !isInTouchMode()) {
        if (!mIsAttached && mAdapter != null) {
            // Data may have changed while we were detached and it's valid
            // to change focus while detached. Refresh so we don't die.
            mDataChanged = true;//from w  ww  .j  a  v a2s  .  c  o m
            mOldItemCount = mItemCount;
            mItemCount = mAdapter.getCount();
        }

        resurrectSelection();
    }

    final ListAdapter adapter = mAdapter;
    int closetChildIndex = INVALID_POSITION;
    int closestChildStart = 0;

    if (adapter != null && gainFocus && previouslyFocusedRect != null) {
        previouslyFocusedRect.offset(getScrollX(), getScrollY());

        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();
        }

        // Figure out which item should be selected based on previously
        // focused rect.
        Rect otherRect = mTempRect;
        int minDistance = Integer.MAX_VALUE;
        final int childCount = getChildCount();
        final int firstPosition = mFirstPosition;

        for (int i = 0; i < childCount; i++) {
            // Only consider selectable views
            if (!adapter.isEnabled(firstPosition + i)) {
                continue;
            }

            View other = getChildAt(i);
            other.getDrawingRect(otherRect);
            offsetDescendantRectToMyCoords(other, otherRect);
            int distance = getDistance(previouslyFocusedRect, otherRect, direction);

            if (distance < minDistance) {
                minDistance = distance;
                closetChildIndex = i;
                closestChildStart = getChildStartEdge(other);
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelectionFromOffset(closetChildIndex + mFirstPosition, closestChildStart);
    } else {
        requestLayout();
    }
}

From source file:com.artifex.mupdflib.TwoWayView.java

/**
 * Measures the width of the given range of children (inclusive) and
 * returns the width with this TwoWayView's padding and item margin widths
 * included. If maxWidth is provided, the measuring will stop when the
 * current width reaches maxWidth./*ww  w  . j a  v a2s.  c  o m*/
 *
 * @param heightMeasureSpec The height measure spec to be given to a child's
 *            {@link View#measure(int, int)}.
 * @param startPosition The position of the first child to be shown.
 * @param endPosition The (inclusive) position of the last child to be
 *            shown. Specify {@link #NO_POSITION} if the last child should be
 *            the last available child from the adapter.
 * @param maxWidth The maximum width that will be returned (if all the
 *            children don't fit in this value, this value will be
 *            returned).
 * @param disallowPartialChildPosition In general, whether the returned
 *            width should only contain entire children. This is more
 *            powerful--it is the first inclusive position at which partial
 *            children will not be allowed. Example: it looks nice to have
 *            at least 3 completely visible children, and in portrait this
 *            will most likely fit; but in landscape there could be times
 *            when even 2 children can not be completely shown, so a value
 *            of 2 (remember, inclusive) would be good (assuming
 *            startPosition is 0).
 * @return The width of this TwoWayView with the given children.
 */
private int measureWidthOfChildren(int heightMeasureSpec, int startPosition, int endPosition,
        final int maxWidth, int disallowPartialChildPosition) {

    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();

    final ListAdapter adapter = mAdapter;
    if (adapter == null) {
        return paddingLeft + paddingRight;
    }

    // Include the padding of the list
    int returnedWidth = paddingLeft + paddingRight;
    final int itemMargin = mItemMargin;

    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevWidthWithoutPartialChild = 0;
    int i;
    View child;

    // mItemCount - 1 since endPosition parameter is inclusive
    endPosition = (endPosition == NO_POSITION) ? adapter.getCount() - 1 : endPosition;
    final RecycleBin recycleBin = mRecycler;
    final boolean shouldRecycle = recycleOnMeasure();
    final boolean[] isScrap = mIsScrap;

    for (i = startPosition; i <= endPosition; ++i) {
        child = obtainView(i, isScrap);

        measureScrapChild(child, i, heightMeasureSpec);

        if (i > 0) {
            // Count the item margin for all but one child
            returnedWidth += itemMargin;
        }

        // Recycle the view before we possibly return from the method
        if (shouldRecycle) {
            recycleBin.addScrapView(child, -1);
        }

        returnedWidth += child.getMeasuredWidth();

        if (returnedWidth >= maxWidth) {
            // We went over, figure out which width to return.  If returnedWidth > maxWidth,
            // then the i'th position did not fit completely.
            return (disallowPartialChildPosition >= 0) // Disallowing is enabled (> -1)
                    && (i > disallowPartialChildPosition) // We've past the min pos
                    && (prevWidthWithoutPartialChild > 0) // We have a prev width
                    && (returnedWidth != maxWidth) // i'th child did not fit completely
                            ? prevWidthWithoutPartialChild
                            : maxWidth;
        }

        if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) {
            prevWidthWithoutPartialChild = returnedWidth;
        }
    }

    // At this point, we went through the range of children, and they each
    // completely fit, so return the returnedWidth
    return returnedWidth;
}

From source file:com.artifex.mupdflib.TwoWayView.java

@Override
public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null && mDataSetObserver != null) {
        mAdapter.unregisterDataSetObserver(mDataSetObserver);
    }//from  www  .  ja  va2  s  .c o m

    resetState();
    mRecycler.clear();

    mAdapter = adapter;
    mDataChanged = true;

    mOldSelectedPosition = INVALID_POSITION;
    mOldSelectedRowId = INVALID_ROW_ID;

    if (mCheckStates != null) {
        mCheckStates.clear();
    }

    if (mCheckedIdStates != null) {
        mCheckedIdStates.clear();
    }

    if (mAdapter != null) {
        mOldItemCount = mItemCount;
        mItemCount = adapter.getCount();

        mDataSetObserver = new AdapterDataSetObserver();
        mAdapter.registerDataSetObserver(mDataSetObserver);

        mRecycler.setViewTypeCount(adapter.getViewTypeCount());

        mHasStableIds = adapter.hasStableIds();
        mAreAllItemsSelectable = adapter.areAllItemsEnabled();

        if (mChoiceMode != ChoiceMode.NONE && mHasStableIds && mCheckedIdStates == null) {
            mCheckedIdStates = new LongSparseArray<Integer>();
        }

        final int position = lookForSelectablePosition(0);
        setSelectedPositionInt(position);
        setNextSelectedPositionInt(position);

        if (mItemCount == 0) {
            checkSelectionChanged();
        }
    } else {
        mItemCount = 0;
        mHasStableIds = false;
        mAreAllItemsSelectable = true;

        checkSelectionChanged();
    }

    checkFocus();
    requestLayout();
}