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:cx.ring.fragments.CallListFragment.java

public void setGridViewHeight(GridView gridView, LinearLayout llMain) {
    ListAdapter listAdapter = gridView.getAdapter();
    if (listAdapter == null) {
        return;/*from  www  .java 2s.c  om*/
    }

    int totalHeight = 0;
    int firstHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(gridView.getWidth(), View.MeasureSpec.AT_MOST);

    int rows = (listAdapter.getCount() + gridView.getNumColumns() - 1) / gridView.getNumColumns();

    for (int i = 0; i < rows; i++) {
        if (i == 0) {
            View listItem = listAdapter.getView(i, null, gridView);
            listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            firstHeight = listItem.getMeasuredHeight();
        }
        totalHeight += firstHeight;
    }

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llMain.getLayoutParams();

    params.height = (int) (totalHeight
            + (getResources().getDimension(R.dimen.contact_vertical_spacing) * (rows - 1)
                    + llMain.getPaddingBottom() + llMain.getPaddingTop()));
    llMain.setLayoutParams(params);
    mHeader.requestLayout();
}

From source file:android.support.v7.internal.widget.ListViewCompat.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.
 *
 * @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.
 *//*from www. j  a va  2 s .c om*/
public int measureHeightOfChildrenCompat(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final int paddingTop = getListPaddingTop();
    final int paddingBottom = getListPaddingBottom();
    final int paddingLeft = getListPaddingLeft();
    final int paddingRight = getListPaddingRight();
    final int reportedDividerHeight = getDividerHeight();
    final Drawable divider = getDivider();

    final ListAdapter adapter = getAdapter();

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

    // Include the padding of the list
    int returnedHeight = paddingTop + paddingBottom;
    final int dividerHeight = ((reportedDividerHeight > 0) && divider != null) ? reportedDividerHeight : 0;

    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevHeightWithoutPartialChild = 0;

    View child = null;
    int viewType = 0;
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        int newType = adapter.getItemViewType(i);
        if (newType != viewType) {
            child = null;
            viewType = newType;
        }
        child = adapter.getView(i, child, this);

        // Compute child height spec
        int heightMeasureSpec;
        final ViewGroup.LayoutParams childLp = child.getLayoutParams();
        if (childLp != null && childLp.height > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(childLp.height, MeasureSpec.EXACTLY);
        } else {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(widthMeasureSpec, heightMeasureSpec);

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

        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:org.getlantern.firetweet.fragment.support.BaseSupportListFragment.java

@Override
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount,
        final int totalItemCount) {
    mListScrollCounter.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
    final ListAdapter adapter = getListAdapter();
    if (adapter == null)
        return;// ww  w  .ja  v a 2s .  c  om
    final boolean reachedTop = firstVisibleItem == 0;
    final boolean reachedBottom = firstVisibleItem + visibleItemCount >= totalItemCount
            && totalItemCount >= visibleItemCount;

    if (mReachedBottom != reachedBottom) {
        mReachedBottom = reachedBottom;
        if (mReachedBottom && mNotReachedBottomBefore) {
            mNotReachedBottomBefore = false;
            return;
        }
        if (mReachedBottom && adapter.getCount() > visibleItemCount) {
            onReachedBottom();
        }
    }
    if (mReachedTop != reachedTop) {
        mReachedTop = reachedTop;
        if (mReachedTop && mNotReachedTopBefore) {
            mNotReachedTopBefore = false;
            return;
        }
        if (mReachedTop && adapter.getCount() > visibleItemCount) {
            onReachedTop();
        }
    }
}

From source file:android.support.v7.widget.ListViewCompat.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.
 *
 * @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.
 *///from   w w w.jav  a 2  s  .  c  om
public int measureHeightOfChildrenCompat(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final int paddingTop = getListPaddingTop();
    final int paddingBottom = getListPaddingBottom();
    final int paddingLeft = getListPaddingLeft();
    final int paddingRight = getListPaddingRight();
    final int reportedDividerHeight = getDividerHeight();
    final Drawable divider = getDivider();

    final ListAdapter adapter = getAdapter();

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

    // Include the padding of the list
    int returnedHeight = paddingTop + paddingBottom;
    final int dividerHeight = ((reportedDividerHeight > 0) && divider != null) ? reportedDividerHeight : 0;

    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevHeightWithoutPartialChild = 0;

    View child = null;
    int viewType = 0;
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        int newType = adapter.getItemViewType(i);
        if (newType != viewType) {
            child = null;
            viewType = newType;
        }
        child = adapter.getView(i, child, this);

        // Compute child height spec
        int heightMeasureSpec;
        ViewGroup.LayoutParams childLp = child.getLayoutParams();

        if (childLp == null) {
            childLp = generateDefaultLayoutParams();
            child.setLayoutParams(childLp);
        }

        if (childLp.height > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(childLp.height, MeasureSpec.EXACTLY);
        } else {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(widthMeasureSpec, heightMeasureSpec);

        // Since this view was measured directly aginst the parent measure
        // spec, we must measure it again before reuse.
        child.forceLayout();

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

        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.tr.ui.widgets.HorizontalListView.java

/**
* Find a position that can be selected (i.e., is not a separator).
*
* @param position The starting position to look at.
* @param lookDown Whether to look down for other positions.
* @return The next selectable position starting at position and then searching either up or
* down. Returns {@link #INVALID_POSITION} if nothing can be found.
*//*from  w ww .  j  a  v a2s.com*/
private int lookForSelectablePosition(int position, boolean lookDown) {
    final ListAdapter adapter = mAdapter;
    if (adapter == null || isInTouchMode()) {
        return INVALID_POSITION;
    }

    final int count = adapter.getCount();
    if (!adapter.areAllItemsEnabled()) {
        if (lookDown) {
            position = Math.max(0, position);
            while (position < count && !adapter.isEnabled(position)) {
                position++;
            }
        } else {
            position = Math.min(position, count - 1);
            while (position >= 0 && !adapter.isEnabled(position)) {
                position--;
            }
        }

        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    } else {
        if (position < 0 || position >= count) {
            return INVALID_POSITION;
        }
        return position;
    }
}

From source file:com.farmerbb.taskbar.service.StartMenuService.java

@SuppressLint("RtlHardcoded")
private void drawStartMenu() {
    IconCache.getInstance(this).clearCache();

    final SharedPreferences pref = U.getSharedPreferences(this);
    final boolean hasHardwareKeyboard = getResources()
            .getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS;

    switch (pref.getString("show_search_bar", "keyboard")) {
    case "always":
        shouldShowSearchBox = true;/*  w  w  w  .  j  a  va2s.c o  m*/
        break;
    case "keyboard":
        shouldShowSearchBox = hasHardwareKeyboard;
        break;
    case "never":
        shouldShowSearchBox = false;
        break;
    }

    // Initialize layout params
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    U.setCachedRotation(windowManager.getDefaultDisplay().getRotation());

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            shouldShowSearchBox ? 0
                    : WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
            PixelFormat.TRANSLUCENT);

    // Determine where to show the start menu on screen
    switch (U.getTaskbarPosition(this)) {
    case "bottom_left":
        layoutId = R.layout.start_menu_left;
        params.gravity = Gravity.BOTTOM | Gravity.LEFT;
        break;
    case "bottom_vertical_left":
        layoutId = R.layout.start_menu_vertical_left;
        params.gravity = Gravity.BOTTOM | Gravity.LEFT;
        break;
    case "bottom_right":
        layoutId = R.layout.start_menu_right;
        params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
        break;
    case "bottom_vertical_right":
        layoutId = R.layout.start_menu_vertical_right;
        params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
        break;
    case "top_left":
        layoutId = R.layout.start_menu_top_left;
        params.gravity = Gravity.TOP | Gravity.LEFT;
        break;
    case "top_vertical_left":
        layoutId = R.layout.start_menu_vertical_left;
        params.gravity = Gravity.TOP | Gravity.LEFT;
        break;
    case "top_right":
        layoutId = R.layout.start_menu_top_right;
        params.gravity = Gravity.TOP | Gravity.RIGHT;
        break;
    case "top_vertical_right":
        layoutId = R.layout.start_menu_vertical_right;
        params.gravity = Gravity.TOP | Gravity.RIGHT;
        break;
    }

    // Initialize views
    int theme = 0;

    switch (pref.getString("theme", "light")) {
    case "light":
        theme = R.style.AppTheme;
        break;
    case "dark":
        theme = R.style.AppTheme_Dark;
        break;
    }

    ContextThemeWrapper wrapper = new ContextThemeWrapper(this, theme);
    layout = (StartMenuLayout) LayoutInflater.from(wrapper).inflate(layoutId, null);
    startMenu = (GridView) layout.findViewById(R.id.start_menu);

    if ((shouldShowSearchBox && !hasHardwareKeyboard)
            || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)
        layout.viewHandlesBackButton();

    boolean scrollbar = pref.getBoolean("scrollbar", false);
    startMenu.setFastScrollEnabled(scrollbar);
    startMenu.setFastScrollAlwaysVisible(scrollbar);
    startMenu.setScrollBarStyle(scrollbar ? View.SCROLLBARS_OUTSIDE_INSET : View.SCROLLBARS_INSIDE_OVERLAY);

    if (pref.getBoolean("transparent_start_menu", false))
        startMenu.setBackgroundColor(0);

    searchView = (SearchView) layout.findViewById(R.id.search);

    int backgroundTint = U.getBackgroundTint(this);

    FrameLayout startMenuFrame = (FrameLayout) layout.findViewById(R.id.start_menu_frame);
    FrameLayout searchViewLayout = (FrameLayout) layout.findViewById(R.id.search_view_layout);
    startMenuFrame.setBackgroundColor(backgroundTint);
    searchViewLayout.setBackgroundColor(backgroundTint);

    if (shouldShowSearchBox) {
        if (!hasHardwareKeyboard)
            searchView.setIconifiedByDefault(true);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (!hasSubmittedQuery) {
                    ListAdapter adapter = startMenu.getAdapter();
                    if (adapter != null) {
                        hasSubmittedQuery = true;

                        if (adapter.getCount() > 0) {
                            View view = adapter.getView(0, null, startMenu);
                            LinearLayout layout = (LinearLayout) view.findViewById(R.id.entry);
                            layout.performClick();
                        } else {
                            if (pref.getBoolean("hide_taskbar", true)
                                    && !FreeformHackHelper.getInstance().isInFreeformWorkspace())
                                LocalBroadcastManager.getInstance(StartMenuService.this)
                                        .sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_TASKBAR"));
                            else
                                LocalBroadcastManager.getInstance(StartMenuService.this)
                                        .sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));

                            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                            intent.putExtra(SearchManager.QUERY, query);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            if (intent.resolveActivity(getPackageManager()) != null)
                                startActivity(intent);
                            else {
                                Uri uri = new Uri.Builder().scheme("https").authority("www.google.com")
                                        .path("search").appendQueryParameter("q", query).build();

                                intent = new Intent(Intent.ACTION_VIEW);
                                intent.setData(uri);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                                try {
                                    startActivity(intent);
                                } catch (ActivityNotFoundException e) {
                                    /* Gracefully fail */ }
                            }
                        }
                    }
                }
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                searchView.setIconified(false);

                View closeButton = searchView.findViewById(R.id.search_close_btn);
                if (closeButton != null)
                    closeButton.setVisibility(View.GONE);

                refreshApps(newText, false);

                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
                    new Handler().postDelayed(() -> {
                        EditText editText = (EditText) searchView.findViewById(R.id.search_src_text);
                        if (editText != null) {
                            editText.requestFocus();
                            editText.setSelection(editText.getText().length());
                        }
                    }, 50);
                }

                return true;
            }
        });

        searchView.setOnQueryTextFocusChangeListener((view, b) -> {
            if (!hasHardwareKeyboard) {
                ViewGroup.LayoutParams params1 = startMenu.getLayoutParams();
                params1.height = getResources().getDimensionPixelSize(b
                        && !U.isServiceRunning(this, "com.farmerbb.secondscreen.service.DisableKeyboardService")
                                ? R.dimen.start_menu_height_half
                                : R.dimen.start_menu_height);
                startMenu.setLayoutParams(params1);
            }

            if (!b) {
                if (hasHardwareKeyboard && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
                    LocalBroadcastManager.getInstance(StartMenuService.this)
                            .sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
                else {
                    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            }
        });

        searchView.setImeOptions(EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI);

        LinearLayout powerButton = (LinearLayout) layout.findViewById(R.id.power_button);
        powerButton.setOnClickListener(view -> {
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            openContextMenu(location);
        });

        powerButton.setOnGenericMotionListener((view, motionEvent) -> {
            if (motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
                    && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                openContextMenu(location);
            }
            return false;
        });

        searchViewLayout.setOnClickListener(view -> searchView.setIconified(false));

        startMenu.setOnItemClickListener((parent, view, position, id) -> {
            hideStartMenu();

            AppEntry entry = (AppEntry) parent.getAdapter().getItem(position);
            U.launchApp(StartMenuService.this, entry.getPackageName(), entry.getComponentName(),
                    entry.getUserId(StartMenuService.this), null, false, false);
        });

        if (pref.getBoolean("transparent_start_menu", false))
            layout.findViewById(R.id.search_view_child_layout).setBackgroundColor(0);
    } else
        searchViewLayout.setVisibility(View.GONE);

    textView = (TextView) layout.findViewById(R.id.no_apps_found);

    LocalBroadcastManager.getInstance(this).unregisterReceiver(toggleReceiver);
    LocalBroadcastManager.getInstance(this).unregisterReceiver(toggleReceiverAlt);
    LocalBroadcastManager.getInstance(this).unregisterReceiver(hideReceiver);

    LocalBroadcastManager.getInstance(this).registerReceiver(toggleReceiver,
            new IntentFilter("com.farmerbb.taskbar.TOGGLE_START_MENU"));
    LocalBroadcastManager.getInstance(this).registerReceiver(toggleReceiverAlt,
            new IntentFilter("com.farmerbb.taskbar.TOGGLE_START_MENU_ALT"));
    LocalBroadcastManager.getInstance(this).registerReceiver(hideReceiver,
            new IntentFilter("com.farmerbb.taskbar.HIDE_START_MENU"));

    handler = new Handler();
    refreshApps(true);

    windowManager.addView(layout, params);
}

