android.widget.ListAdapter#getCount ( )源码实例Demo

下面列出了android.widget.ListAdapter#getCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Paralloid   文件: AbsListScrollSize.java
/**
     * This method is by no means accurate, and Will only work to any degree of accuracy if your list items
     * are the same height.
     * Otherwise it becomes vastly more difficult to calculate the correct height.
     *
     * @param listView listView to get height of, if no adapter is attached then nothing will happen.
     * @return 0 for failure.
     */
    public static int calculateApproximateHeight(AbsListView listView) {
        final ListAdapter adapter = listView.getAdapter();
        int onScreenHeight = 0, totalHeight = 0;
        final int totalCount = adapter.getCount();
        final int visibleCount = listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
        if (totalCount > 0) {
            View view;
            for (int i = 0; i < visibleCount; i++) {
//                final View view = adapter.getView(0, null, listView);
                view = listView.getChildAt(i);
//                view.measure(
//                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
//                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                onScreenHeight += view.getMeasuredHeight();
            }
            // Get the average on screen height, then multiply it up.
            totalHeight = (onScreenHeight / visibleCount) * totalCount;
            // Add the divider height.
            if (listView instanceof ListView) {
                totalHeight += ((ListView) listView).getDividerHeight() * (totalCount - 1);
            }
        }
        return totalHeight;
    }
 
源代码2 项目: MonsterHunter4UDatabase   文件: MHUtils.java
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    if(listView.isInLayout() == false){
        listView.requestLayout();
    }
}
 
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();
    int height = 0;
    ListAdapter adapter = getAdapter();
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        View childView = adapter.getView(i, null, this);
        childView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        height += childView.getMeasuredHeight();
    }
    Rect bgPadding = new Rect();
    getBackground().getPadding(bgPadding);
    height += (count - 1) * getDividerHeight() + bgPadding.top + bgPadding.bottom;
    setMeasuredDimension(width, height);
}
 
源代码4 项目: ViewToImage   文件: ViewToImageUtil.java
/**
 * ListView转换成bitmap
 *
 * @param listView
 * @return List<Bitmap>
 */
public static List<BitmapWithHeight> getWholeListViewItemsToBitmap(final ListView listView) {
    List<BitmapWithHeight> list = new ArrayList<>();
    if (listView == null || listView.getAdapter() == null) {
        return list;
    }

    ListAdapter adapter = listView.getAdapter();
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        View childView = adapter.getView(i, null, listView);
        list.add(getSimpleViewToBitmap(childView, listView.getMeasuredWidth()));
    }

    return list;
}
 
源代码5 项目: Social   文件: RecruitDataActivity.java
public void setListViewHeightBasedOnChildren(ListView listView) {
    // 获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
        // listAdapter.getCount()返回数据项的数目
        View listItem = listAdapter.getView(i, null, listView);
        // 计算子项View 的宽高
        listItem.measure(0, 0);
        // 统计所有子项的总高度
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
}
 
源代码6 项目: AndroidBleManager   文件: MergeAdapter.java
@Override
public int getSectionForPosition(int position) {
    int section=0;

    for (ListAdapter piece : getPieces()) {
        int size=piece.getCount();

        if (position < size) {
            if (piece instanceof SectionIndexer) {
                return(section + ((SectionIndexer)piece).getSectionForPosition(position));
            }

            return(0);
        }
        else {
            if (piece instanceof SectionIndexer) {
                Object[] sections=((SectionIndexer)piece).getSections();

                if (sections != null) {
                    section+=sections.length;
                }
            }
        }

        position-=size;
    }

    return(0);
}
 
源代码7 项目: BLE   文件: MergeAdapter.java
public long getItemId(int position) {
    int size;
    for (Iterator i$ = this.getPieces().iterator(); i$.hasNext(); position -= size) {
        ListAdapter piece = (ListAdapter) i$.next();
        size = piece.getCount();
        if (position < size) {
            return piece.getItemId(position);
        }
    }

    return -1L;
}
 
