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.huewu.pla.lib.internal.PLAListView.java

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

    if (DEBUG) {/*w w w . ja  v  a  2  s  .  c  o m*/
        Log.v("PLA_ListView", "onFocusChanged");
    }

    int closetChildIndex = -1;
    if (gainFocus && previouslyFocusedRect != null) {
        // previouslyFocusedRect.offset(mScrollX, mScrollY);
        previouslyFocusedRect.offset(getScrollX(), getScrollY());

        final ListAdapter adapter = mAdapter;
        // 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
        final 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;
            }

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

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

    if (closetChildIndex >= 0) {
        setSelection(closetChildIndex + mFirstPosition);
    } else {
        requestLayout();
    }
}

From source file:com.huewu.pla.lib.internal.PLA_ListView.java

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    boolean populated = super.dispatchPopulateAccessibilityEvent(event);

    // If the item count is less than 15 then subtract disabled items from
    // the count and
    // position. Otherwise ignore disabled items.
    if (!populated) {
        int itemCount = 0;
        int currentItemIndex = getSelectedItemPosition();

        ListAdapter adapter = getAdapter();
        if (adapter != null) {
            final int count = adapter.getCount();
            if (count < 15) {
                for (int i = 0; i < count; i++) {
                    if (adapter.isEnabled(i)) {
                        itemCount++;//from   w  w w .j  a v a 2s  .  c  o  m
                    } else if (i <= currentItemIndex) {
                        currentItemIndex--;
                    }
                }
            } else {
                itemCount = count;
            }
        }

        event.setItemCount(itemCount);
        event.setCurrentItemIndex(currentItemIndex);
    }

    return populated;
}

From source file:caesar.feng.framework.widget.StaggeredGrid.ExtendableListView.java

/**
 * Remember enough information to restore the screen state when the data has
 * changed./*from   www  .  j  a v  a  2  s .  c om*/
 */
void rememberSyncState() {
    if (getChildCount() > 0) {
        mNeedSync = true;
        mSyncHeight = getHeight();
        // Sync the based on the offset of the first view
        View v = getChildAt(0);
        ListAdapter adapter = getAdapter();
        if (mFirstPosition >= 0 && mFirstPosition < adapter.getCount()) {
            mSyncRowId = adapter.getItemId(mFirstPosition);
        } else {
            mSyncRowId = NO_ID;
        }
        if (v != null) {
            mSpecificTop = v.getTop();
        }
        mSyncPosition = mFirstPosition;
    }
}

From source file:com.huewu.pla.lib.internal.PLA_ListView.java

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

    if (DEBUG)//  w w  w .j  ava2s  .  co  m
        Log.v("PLA_ListView", "onFocusChanged");

    int closetChildIndex = -1;
    if (gainFocus && previouslyFocusedRect != null) {
        // previouslyFocusedRect.offset(mScrollX, mScrollY);
        previouslyFocusedRect.offset(getScrollX(), getScrollY());

        final ListAdapter adapter = mAdapter;
        // 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;
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelection(closetChildIndex + mFirstPosition);
    } else {
        requestLayout();
    }
}

From source file:cn.iterlog.myapplication.widget.overscroll.StaggeredGridView.java

public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null) {
        mAdapter.unregisterDataSetObserver(mObserver);
    }//from ww  w .  ja v  a2  s .com
    // TODO: If the new adapter says that there are stable IDs, remove certain layout records
    // and onscreen views if they have changed instead of removing all of the state here.
    clearAllState();
    mAdapter = adapter;
    mDataChanged = true;

    mItemCount = adapter != null ? adapter.getCount() : 0;

    if (adapter != null) {
        adapter.registerDataSetObserver(mObserver);
        mRecycler.setViewTypeCount(adapter.getViewTypeCount());
        mHasStableIds = adapter.hasStableIds();
    } else {
        mHasStableIds = false;
    }
    populate(adapter != null);
}

From source file:com.ad.view.staggeredgridview.StaggeredGridView.java

public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null) {
        mAdapter.unregisterDataSetObserver(mObserver);
    }//from www . j av  a  2s .  c o m
    // TODO: If the new adapter says that there are stable IDs, remove
    // certain layout records
    // and onscreen views if they have changed instead of removing all of
    // the state here.
    clearAllState();
    mAdapter = adapter;
    mDataChanged = true;

    mItemCount = adapter != null ? adapter.getCount() : 0;

    if (adapter != null) {
        adapter.registerDataSetObserver(mObserver);
        mRecycler.setViewTypeCount(adapter.getViewTypeCount());
        mHasStableIds = adapter.hasStableIds();
    } else {
        mHasStableIds = false;
    }
    populate(adapter != null);
}

