Example usage for android.widget Adapter getItemId

List of usage examples for android.widget Adapter getItemId

Introduction

In this page you can find the example usage for android.widget Adapter getItemId.

Prototype

long getItemId(int position);

Source Link

Document

Get the row id associated with the specified position in the list.

Usage

From source file:Main.java

public static final int getAdapterPositionById(final Adapter adapter, final long id)
        throws NoSuchElementException {
    final int count = adapter.getCount();

    for (int pos = 0; pos < count; pos++) {
        if (id == adapter.getItemId(pos)) {
            return pos;
        }/*  w  w  w.jav a 2s . c o m*/
    }

    throw new NoSuchElementException();
}

From source file:com.cocosw.accessory.views.adapter.AdapterViewAnimator.java

private void beforeDataSetChanged() {
    Adapter adapter = adapterView.getAdapter();
    final int firstVisiblePosition = adapterView.getFirstVisiblePosition();
    for (int i = 0, childCount = adapterView.getChildCount(); i < childCount; i++) {
        final int position = firstVisiblePosition + i;
        final long id = adapter.getItemId(position);
        final View child = adapterView.getChildAt(i);
        Rect r = new Rect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
        ViewCompat.setHasTransientState(child, false);
        viewBounds.put(id, r);/*from  w  w w.  j a  va  2  s .  c o m*/
        idToViewMap.put(id, child);
    }
}

From source file:co.edu.uniajc.vtf.content.SettingsFragment.java

public int getAdapterPositionById(Adapter adapter, final long id) throws NoSuchElementException {
    final int count = adapter.getCount();

    for (int pos = 0; pos < count; pos++) {
        if (id == adapter.getItemId(pos)) {
            int liPosition = pos;
            return liPosition;
        }//  w w w  .ja  v  a  2  s . c  o  m
    }
    return 0;
}

From source file:com.cocosw.accessory.views.adapter.AdapterViewAnimator.java

public void animate() {
    if (animateCalled) {
        throw new RuntimeException("animate must only be called once");
    }//from  w w w  . ja va 2  s.c o  m
    animateCalled = true;

    final ViewTreeObserver observer = adapterView.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            observer.removeOnPreDrawListener(this);

            Adapter adapter = adapterView.getAdapter();
            final int firstVisiblePosition = adapterView.getFirstVisiblePosition();
            for (int i = 0, childCount = adapterView.getChildCount(); i < childCount; i++) {
                final int position = firstVisiblePosition + i;
                final long id = adapter.getItemId(position);
                idToViewMap.remove(id);
                final View child = adapterView.getChildAt(i);

                final Rect bounds = viewBounds.get(id);
                Runnable endAction = new Runnable() {
                    @Override
                    public void run() {
                        ViewCompat.setHasTransientState(child, false);
                    }
                };
                if (bounds != null) {
                    if (callback == null
                            || !callback.onMoveView(adapterView, child, position, id, bounds, endAction)) {
                        final int dx = bounds.left - child.getLeft();
                        final int dy = bounds.top - child.getTop();
                        ViewCompat.setTranslationX(child, dx);
                        ViewCompat.setTranslationY(child, dy);
                        ViewCompat.animate(child).setDuration(DURATION_MOVE).translationX(0.0f)
                                .translationY(0.0f).withEndAction(endAction);
                    }
                } else {
                    if (callback == null || !callback.onAddView(adapterView, child, position, id)) {
                        ViewCompat.setAlpha(child, 0.0f);
                        ViewCompat.animate(child).setDuration(DURATION_ADD).alpha(1.0f);
                    }
                }
            }

            int[] adapterViewLocation = new int[2];
            int[] hostViewLocation = new int[2];
            final int size = idToViewMap.size();
            for (int i = 0; i < size; i++) {
                final long id = idToViewMap.keyAt(i);
                final View child = idToViewMap.get(id);
                ViewCompat.setHasTransientState(child, false);
                final View viewCopy = new ViewCopy(child);
                Rect bounds = viewBounds.get(id);

                if (overlay == null) {
                    ViewGroup parent = (ViewGroup) adapterView.getParent();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
                        overlay = parent.getOverlay();
                    adapterView.getLocationOnScreen(adapterViewLocation);
                    parent.getLocationOnScreen(hostViewLocation);
                }

                overlay.add(viewCopy);
                viewCopy.offsetLeftAndRight(adapterViewLocation[0] - hostViewLocation[0]);
                viewCopy.offsetTopAndBottom(adapterViewLocation[1] - hostViewLocation[1]);

                if (callback == null || !callback.onRemoveView(adapterView, viewCopy, id, bounds)) {
                    ViewCompat.animate(viewCopy).setDuration(DURATION_REMOVE).alpha(0.0f)
                            .withEndAction(new Runnable() {
                                @Override
                                public void run() {
                                    overlay.remove(viewCopy);
                                }
                            });
                }
            }

            return true;
        }
    });
}

