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.appunite.list.ListView.java

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

    final ListAdapter adapter = mAdapter;
    int closetChildIndex = -1;
    int closestChildTop = 0;
    if (adapter != null && gainFocus && previouslyFocusedRect != null) {
        final int scrollX = getScrollX();
        final int scrollY = getScrollY();
        previouslyFocusedRect.offset(scrollX, scrollY);

        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();//from   www .  j  a v  a2  s.c  o  m
        }

        // 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;
                closestChildTop = other.getTop();
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelectionFromTop(closetChildIndex + mFirstPosition, closestChildTop);
    } else {
        requestLayout();
    }
}

From source file:org.bangbang.support.v4.widget.HListView.java

/**
     * Measures the height of the given range of children (inclusive) and
     * returns the height with this ListView's padding and divider heights
     * included. If maxHeight is provided, the measuring will stop when the
     * current height reaches maxHeight.
     */*  w w w  .j  a v  a 2 s  . 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
     *            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 width of this ListView with the given children.
     */
    final int measureWidthOfChildren(int heightMeasureSpec, int startPosition, int endPosition, final int maxWidth,
            int disallowPartialChildPosition) {

        final ListAdapter adapter = mAdapter;
        if (adapter == null) {
            return mListPadding.left + mListPadding.right;
        }

        // Include the padding of the list
        int returnedWidth = mListPadding.left + mListPadding.right;
        final int dividerWidth = ((mDividerWidth > 0) && mDivider != null) ? mDividerWidth : 0;
        // 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 HAbsListView.RecycleBin recycleBin = mRecycler;
        final boolean recyle = recycleOnMeasure();

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

            measureScrapChild(child, i, heightMeasureSpec);

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

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

            returnedWidth += child.getMeasuredWidth();

            if (returnedWidth >= maxWidth) {
                // 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
                        && (prevWidthWithoutPartialChild > 0) // We have a prev height
                        && (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 returnedHeight
        return returnedWidth;
    }

From source file:com.appunite.list.HorizontalListView.java

/**
 * Measures the width of the given range of children (inclusive) and
 * returns the width with this ListView's padding and divider heights
 * included. If maxWidth is provided, the measuring will stop when the
 * current width reaches maxWidth.//from   ww  w.jav a2  s  .  c  om
 *
 * @param heightMeasureSpec The width measure spec to be given to a child's
 *            {@link android.view.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 ListView with the given children.
 */
final int measureWidthOfChildren(int heightMeasureSpec, int startPosition, int endPosition, final int maxWidth,
        int disallowPartialChildPosition) {

    final ListAdapter adapter = mAdapter;
    if (adapter == null) {
        return mListPadding.left + mListPadding.right;
    }

    // Include the padding of the list
    int returnedWidth = mListPadding.left + mListPadding.right;
    final int dividerWidth = ((mDividerWidth > 0) && mDivider != null) ? mDividerWidth : 0;
    // The previous width value that was less than maxWidth 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 recyle = recycleOnMeasure();
    final boolean[] isScrap = mIsScrap;

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

        measureScrapChild(child, i, heightMeasureSpec);

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

        // Recycle the view before we possibly return from the method
        if (recyle && recycleBin.shouldRecycleViewType(((LayoutParams) child.getLayoutParams()).viewType)) {
            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.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

/**
 * Measures the height of the given range of children (inclusive) and returns the height with this ListView's padding and divider
 * heights included. If maxHeight is provided, the measuring will stop when the current height reaches maxHeight.
 * //from  www . j  av  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 com.awrtechnologies.carbudgetsales.adapter.
 * @param maxWidth
 *           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 ListView with the given children.
 */
final int measureWidthOfChildren(int heightMeasureSpec, int startPosition, int endPosition, final int maxWidth,
        int disallowPartialChildPosition) {
    if (LOG_ENABLED) {
        Log.i(LOG_TAG, "measureWidthOfChildren, from " + startPosition + " to " + endPosition);
    }

    final ListAdapter adapter = mAdapter;
    if (adapter == null) {
        return mListPadding.left + mListPadding.right;
    }

    // Include the padding of the list
    int returnedWidth = mListPadding.left + mListPadding.right;
    final int dividerWidth = ((mDividerWidth > 0) && mDivider != null) ? mDividerWidth : 0;
    // 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 AbsHListView.RecycleBin recycleBin = mRecycler;
    final boolean recyle = recycleOnMeasure();
    final boolean[] isScrap = mIsScrap;

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

        measureScrapChildWidth(child, i, heightMeasureSpec);

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

        // Recycle the view before we possibly return from the method
        if (recyle && recycleBin.shouldRecycleViewType(((LayoutParams) child.getLayoutParams()).viewType)) {
            recycleBin.addScrapView(child, -1);
        }

        returnedWidth += child.getMeasuredWidth();

        if (returnedWidth >= maxWidth) {
            // 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
                    && (prevWidthWithoutPartialChild > 0) // We have a prev height
                    && (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 returnedHeight
    return returnedWidth;
}

From source file:com.appunite.list.ListView.java

/**
 * Measures the width of the given range of children (inclusive) and
 * returns the width with this ListView's padding and divider heights
 * included. If maxHeight is provided, the measuring will stop when the
 * current width reaches maxHeight.//from   www. 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 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 ListView with the given children.
 */
final int measureHeightOfChildren(int widthMeasureSpec, int startPosition, int endPosition, final int maxHeight,
        int disallowPartialChildPosition) {

    final ListAdapter adapter = mAdapter;
    if (adapter == null) {
        return mListPadding.top + mListPadding.bottom;
    }

    // Include the padding of the list
    int returnedHeight = mListPadding.top + mListPadding.bottom;
    final int dividerHeight = ((mDividerHeight > 0) && mDivider != null) ? mDividerHeight : 0;
    // The previous width 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 AbsListView.RecycleBin recycleBin = mRecycler;
    final boolean recyle = recycleOnMeasure();
    final boolean[] isScrap = mIsScrap;

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

        measureScrapChild(child, i, widthMeasureSpec);

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

        // Recycle the view before we possibly return from the method
        if (recyle && recycleBin.shouldRecycleViewType(((LayoutParams) child.getLayoutParams()).viewType)) {
            recycleBin.addScrapView(child, -1);
        }

        returnedHeight += child.getMeasuredHeight();

        if (returnedHeight >= maxHeight) {
            // We went over, figure out which width 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 width
                    && (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.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

final int[] measureWithLargeChildren(int heightMeasureSpec, int startPosition, int endPosition,
        final int maxWidth, final int maxHeight, int disallowPartialChildPosition) {
    if (LOG_ENABLED) {
        Log.i(LOG_TAG, "measureWithLargeChildren, from " + startPosition + " to " + endPosition);
    }/*from   w w  w.j  av a  2s.  c o m*/

    final ListAdapter adapter = mAdapter;
    if (adapter == null) {
        return new int[] { mListPadding.left + mListPadding.right, mListPadding.top + mListPadding.bottom };
    }

    // Include the padding of the list
    int returnedWidth = mListPadding.left + mListPadding.right;
    int returnedHeight = mListPadding.top + mListPadding.bottom;

    final int dividerWidth = ((mDividerWidth > 0) && mDivider != null) ? mDividerWidth : 0;

    int childWidth = 0;
    int childHeight = 0;

    int i;
    View child;

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

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

        measureScrapChildWidth(child, i, heightMeasureSpec);

        // Recycle the view before we possibly return from the method
        if (recyle && recycleBin.shouldRecycleViewType(((LayoutParams) child.getLayoutParams()).viewType)) {
            recycleBin.addScrapView(child, -1);
        }

        childWidth = Math.max(childWidth, child.getMeasuredWidth() + dividerWidth);
        childHeight = Math.max(childHeight, child.getMeasuredHeight());
    }

    returnedWidth += childWidth;
    returnedHeight += childHeight;

    return new int[] { Math.min(returnedWidth, maxWidth), Math.min(returnedHeight, maxHeight) };
}

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

@Override
public void setFocusable(boolean focusable) {
    final ListAdapter adapter = getAdapter();
    final boolean empty = (adapter == null || adapter.getCount() == 0);

    mDesiredFocusableState = focusable;//from   w  w  w  .ja va 2s  .  com
    if (!focusable) {
        mDesiredFocusableInTouchModeState = false;
    }

    super.setFocusable(focusable && !empty);
}

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

@Override
public void setFocusableInTouchMode(boolean focusable) {
    final ListAdapter adapter = getAdapter();
    final boolean empty = (adapter == null || adapter.getCount() == 0);

    mDesiredFocusableInTouchModeState = focusable;
    if (focusable) {
        mDesiredFocusableState = true;/*from   www.j  a  v a 2 s.  c  o  m*/
    }

    super.setFocusableInTouchMode(focusable && !empty);
}

From source file:com.aliasapps.seq.scroller.TwoWayView.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
    // for the client, see View.setFocusableInTouchMode() comments for more
    // details/*from   w w w  . j  a  v  a  2  s. co  m*/
    super.setFocusableInTouchMode(focusable && mDesiredFocusableInTouchModeState);
    super.setFocusable(focusable && mDesiredFocusableState);

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

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

private void rememberSyncState() {
    if (getChildCount() == 0) {
        return;//from   w w w  .  jav a  2s  .  co  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 = (mIsVertical ? child.getTop() : child.getLeft());
        }

        mSyncMode = SYNC_FIRST_POSITION;
    }
}