From source file:com.cbs.corona.lib.ExtendableListView.java

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

    // use a wrapper list adapter if we have a header or footer
    if (mHeaderViewInfos.size() > 0 || mFooterViewInfos.size() > 0) {
        mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, adapter);
    } else {
        mAdapter = adapter;
    }

    mDataChanged = true;
    mItemCount = adapter != null ? adapter.getCount() : 0;

    if (adapter != null) {
        adapter.registerDataSetObserver(mObserver);
        mRecycleBin.setViewTypeCount(adapter.getViewTypeCount());
    }

    requestLayout();
}

From source file:com.app.afteryou.ui.staggered.StaggeredGridView.java

public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null) {
        mAdapter.unregisterDataSetObserver(mDataSetObserver);
    }//from   w w  w  .ja va 2  s. co m
    // TODO: If the new adapter says that there are stable IDs, remove
    // certain layout records
    // and onscreen views if they have changed instead of removing all of
    // the state here.
    clearAllState();
    mAdapter = adapter;
    mDataChanged = true;
    mOldItemCount = mItemCount = adapter != null ? adapter.getCount() : 0;
    if (adapter != null) {
        adapter.registerDataSetObserver(mDataSetObserver);
        mRecycler.setViewTypeCount(adapter.getViewTypeCount());
        mHasStableIds = adapter.hasStableIds();
    } else {
        mHasStableIds = false;
    }
    populate(adapter != null);
}

From source file:com.nttec.everychan.ui.NewTabFragment.java

private void openLocal() {
    if (!CompatibilityUtils.hasAccessStorage(activity))
        return;/*  www .ja  va2  s.co  m*/
    final ListAdapter savedThreadsAdapter = new ArrayAdapter<Object>(activity, 0) {
        private static final int HEAD_ITEM = 0;
        private static final int NORMAL_ITEM = 1;

        private LayoutInflater inflater = LayoutInflater.from(activity);
        private int drawablePadding = (int) (resources.getDisplayMetrics().density * 5 + 0.5f);

        {
            add(new Object());
            for (Database.SavedThreadEntry entity : MainApplication.getInstance().database.getSavedThreads()) {
                File file = new File(entity.filepath);
                if (file.exists())
                    add(entity);
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            if (position == 0) {
                v = convertView == null ? inflater.inflate(android.R.layout.simple_list_item_1, parent, false)
                        : convertView;
                TextView tv = (TextView) v.findViewById(android.R.id.text1);
                tv.setText(R.string.newtab_select_local_file);
            } else {
                Database.SavedThreadEntry item = (Database.SavedThreadEntry) getItem(position);
                v = convertView == null ? inflater.inflate(android.R.layout.simple_list_item_2, parent, false)
                        : convertView;
                TextView t1 = (TextView) v.findViewById(android.R.id.text1);
                TextView t2 = (TextView) v.findViewById(android.R.id.text2);
                t1.setSingleLine();
                t2.setSingleLine();
                t1.setEllipsize(TextUtils.TruncateAt.END);
                t2.setEllipsize(TextUtils.TruncateAt.START);
                t1.setText(item.title);
                t2.setText(item.filepath);
                ChanModule chan = MainApplication.getInstance().getChanModule(item.chan);
                if (chan != null) {
                    t1.setCompoundDrawablesWithIntrinsicBounds(chan.getChanFavicon(), null, null, null);
                    t1.setCompoundDrawablePadding(drawablePadding);
                }
            }
            return v;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            return position == 0 ? HEAD_ITEM : NORMAL_ITEM;
        }
    };

    if (savedThreadsAdapter.getCount() == 1) {
        selectFile();
        return;
    }
    DialogInterface.OnClickListener listListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == 0) {
                selectFile();
            } else {
                Database.SavedThreadEntry item = (Database.SavedThreadEntry) savedThreadsAdapter.getItem(which);
                LocalHandler.open(item.filepath, activity);
            }
        }
    };
    new AlertDialog.Builder(activity).setTitle(R.string.newtab_saved_threads_title)
            .setAdapter(savedThreadsAdapter, listListener).setNegativeButton(android.R.string.cancel, null)
            .show();
}

From source file:com.huewu.pla.lib.internal.PLAListView.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   w w  w  . jav a 2  s.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 ListView with the given children.
 */
final int measureHeightOfChildren(final int widthMeasureSpec, final int startPosition, int endPosition,
        final int maxHeight, final 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 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 PLAAbsListView.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);
        }

        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;
}