源代码8 项目: letv   文件: HListView.java
final int[] measureWithLargeChildren(int heightMeasureSpec, int startPosition, int endPosition, int maxWidth, int maxHeight, int disallowPartialChildPosition) {
    ListAdapter adapter = this.mAdapter;
    if (adapter == null) {
        return new int[]{this.mListPadding.left + this.mListPadding.right, this.mListPadding.top + this.mListPadding.bottom};
    }
    int returnedWidth = this.mListPadding.left + this.mListPadding.right;
    int returnedHeight = this.mListPadding.top + this.mListPadding.bottom;
    int dividerWidth = (this.mDividerWidth <= 0 || this.mDivider == null) ? 0 : this.mDividerWidth;
    int childWidth = 0;
    int childHeight = 0;
    if (endPosition == -1) {
        endPosition = adapter.getCount() - 1;
    }
    RecycleBin recycleBin = this.mRecycler;
    boolean recyle = recycleOnMeasure();
    boolean[] isScrap = this.mIsScrap;
    for (int i = startPosition; i <= endPosition; i++) {
        View child = obtainView(i, isScrap);
        measureScrapChildWidth(child, i, heightMeasureSpec);
        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());
    }
    returnedHeight += childHeight;
    return new int[]{Math.min(returnedWidth + childWidth, maxWidth), Math.min(returnedHeight, maxHeight)};
}
 
源代码9 项目: Shield   文件: MergeAdapter.java
/**
 * How many items are in the data set represented by this Adapter.
 */
@Override
public int getCount() {
    int total = 0;

    for (ListAdapter piece : pieces) {
        total += piece.getCount();
    }

    return (total);
}
 
源代码10 项目: styT   文件: WidgetUtils.java
public static void setListViewHeightBasedOnChildren(GridView listView) {
    // 获取listview的adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    // 固定列宽,有多少列
    int col = 3;// listView.getNumColumns();
    int totalHeight = 0;
    // i每次加4,相当于listAdapter.getCount()小于等于4时 循环一次,计算一次item的高度,
    // listAdapter.getCount()小于等于8时计算两次高度相加
    for (int i = 0; i < listAdapter.getCount(); i += col) {
        // 获取listview的每一个item
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        // 获取item的高度和
        totalHeight += listItem.getMeasuredHeight();
    }

    // 获取listview的布局参数
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    // 设置高度
    params.height = totalHeight;
    // 设置margin
    ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
    // 设置参数
    listView.setLayoutParams(params);
}
 
源代码11 项目: BLE   文件: MergeAdapter.java
public ListAdapter getAdapter(int position) {
    int size;
    for (Iterator i$ = this.getPieces().iterator(); i$.hasNext(); position -= size) {
        ListAdapter piece = (ListAdapter) i$.next();
        size = piece.getCount();
        if (position < size) {
            return piece;
        }
    }

    return null;
}
 
源代码12 项目: Shield   文件: MergeAdapter.java
/**
 * Get the data item associated with the specified position in the data set.
 *
 * @param position Position of the item whose data we want
 */
@Override
public Object getItem(int position) {
    for (ListAdapter piece : pieces) {
        int size = piece.getCount();

        if (position < size) {
            return (piece.getItem(position));
        }

        position -= size;
    }

    return (null);
}
 
源代码13 项目: SimpleExplorer   文件: MergeAdapter.java
/**
 * Get the data item associated with the specified position in the data set.
 *
 * @param position Position of the item whose data we want
 */
public Object getItem(int position) {
    for (ListAdapter piece : pieces) {
        int size = piece.getCount();

        if (position < size) {
            return piece.getItem(position);
        }

        position -= size;
    }

    return null;
}
 
源代码14 项目: AndroidWeekly   文件: PinnedSectionListView.java
void recreatePinnedShadow() {
    destroyPinnedShadow();
    ListAdapter adapter = getAdapter();
    if (adapter != null && adapter.getCount() > 0) {
        int firstVisiblePosition = getFirstVisiblePosition();
        int sectionPosition = findCurrentSectionPosition(firstVisiblePosition);
        if (sectionPosition == -1) return; // no views to pin, exit
        ensureShadowForPosition(sectionPosition,
                firstVisiblePosition, getLastVisiblePosition() - firstVisiblePosition);
    }
}
 
源代码15 项目: Nimingban   文件: PrettyPreferenceActivity.java
@Override
public void setListAdapter(ListAdapter adapter) {
  int count = adapter.getCount();
  List<Header> headers = new ArrayList<>(count);
  for (int i = 0; i < count; ++i) {
    headers.add((Header) adapter.getItem(i));
  }

  super.setListAdapter(new HeaderAdapter(this, headers, R.layout.item_preference_header, true));
}
 
