Java Code Examples for android.widget.ListAdapter#getCount()

The following examples show how to use android.widget.ListAdapter#getCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbsListScrollSize.java    From Paralloid with Apache License 2.0 6 votes vote down vote up
/**
     * 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;
    }
 
Example 2
Source File: RecruitDataActivity.java    From Social with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: MHUtils.java    From MonsterHunter4UDatabase with MIT License 6 votes vote down vote up
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();
    }
}
 
Example 4
Source File: FullListView.java    From FacebookNewsfeedSample-Android with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 5
Source File: ViewToImageUtil.java    From ViewToImage with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: MergeAdapter.java    From Shield with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: PLA_AbsListView.java    From EverMemo with MIT License 5 votes vote down vote up
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));
	}
}
 
Example 8
Source File: PrettyPreferenceActivity.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 9
Source File: PinnedSectionListView.java    From AndroidWeekly with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 10
Source File: MergeAdapter.java    From SimpleExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: MergeAdapter.java    From AndroidBleManager with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 12
Source File: MergeAdapter.java    From BLE with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: WidgetUtils.java    From styT with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: MergeAdapter.java    From Shield with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: HListView.java    From letv with Apache License 2.0 5 votes vote down vote up
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)};
}
 
Example 16
Source File: MergeAdapter.java    From BLE with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 17
Source File: ListPopupWindow.java    From MDPreference with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: ZrcListView.java    From AndroidStudyDemo with GNU General Public License v2.0 4 votes vote down vote up
@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();
}
 
Example 19
Source File: ArtistDetailActivity.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
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();

    }
 
Example 20
Source File: SnippetListActivityTests.java    From android-java-snippets-sample with MIT License 3 votes vote down vote up
private int getItemsCount(ActivityTestRule<SnippetListActivity> snippetListActivityRule){
    SnippetListActivity snippetListActivity = snippetListActivityRule.launchActivity(null);

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

    snippetListActivity.finish();

    return numItems;
}