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.mupdf.view.ThumbnailViews.java

private void checkFocus() {
    final ListAdapter adapter = getAdapter();
    final boolean focusable = (adapter != null && adapter.getCount() > 0);

    // The order in which we set focusable in touch mode/focusable may
    // matter/*  w  w w .  j  av  a  2s .co m*/
    // for the client, see View.setFocusableInTouchMode() comments for more
    // details
    super.setFocusableInTouchMode(focusable && mDesiredFocusableInTouchModeState);
    super.setFocusable(focusable && mDesiredFocusableState);

    if (mEmptyView != null) {
        updateEmptyStatus();
    }
}

From source file:com.artifex.mupdf.view.ThumbnailViews.java

private void rememberSyncState() {
    if (getChildCount() == 0) {
        return;//from ww  w  . j  ava  2s . c  o  m
    }

    mNeedSync = true;

    if (mSelectedPosition >= 0) {
        View child = getChildAt(mSelectedPosition - mFirstPosition);

        mSyncRowId = mNextSelectedRowId;
        mSyncPosition = mNextSelectedPosition;

        if (child != null) {
            mSpecificStart = (mIsVertical ? child.getTop() : child.getLeft());
        }

        mSyncMode = SYNC_SELECTED_POSITION;
    } else {
        // Sync the based on the offset of the first view
        View child = getChildAt(0);
        ListAdapter adapter = getAdapter();

        if (mFirstPosition >= 0 && mFirstPosition < adapter.getCount()) {
            mSyncRowId = adapter.getItemId(mFirstPosition);
        } else {
            mSyncRowId = NO_ID;
        }

        mSyncPosition = mFirstPosition;

        if (child != null) {
            mSpecificStart = child.getTop();
        }

        mSyncMode = SYNC_FIRST_POSITION;
    }
}

From source file:com.aliasapps.seq.scroller.TwoWayView.java

/**
 * @param direction either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN} or
 *        {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT} depending on the
 *        current view orientation./*from  w  w w  .  ja v a2 s . c  o m*/
 *
 * @return The position of the next selectable position of the views that
 *         are currently visible, taking into account the fact that there might
 *         be no selection.  Returns {@link #INVALID_POSITION} if there is no
 *         selectable view on screen in the given direction.
 */
private int lookForSelectablePositionOnScreen(int direction) {
    forceValidFocusDirection(direction);

    final int firstPosition = mFirstPosition;
    final ListAdapter adapter = getAdapter();

    if (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT) {
        int startPos = (mSelectedPosition != INVALID_POSITION ? mSelectedPosition + 1 : firstPosition);

        if (startPos >= adapter.getCount()) {
            return INVALID_POSITION;
        }

        if (startPos < firstPosition) {
            startPos = firstPosition;
        }

        final int lastVisiblePos = getLastVisiblePosition();

        for (int pos = startPos; pos <= lastVisiblePos; pos++) {
            if (adapter.isEnabled(pos) && getChildAt(pos - firstPosition).getVisibility() == View.VISIBLE) {
                return pos;
            }
        }
    } else {
        final int last = firstPosition + getChildCount() - 1;

        int startPos = (mSelectedPosition != INVALID_POSITION) ? mSelectedPosition - 1
                : firstPosition + getChildCount() - 1;

        if (startPos < 0 || startPos >= adapter.getCount()) {
            return INVALID_POSITION;
        }

        if (startPos > last) {
            startPos = last;
        }

        for (int pos = startPos; pos >= firstPosition; pos--) {
            if (adapter.isEnabled(pos) && getChildAt(pos - firstPosition).getVisibility() == View.VISIBLE) {
                return pos;
            }
        }
    }

    return INVALID_POSITION;
}

From source file:com.aliasapps.seq.scroller.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 ww  w  .  j a  va 2s .  co 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 = (mIsVertical ? other.getTop() : other.getLeft());
            }
        }
    }

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

From source file:com.aliasapps.seq.scroller.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./*w  w  w.  j  a v a  2s  .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.getMeasuredHeight();

        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.aliasapps.seq.scroller.TwoWayView.java

/**
 * Measures the height of the given range of children (inclusive) and
 * returns the height with this TwoWayView's padding and item margin heights
 * included. If maxHeight is provided, the measuring will stop when the
 * current height reaches maxHeight./*from  ww  w  .ja v  a2 s . c  om*/
 *
 * @param widthMeasureSpec The width 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 maxHeight The maximum height 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
 *            height 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 height of this TwoWayView with the given children.
 */
private int measureHeightOfChildren(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();

    final ListAdapter adapter = mAdapter;
    if (adapter == null) {
        return paddingTop + paddingBottom;
    }

    // Include the padding of the list
    int returnedHeight = paddingTop + paddingBottom;
    final int itemMargin = mItemMargin;

    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevHeightWithoutPartialChild = 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, widthMeasureSpec);

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

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

        returnedHeight += child.getMeasuredHeight();

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

        if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) {
            prevHeightWithoutPartialChild = returnedHeight;
        }
    }

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

From source file:com.aliasapps.seq.scroller.TwoWayView.java

@Override
public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null && mDataSetObserver != null) {
        mAdapter.unregisterDataSetObserver(mDataSetObserver);
    }/*from   w ww  .  j a va  2  s .c  om*/

    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.compareTo(ChoiceMode.NONE) != 0 && 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();
}

From source file:com.artifex.mupdf.view.ThumbnailViews.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.//from   ww  w. j av 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.getMeasuredHeight();

        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.mupdf.view.ThumbnailViews.java

/**
 * Measures the height of the given range of children (inclusive) and
 * returns the height with this TwoWayView's padding and item margin heights
 * included. If maxHeight is provided, the measuring will stop when the
 * current height reaches maxHeight.// w  w w .  java 2s  .  co m
 * 
 * @param widthMeasureSpec
 *            The width 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 maxHeight
 *            The maximum height 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 height 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 height of this TwoWayView with the given children.
 */
private int measureHeightOfChildren(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();

    final ListAdapter adapter = mAdapter;
    if (adapter == null) {
        return paddingTop + paddingBottom;
    }

    // Include the padding of the list
    int returnedHeight = paddingTop + paddingBottom;
    final int itemMargin = mItemMargin;

    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevHeightWithoutPartialChild = 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, widthMeasureSpec);

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

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

        returnedHeight += child.getMeasuredHeight();

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

        if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) {
            prevHeightWithoutPartialChild = returnedHeight;
        }
    }

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

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

private void rememberSyncState() {
    if (getChildCount() == 0) {
        return;/*from w ww  .j  a  va2 s . c  o  m*/
    }

    mNeedSync = true;

    if (mSelectedPosition >= 0) {
        View child = getChildAt(mSelectedPosition - mFirstPosition);

        mSyncRowId = mNextSelectedRowId;
        mSyncPosition = mNextSelectedPosition;

        if (child != null) {
            mSpecificStart = getChildStartEdge(child);
        }

        mSyncMode = SYNC_SELECTED_POSITION;
    } else {
        // Sync the based on the offset of the first view
        View child = getChildAt(0);
        ListAdapter adapter = getAdapter();

        if (mFirstPosition >= 0 && mFirstPosition < adapter.getCount()) {
            mSyncRowId = adapter.getItemId(mFirstPosition);
        } else {
            mSyncRowId = NO_ID;
        }

        mSyncPosition = mFirstPosition;

        if (child != null) {
            mSpecificStart = getChildStartEdge(child);
        }

        mSyncMode = SYNC_FIRST_POSITION;
    }
}