源代码16 项目: EverMemo   文件: PLA_AbsListView.java
public void run() {
	// The data has changed since we posted this action in the event queue,
	// bail out before bad things happen
	if (mDataChanged) return;

	final ListAdapter adapter = mAdapter;
	final int motionPosition = mClickMotionPosition;
	if (adapter != null && mItemCount > 0 &&
			motionPosition != INVALID_POSITION &&
			motionPosition < adapter.getCount() && sameWindow()) {
		performItemClick(mChild, motionPosition, adapter.getItemId(motionPosition));
	}
}
 
源代码17 项目: AndroidStudyDemo   文件: ZrcListView.java
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

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

    requestLayout();
}
 
源代码18 项目: MDPreference   文件: ListPopupWindow.java
/**
 * Filter key down events. By forwarding key down events to this function,
 * views using non-modal ListPopupWindow can have it handle key selection of items.
 *
 * @param keyCode keyCode param passed to the host view's onKeyDown
 * @param event event param passed to the host view's onKeyDown
 * @return true if the event was handled, false if it was ignored.
 *
 * @see #setModal(boolean)
 */
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // when the drop down is shown, we drive it directly
    if (isShowing()) {
        // the key events are forwarded to the list in the drop down view
        // note that ListView handles space but we don't want that to happen
        // also if selection is not currently in the drop down, then don't
        // let center or enter presses go there since that would cause it
        // to select one of its items
        if (keyCode != KeyEvent.KEYCODE_SPACE
                && (mDropDownList.getSelectedItemPosition() >= 0
                || !isConfirmKey(keyCode))) {
            int curIndex = mDropDownList.getSelectedItemPosition();
            boolean consumed;

            final boolean below = !mPopup.isAboveAnchor();

            final ListAdapter adapter = mAdapter;

            boolean allEnabled;
            int firstItem = Integer.MAX_VALUE;
            int lastItem = Integer.MIN_VALUE;

            if (adapter != null) {
                allEnabled = adapter.areAllItemsEnabled();
                firstItem = allEnabled ? 0 :
                        mDropDownList.lookForSelectablePosition(0, true);
                lastItem = allEnabled ? adapter.getCount() - 1 :
                        mDropDownList.lookForSelectablePosition(adapter.getCount() - 1, false);
            }

            if ((below && keyCode == KeyEvent.KEYCODE_DPAD_UP && curIndex <= firstItem) ||
                    (!below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN && curIndex >= lastItem)) {
                // When the selection is at the top, we block the key
                // event to prevent focus from moving.
                clearListSelection();
                mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                show();
                return true;
            } else {
                // WARNING: Please read the comment where mListSelectionHidden
                //          is declared
                mDropDownList.mListSelectionHidden = false;
            }

            consumed = mDropDownList.onKeyDown(keyCode, event);
            if (DEBUG) Log.v(TAG, "Key down: code=" + keyCode + " list consumed=" + consumed);

            if (consumed) {
                // If it handled the key event, then the user is
                // navigating in the list, so we should put it in front.
                mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
                // Here's a little trick we need to do to make sure that
                // the list view is actually showing its focus indicator,
                // by ensuring it has focus and getting its window out
                // of touch mode.
                mDropDownList.requestFocusFromTouch();
                show();

                switch (keyCode) {
                    // avoid passing the focus from the text view to the
                    // next component
                    case KeyEvent.KEYCODE_ENTER:
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                    case KeyEvent.KEYCODE_DPAD_UP:
                        return true;
                }
            } else {
                if (below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    // when the selection is at the bottom, we block the
                    // event to avoid going to the next focusable widget
                    if (curIndex == lastItem) {
                        return true;
                    }
                } else if (!below && keyCode == KeyEvent.KEYCODE_DPAD_UP &&
                        curIndex == firstItem) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
源代码19 项目: Orin   文件: ArtistDetailActivity.java
public void setHeightofListViewBasedOnContent(ListView listView) {

        ListAdapter mAdapter = listView.getAdapter();

        int totalHeight = 0;

        for (int i = 0; i < mAdapter.getCount(); i++) {

            totalHeight += getResources().getDimension(R.dimen.item_list_height);
            Log.w("HEIGHT" + i, String.valueOf(totalHeight));

        }

        totalHeight = totalHeight +  (listView.getDividerHeight() * (mAdapter.getCount() - 1)) + listView.getPaddingTop();

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

    }
 
private int getItemsCount(ActivityTestRule<SnippetListActivity> snippetListActivityRule){
    SnippetListActivity snippetListActivity = snippetListActivityRule.launchActivity(null);

    ListAdapter listAdapter = getListAdapter(snippetListActivity);
    int numItems = listAdapter.getCount();

    snippetListActivity.finish();

    return numItems;
}