From source file:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.ui.widget.ListWidgetConfig.java

private int getListPositionOf(final Adapter adapter, final long id) {
    if (adapter == null || adapter.getCount() == 0)
        return 0;
    int pos = 0;/* w ww  .  java2  s  .  c o  m*/
    for (int i = 0; i < adapter.getCount(); i++) {
        if (adapter.getItemId(i) == id) {
            pos = i;
            break;
        }
    }
    return pos;
}

From source file:co.codecrunch.musicplayerlite.recyclerviewutils.ItemSelectionSupport.java

/**
 * Sets the checked state of the specified position. The is only valid if
 * the choice mode has been set to {@link ChoiceMode#SINGLE} or
 * {@link ChoiceMode#MULTIPLE}.//from  w  ww.j a  v a2 s.c o  m
 *
 * @param position The item whose checked state is to be checked
 * @param checked The new checked state for the item
 */
public void setItemChecked(int position, boolean checked) {
    if (mChoiceMode == ChoiceMode.NONE) {
        return;
    }

    final Adapter adapter = mRecyclerView.getAdapter();

    if (mChoiceMode == ChoiceMode.MULTIPLE) {
        boolean oldValue = mCheckedStates.get(position);
        mCheckedStates.put(position, checked);

        if (mCheckedIdStates != null && adapter.hasStableIds()) {
            if (checked) {
                mCheckedIdStates.put(adapter.getItemId(position), position);
            } else {
                mCheckedIdStates.delete(adapter.getItemId(position));
            }
        }

        if (oldValue != checked) {
            if (checked) {
                mCheckedCount++;
            } else {
                mCheckedCount--;
            }
        }
    } else {
        boolean updateIds = mCheckedIdStates != null && adapter.hasStableIds();

        // Clear all values if we're checking something, or unchecking the currently
        // selected item
        if (checked || isItemChecked(position)) {
            mCheckedStates.clear();

            if (updateIds) {
                mCheckedIdStates.clear();
            }
        }

        // This may end up selecting the checked we just cleared but this way
        // we ensure length of mCheckStates is 1, a fact getCheckedItemPosition relies on
        if (checked) {
            mCheckedStates.put(position, true);

            if (updateIds) {
                mCheckedIdStates.put(adapter.getItemId(position), position);
            }

            mCheckedCount = 1;
        } else if (mCheckedStates.size() == 0 || !mCheckedStates.valueAt(0)) {
            mCheckedCount = 0;
        }
    }

    updateOnScreenCheckedViews();
}

From source file:co.codecrunch.musicplayerlite.recyclerviewutils.ItemSelectionSupport.java

public void onAdapterDataChanged() {
    final Adapter adapter = mRecyclerView.getAdapter();
    if (mChoiceMode == ChoiceMode.NONE || adapter == null || !adapter.hasStableIds()) {
        return;//from  w  w w  .j a  va2s . co m
    }

    final int itemCount = adapter.getItemCount();

    // Clear out the positional check states, we'll rebuild it below from IDs.
    mCheckedStates.clear();

    for (int checkedIndex = 0; checkedIndex < mCheckedIdStates.size(); checkedIndex++) {
        final long currentId = mCheckedIdStates.keyAt(checkedIndex);
        final int currentPosition = mCheckedIdStates.valueAt(checkedIndex);

        final long newPositionId = adapter.getItemId(currentPosition);
        if (currentId != newPositionId) {
            // Look around to see if the ID is nearby. If not, uncheck it.
            final int start = Math.max(0, currentPosition - CHECK_POSITION_SEARCH_DISTANCE);
            final int end = Math.min(currentPosition + CHECK_POSITION_SEARCH_DISTANCE, itemCount);

            boolean found = false;
            for (int searchPos = start; searchPos < end; searchPos++) {
                final long searchId = adapter.getItemId(searchPos);
                if (currentId == searchId) {
                    found = true;
                    mCheckedStates.put(searchPos, true);
                    mCheckedIdStates.setValueAt(checkedIndex, searchPos);
                    break;
                }
            }

            if (!found) {
                mCheckedIdStates.delete(currentId);
                mCheckedCount--;
                checkedIndex--;
            }
        } else {
            mCheckedStates.put(currentPosition, true);
        }
    }
}