From source file:net.naonedbus.fragment.CustomListFragment.java

@Override
public void onLoadFinished(final Loader<AsyncResult<ListAdapter>> loader,
        final AsyncResult<ListAdapter> result) {
    if (DBG)/*  www  . j ava 2  s.  c  om*/
        Log.d(LOG_TAG + "$" + getClass().getSimpleName(), "onLoadFinished " + result);

    if (result == null) {
        showMessage(mMessageEmptyTitleId, mMessageEmptySummaryId, mMessageEmptyDrawableId);
        return;
    }

    final Exception exception = result.getException();

    if (exception == null) {

        final ListAdapter adapter = result.getResult();
        setListAdapter(adapter);

        if (adapter == null) {
            showMessage(mMessageEmptyTitleId, mMessageEmptySummaryId, mMessageEmptyDrawableId);
        } else {
            adapter.registerDataSetObserver(new DataSetObserver() {
                @Override
                public void onChanged() {
                    super.onChanged();
                    onListAdapterChange(adapter);
                }
            });

            if (adapter.getCount() > 0) {
                if (mListViewStatePosition != -1 && isAdded()) {
                    getListView().setSelectionFromTop(mListViewStatePosition, mListViewStateTop);
                    mListViewStatePosition = -1;
                }

                showContent();
                resetNextUpdate();
            } else {
                showMessage(mMessageEmptyTitleId, mMessageEmptySummaryId, mMessageEmptyDrawableId);
            }
        }

    } else {

        int titleRes = R.string.error_title;
        int messageRes = R.string.error_summary;
        int drawableRes = R.drawable.warning;

        // Erreur rseau ou interne ?
        if (exception instanceof IOException) {
            titleRes = R.string.error_title_network;
            messageRes = R.string.error_summary_network;
            drawableRes = R.drawable.ic_thunderstorm;
        } else if (exception instanceof JSONException) {
            titleRes = R.string.error_title_webservice;
            messageRes = R.string.error_summary_webservice;
        }

        if (getListAdapter() == null || getListAdapter().isEmpty()) {
            showMessage(titleRes, messageRes, drawableRes);
        } else {
            Crouton.makeText(getActivity(), titleRes, Style.ALERT, (ViewGroup) getView()).show();
        }

        Log.e(getClass().getSimpleName(), "Erreur de chargement.", exception);
    }

    onPostExecute();
}

From source file:com.actionbarsherlock.internal.widget.IcsListPopupWindow.java

private int measureHeightOfChildren(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final ListAdapter adapter = mAdapter;
    if (adapter == null) {
        return mDropDownList.getListPaddingTop() + mDropDownList.getListPaddingBottom();
    }/*from ww w  . ja  va  2  s .c  o m*/

    // Include the padding of the list
    int returnedHeight = mDropDownList.getListPaddingTop() + mDropDownList.getListPaddingBottom();
    final int dividerHeight = ((mDropDownList.getDividerHeight() > 0) && mDropDownList.getDivider() != null)
            ? mDropDownList.getDividerHeight()
            : 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 == -1/*NO_POSITION*/) ? adapter.getCount() - 1 : endPosition;

    for (i = startPosition; i <= endPosition; ++i) {
        child = mAdapter.getView(i, null, mDropDownList);
        if (mDropDownList.getCacheColorHint() != 0) {
            child.setDrawingCacheBackgroundColor(mDropDownList.getCacheColorHint());
        }

        measureScrapChild(child, i, widthMeasureSpec);

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

        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.zgntech.core.view.HorizontalListView.java

@Override
public void setSelection(int position) {
    mCurrentlySelectedAdapterIndex = position;
    ListAdapter adapter = getAdapter();
    if (null != adapter && adapter.getCount() > 0 && null != getChildAt(0)) {
        int positionX = mCurrentlySelectedAdapterIndex * getChildAt(0).getWidth();
        int maxWidth = adapter.getCount() * getChildAt(0).getWidth();
        Log.d("aaaaa",
                "position:" + position + ",positionX:" + positionX + ",this.getWidth():" + this.getWidth()
                        + ",maxWidth:" + maxWidth + ",this.getChildCount():" + this.getAdapter().getCount());
        if (positionX <= 0) {
            positionX = 0;//from   ww  w.j  av  a 2 s . co  m
        }
        if (positionX > maxWidth) {
            positionX = maxWidth;
        }
        scrollTo(positionX);
    }
}

From source file:dev.ukanth.ufirewall.MainActivity.java

private void selectAllWifi(boolean flag) {
    if (this.listview == null) {
        this.listview = (ListView) this.findViewById(R.id.listview);
    }/*from   w w w  .  j  a v  a2  s .  c  o  m*/
    ListAdapter adapter = listview.getAdapter();
    int count = adapter.getCount(), item;
    if (adapter != null) {
        for (item = 0; item < count; item++) {
            PackageInfoData data = (PackageInfoData) adapter.getItem(item);
            if (data.uid != Api.SPECIAL_UID_ANY) {
                data.selected_wifi = flag;
            }
            setDirty(true);
        }
        ((BaseAdapter) adapter).notifyDataSetChanged();
    }
}