Example usage for android.view View requestFocus

List of usage examples for android.view View requestFocus

Introduction

In this page you can find the example usage for android.view View requestFocus.

Prototype

public final boolean requestFocus(int direction) 

Source Link

Document

Call this to try to give focus to a specific view or to one of its descendants and give it a hint about what direction focus is heading.

Usage

From source file:com.ferg.awfulapp.widget.AwfulViewPager.java

void populate() {
    if (mAdapter == null) {
        return;/*from  w w w.  j  a  v a2 s  .c om*/
    }

    // Bail now if we are waiting to populate.  This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a new position until we have finished the scroll to
    // that position, avoiding glitches from happening at that point.
    if (mPopulatePending) {
        //               if (DEBUG) Log.i(TAG, "populate is pending, skipping for now...");
        return;
    }

    // Also, don't populate until we are attached to a window.  This is to
    // avoid trying to populate before we have restored our view hierarchy
    // state and conflicting with what is restored.
    if (getWindowToken() == null) {
        return;
    }

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;
    final int startPos = Math.max(0, mCurItem - pageLimit);
    final int N = mAdapter.getCount();
    final int endPos = Math.min(N - 1, mCurItem + pageLimit);

    //           if (DEBUG) Log.v(TAG, "populating: startPos=" + startPos + " endPos=" + endPos);

    // Add and remove pages in the existing list.
    int lastPos = -1;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {
            //                   if (DEBUG) Log.i(TAG, "removing: " + ii.position + " @ " + i);
            mItems.remove(i);
            i--;
            mAdapter.destroyItem(this, ii.position, ii.object);
        } else if (lastPos < endPos && ii.position > startPos) {
            // The next item is outside of our range, but we have a gap
            // between it and the last item where we want to have a page
            // shown.  Fill in the gap.
            lastPos++;
            if (lastPos < startPos) {
                lastPos = startPos;
            }
            while (lastPos <= endPos && lastPos < ii.position) {
                //                       if (DEBUG) Log.i(TAG, "inserting: " + lastPos + " @ " + i);
                addNewItem(lastPos, i);
                lastPos++;
                i++;
            }
        }
        lastPos = ii.position;
    }

    // Add any new pages we need at the end.
    lastPos = mItems.size() > 0 ? mItems.get(mItems.size() - 1).position : -1;
    if (lastPos < endPos) {
        lastPos++;
        lastPos = lastPos > startPos ? lastPos : startPos;
        while (lastPos <= endPos) {
            //                   if (DEBUG) Log.i(TAG, "appending: " + lastPos);
            addNewItem(lastPos, -1);
            lastPos++;
        }
    }

    //           if (DEBUG) {
    //               Log.i(TAG, "Current page list:");
    //               for (int i=0; i<mItems.size(); i++) {
    //                   Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
    //               }
    //           }

    ItemInfo curItem = null;
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems.get(i).position == mCurItem) {
            curItem = mItems.get(i);
            break;
        }
    }
    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    //this is probably a shitty hack.
    mCurFrag = (AwfulPagerFragment) (curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    if (hasFocus()) {
        View currentFocused = findFocus();
        ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
        if (ii == null || ii.position != mCurItem) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    }
                }
            }
        }
    }
}

From source file:com.tylz.jiaoyanglogistics.view.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;/*  www.  j a va2 s . c  o m*/
    }

    // Bail now if we are waiting to populate.  This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a new position until we have finished the scroll to
    // that position, avoiding glitches from happening at that point.
    if (mPopulatePending) {
        if (DEBUG) {
            Log.i(TAG, "populate is pending, skipping for now...");
        }
        return;
    }

    // Also, don't populate until we are attached to a window.  This is to
    // avoid trying to populate before we have restored our view hierarchy
    // state and conflicting with what is restored.
    if (getWindowToken() == null) {
        return;
    }

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;
    final int startPos = Math.max(0, mCurItem - pageLimit);
    final int N = mAdapter.getCount();
    final int endPos = Math.min(N - 1, mCurItem + pageLimit);

    if (DEBUG) {
        Log.v(TAG, "populating: startPos=" + startPos + " endPos=" + endPos);
    }

    // Add and remove pages in the existing list.
    int lastPos = -1;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {
            if (DEBUG) {
                Log.i(TAG, "removing: " + ii.position + " @ " + i);
            }
            mItems.remove(i);
            i--;
            mAdapter.destroyItem(this, ii.position, ii.object);
        } else if (lastPos < endPos && ii.position > startPos) {
            // The next item is outside of our range, but we have a gap
            // between it and the last item where we want to have a page
            // shown.  Fill in the gap.
            lastPos++;
            if (lastPos < startPos) {
                lastPos = startPos;
            }
            while (lastPos <= endPos && lastPos < ii.position) {
                if (DEBUG) {
                    Log.i(TAG, "inserting: " + lastPos + " @ " + i);
                }
                addNewItem(lastPos, i);
                lastPos++;
                i++;
            }
        }
        lastPos = ii.position;
    }

    // Add any new pages we need at the end.
    lastPos = mItems.size() > 0 ? mItems.get(mItems.size() - 1).position : -1;
    if (lastPos < endPos) {
        lastPos++;
        lastPos = lastPos > startPos ? lastPos : startPos;
        while (lastPos <= endPos) {
            if (DEBUG) {
                Log.i(TAG, "appending: " + lastPos);
            }
            addNewItem(lastPos, -1);
            lastPos++;
        }
    }

    if (DEBUG) {
        Log.i(TAG, "Current page list:");
        for (int i = 0; i < mItems.size(); i++) {
            Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
        }
    }

    ItemInfo curItem = null;
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems.get(i).position == mCurItem) {
            curItem = mItems.get(i);
            break;
        }
    }
    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    if (hasFocus()) {
        View currentFocused = findFocus();
        ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
        if (ii == null || ii.position != mCurItem) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    }
                }
            }
        }
    }
}

From source file:org.immopoly.android.widget.ViewPager.java

void populate() {
    if (mAdapter == null) {
        return;/*from w w w.  j a  v a  2s  . c  o m*/
    }

    // Bail now if we are waiting to populate.  This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a new position until we have finished the scroll to
    // that position, avoiding glitches from happening at that point.
    if (mPopulatePending) {
        if (DEBUG)
            Log.i(TAG, "populate is pending, skipping for now...");
        return;
    }

    // Also, don't populate until we are attached to a window.  This is to
    // avoid trying to populate before we have restored our view hierarchy
    // state and conflicting with what is restored.
    if (getWindowToken() == null) {
        return;
    }

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;
    final int startPos = Math.max(0, mCurItem - pageLimit);
    final int N = mAdapter.getCount();
    final int endPos = Math.min(N - 1, mCurItem + pageLimit);

    if (DEBUG)
        Log.v(TAG, "populating: startPos=" + startPos + " endPos=" + endPos);

    // Add and remove pages in the existing list.
    int lastPos = -1;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if ((ii.position < startPos - 1 || ii.position > endPos + 1) && !ii.scrolling) {
            if (DEBUG)
                Log.i(TAG, "removing: " + ii.position + " @ " + i);
            mItems.remove(i);
            i--;
            mAdapter.destroyItem(this, ii.position, ii.object);
        } else if (lastPos < endPos && ii.position > startPos) {
            // The next item is outside of our range, but we have a gap
            // between it and the last item where we want to have a page
            // shown.  Fill in the gap.
            lastPos++;
            if (lastPos < startPos) {
                lastPos = startPos;
            }
            while (lastPos <= endPos && lastPos < ii.position) {
                if (DEBUG)
                    Log.i(TAG, "inserting: " + lastPos + " @ " + i);
                addNewItem(lastPos, i);
                lastPos++;
                i++;
            }
        }
        lastPos = ii.position;
    }

    // Add any new pages we need at the end.
    lastPos = mItems.size() > 0 ? mItems.get(mItems.size() - 1).position : -1;
    if (lastPos < endPos) {
        lastPos++;
        lastPos = lastPos > startPos ? lastPos : startPos;
        while (lastPos <= endPos) {
            if (DEBUG)
                Log.i(TAG, "appending: " + lastPos);
            addNewItem(lastPos, -1);
            lastPos++;
        }
    }

    if (DEBUG) {
        Log.i(TAG, "Current page list:");
        for (int i = 0; i < mItems.size(); i++) {
            Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
        }
    }

    ItemInfo curItem = null;
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems.get(i).position == mCurItem) {
            curItem = mItems.get(i);
            break;
        }
    }
    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    if (hasFocus()) {
        View currentFocused = findFocus();
        ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
        if (ii == null || ii.position != mCurItem) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    }
                }
            }
        }
    }
}

From source file:com.peerless2012.twowaynestedscrollview.TwoWayNestedScrollView.java

/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy.//from  ww  w .j a  v a2  s  . c  om
 *
 * @param event
 *            The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    mTempRect.setEmpty();

    if (!canVerticalScroll()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this)
                currentFocused = null;
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }
    if (!canHorizontalScroll()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this)
                currentFocused = null;
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }

    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_UP:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_UP);
            } else {
                handled = fullScroll(View.FOCUS_UP);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_DOWN);
            } else {
                handled = fullScroll(View.FOCUS_DOWN);
            }
            break;
        case KeyEvent.KEYCODE_SPACE:
            pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
            break;
        }
    }

    return handled;
}

From source file:com.hippo.widget.BothScrollView.java

/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy.//from  w ww .java  2s  .  c  o  m
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    mTempRect.setEmpty();

    if (!canScrollHorizontally()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this) {
                currentFocused = null;
            }
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_RIGHT);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_RIGHT);
        }
        return false;
    }

    if (!canScrollVertically()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this) {
                currentFocused = null;
            }
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }

    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (!event.isAltPressed()) {
                handled = arrowScrollHorizontally(View.FOCUS_LEFT);
            } else {
                handled = fullScroll(View.FOCUS_LEFT);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (!event.isAltPressed()) {
                handled = arrowScrollHorizontally(View.FOCUS_RIGHT);
            } else {
                handled = fullScroll(View.FOCUS_RIGHT);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            if (!event.isAltPressed()) {
                handled = arrowScrollVertically(View.FOCUS_UP);
            } else {
                handled = fullScroll(View.FOCUS_UP);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (!event.isAltPressed()) {
                handled = arrowScrollVertically(View.FOCUS_DOWN);
            } else {
                handled = fullScroll(View.FOCUS_DOWN);
            }
            break;
        case KeyEvent.KEYCODE_SPACE:
            if (event.isCtrlPressed()) {
                pageScroll(event.isShiftPressed() ? View.FOCUS_LEFT : View.FOCUS_RIGHT);
            } else {
                pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
            }
            break;
        }
    }

    return handled;
}

From source file:org.fireking.app.view.LazyLoadViewPager.java

void populate() {
    if (mAdapter == null) {
        return;//www.  j  a  v  a  2 s  . co m
    }

    // Bail now if we are waiting to populate. This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a new position until we have finished the scroll to
    // that position, avoiding glitches from happening at that point.
    if (mPopulatePending) {
        if (DEBUG)
            Log.i(TAG, "populate is pending, skipping for now...");
        return;
    }

    // Also, don't populate until we are attached to a window. This is to
    // avoid trying to populate before we have restored our view hierarchy
    // state and conflicting with what is restored.
    if (getWindowToken() == null) {
        return;
    }

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;
    final int startPos = Math.max(0, mCurItem - pageLimit);// 0- 4
    final int N = mAdapter.getCount();
    final int endPos = Math.min(N, mCurItem + pageLimit);// 1- 5
    // final int endPos = Math.min(N - 1, mCurItem + 1);

    // Add and remove pages in the existing list.
    int lastPos = -1;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {
            mItems.remove(i);
            i--;
            mAdapter.destroyItem(this, ii.position, ii.object);
        } else if (lastPos < endPos && ii.position > startPos) {
            // The next item is outside of our range, but we have a gap
            // between it and the last item where we want to have a page
            // shown. Fill in the gap.
            lastPos++;
            if (lastPos < startPos) {
                lastPos = startPos;
            }
            while (lastPos < endPos && lastPos < ii.position) {
                if (lastPos == (endPos - 1) && endPos == mCurItem + 1) {
                    addNewItem(lastPos, i);
                }
                lastPos++;
                i++;
            }
        }
        lastPos = ii.position;
    }
    // Add any new pages we need at the end.
    lastPos = mItems.size() > 0 ? mItems.get(mItems.size() - 1).position : -1;
    if (lastPos < endPos) {
        ++lastPos;
        lastPos = lastPos > startPos ? lastPos : startPos;
        while (lastPos < endPos) {
            addNewItem(lastPos, -1);
            lastPos++;
        }
    }

    if (DEBUG) {
        Log.i(TAG, "Current page list:");
        for (int i = 0; i < mItems.size(); i++) {
            Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
        }
    }

    ItemInfo curItem = null;
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems.get(i).position == mCurItem) {
            curItem = mItems.get(i);
            break;
        }
    }
    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    if (hasFocus()) {
        View currentFocused = findFocus();
        ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
        if (ii == null || ii.position != mCurItem) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    }
                }
            }
        }
    }
}

From source file:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

/**
 * Handle scrolling in response to an up or down arrow click.
 *
 * @param direction The direction corresponding to the arrow key that was
 *                  pressed/*w  w w.  j  av  a2 s. c o  m*/
 * @return True if we consumed the event, false otherwise
 */
public boolean arrowScroll(int direction) {

    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);

    final int maxJump = getMaxScrollAmount();

    if (nextFocused != null && isWithinDeltaOfScreen(nextFocused, maxJump, getHeight())) {
        nextFocused.getDrawingRect(mTempRect);
        offsetDescendantRectToMyCoords(nextFocused, mTempRect);
        int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
        doScrollY(scrollDelta);
        nextFocused.requestFocus(direction);
    } else {
        // no new focus
        int scrollDelta = maxJump;

        if (direction == View.FOCUS_UP && getScrollY() < scrollDelta) {
            scrollDelta = getScrollY();
        } else if (direction == View.FOCUS_DOWN) {
            if (getChildCount() > 0) {
                int daBottom = getChildAt(0).getBottom();
                int screenBottom = getScrollY() + getHeight() - getPaddingBottom();
                if (daBottom - screenBottom < maxJump) {
                    scrollDelta = daBottom - screenBottom;
                }
            }
        }
        if (scrollDelta == 0) {
            return false;
        }
        doScrollY(direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta);
    }

    if (currentFocused != null && currentFocused.isFocused() && isOffScreen(currentFocused)) {
        // previously focused item still has focus and is off screen, give
        // it up (take it back to ourselves)
        // (also, need to temporarily force FOCUS_BEFORE_DESCENDANTS so we are
        // sure to
        // get it)
        final int descendantFocusability = getDescendantFocusability(); // save
        setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        requestFocus();
        setDescendantFocusability(descendantFocusability); // restore
    }
    return true;
}

From source file:beichen.douban.ui.view.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;//w w w  .j a v a 2 s .  c  o  m
    }

    // Bail now if we are waiting to populate. This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a new position until we have finished the scroll to
    // that position, avoiding glitches from happening at that point.
    if (mPopulatePending) {
        if (DEBUG)
            Log.i(TAG, "populate is pending, skipping for now...");
        return;
    }

    // Also, don't populate until we are attached to a window. This is to
    // avoid trying to populate before we have restored our view hierarchy
    // state and conflicting with what is restored.
    if (getWindowToken() == null) {
        return;
    }

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;
    final int startPos = Math.max(0, mCurItem - pageLimit);
    final int N = mAdapter.getCount();
    final int endPos = Math.min(N - 1, mCurItem + pageLimit);

    if (DEBUG)
        Log.v(TAG, "populating: startPos=" + startPos + " endPos=" + endPos);

    // Add and remove pages in the existing list.
    int lastPos = -1;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {
            if (DEBUG)
                Log.i(TAG, "removing: " + ii.position + " @ " + i);
            mItems.remove(i);
            i--;
            mAdapter.destroyItem(this, ii.position, ii.object);
        } else if (lastPos < endPos && ii.position > startPos) {
            // The next item is outside of our range, but we have a gap
            // between it and the last item where we want to have a page
            // shown. Fill in the gap.
            lastPos++;
            if (lastPos < startPos) {
                lastPos = startPos;
            }
            while (lastPos <= endPos && lastPos < ii.position) {
                if (DEBUG)
                    Log.i(TAG, "inserting: " + lastPos + " @ " + i);
                addNewItem(lastPos, i);
                lastPos++;
                i++;
            }
        }
        lastPos = ii.position;
    }

    // Add any new pages we need at the end.
    lastPos = mItems.size() > 0 ? mItems.get(mItems.size() - 1).position : -1;
    if (lastPos < endPos) {
        lastPos++;
        lastPos = lastPos > startPos ? lastPos : startPos;
        while (lastPos <= endPos) {
            if (DEBUG)
                Log.i(TAG, "appending: " + lastPos);
            addNewItem(lastPos, -1);
            lastPos++;
        }
    }

    if (DEBUG) {
        Log.i(TAG, "Current page list:");
        for (int i = 0; i < mItems.size(); i++) {
            Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
        }
    }

    ItemInfo curItem = null;
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems.get(i).position == mCurItem) {
            curItem = mItems.get(i);
            break;
        }
    }
    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    if (hasFocus()) {
        View currentFocused = findFocus();
        ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
        if (ii == null || ii.position != mCurItem) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    }
                }
            }
        }
    }
}

From source file:cn.zy.ef.widget.VerticalViewPager.java

/**
 * ??ViewPage?/*from   ww  w .  j ava 2s .  c o m*/
 */
void populate() {
    if (mAdapter == null) {
        return;
    } /* end of if */

    // Bail now if we are waiting to populate.  This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a new position until we have finished the scroll to
    // that position, avoiding glitches from happening at that point.
    if (mPopulatePending) {
        if (DEBUG)
            Log.i(TAG, "populate is pending, skipping for now...");
        return;
    } /* end of if */

    // Also, don't populate until we are attached to a window.  This is to
    // avoid trying to populate before we have restored our view hierarchy
    // state and conflicting with what is restored.
    if (getWindowToken() == null) {
        return;
    } /* end of if */

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;
    final int startPos = Math.max(0, mCurItem - pageLimit);
    final int N = mAdapter.getCount();
    final int endPos = Math.min(N - 1, mCurItem + pageLimit);

    if (DEBUG)
        Log.v(TAG, "populating: startPos=" + startPos + " endPos=" + endPos);

    // Add and remove pages in the existing list.
    int lastPos = -1;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {
            if (DEBUG)
                Log.i(TAG, "removing: " + ii.position + " @ " + i);
            mItems.remove(i);
            i--;
            mAdapter.destroyItem(this, ii.position, ii.object);
        } else if (lastPos < endPos && ii.position > startPos) {
            // The next item is outside of our range, but we have a gap
            // between it and the last item where we want to have a page
            // shown.  Fill in the gap.
            lastPos++;
            if (lastPos < startPos) {
                lastPos = startPos;
            } /* end of if */
            while (lastPos <= endPos && lastPos < ii.position) {
                if (DEBUG)
                    Log.i(TAG, "inserting: " + lastPos + " @ " + i);
                addNewItem(lastPos, i);
                lastPos++;
                i++;
            } /* end of while */
        } /* end of if */
        lastPos = ii.position;
    } /* end of if */

    // Add any new pages we need at the end.
    lastPos = mItems.size() > 0 ? mItems.get(mItems.size() - 1).position : -1;
    if (lastPos < endPos) {
        lastPos++;
        lastPos = lastPos > startPos ? lastPos : startPos;
        while (lastPos <= endPos) {
            if (DEBUG)
                Log.i(TAG, "appending: " + lastPos);
            addNewItem(lastPos, -1);
            lastPos++;
        } /* end of while */
    } /* end of if */

    if (DEBUG) {
        Log.i(TAG, "Current page list:");
        for (int i = 0; i < mItems.size(); i++) {
            Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
        } /* end of for */
    } /* end of if */

    ItemInfo curItem = null;
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems.get(i).position == mCurItem) {
            curItem = mItems.get(i);
            break;
        } /* end of if */
    } /* end of for */
    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    if (hasFocus()) {
        View currentFocused = findFocus();
        ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
        if (ii == null || ii.position != mCurItem) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    } /* end of if */
                } /* end of if */
            } /* end of for */
        } /* end of if */
    } /* end of if */
}

From source file:administrator.example.com.myscrollview.VerticalViewPager.java

/**
 * ??ViewPage?//from  ww  w.  jav  a  2s .co m
 */
void populate() {
    if (mAdapter == null) {
        return;
    } /* end of if */

    // Bail now if we are waiting to populate.  This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a new position until we have finished the scroll to
    // that position, avoiding glitches from happening at that point.
    if (mPopulatePending) {
        if (DEBUG)
            Log.i(TAG, "populate is pending, skipping for now...");
        return;
    } /* end of if */

    // Also, don't populate until we are attached to a window.  This is to
    // avoid trying to populate before we have restored our view hierarchy
    // state and conflicting with what is restored.
    if (getWindowToken() == null) {
        return;
    } /* end of if */

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;
    final int startPos = Math.max(0, mCurItem - pageLimit);
    final int N = mAdapter.getCount();
    final int endPos = Math.min(N - 1, mCurItem + pageLimit);

    if (DEBUG)
        Log.v(TAG, "populating: startPos=" + startPos + " endPos=" + endPos);

    // Add and remove pages in the existing list.
    int lastPos = -1;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {
            if (DEBUG)
                Log.i(TAG, "removing: " + ii.position + " @ " + i);
            mItems.remove(i);
            i--;
            mAdapter.destroyItem(this, ii.position, ii.object);
        } else if (lastPos < endPos && ii.position > startPos) {
            // The next item is outside of our range, but we have a gap
            // between it and the last item where we want to have a page
            // shown.  Fill in the gap.
            lastPos++;
            if (lastPos < startPos) {
                lastPos = startPos;
            } /* end of if */
            while (lastPos <= endPos && lastPos < ii.position) {
                if (DEBUG)
                    Log.i(TAG, "inserting: " + lastPos + " @ " + i);
                addNewItem(lastPos, i, null);
                lastPos++;
                i++;
            } /* end of while */
        } /* end of if */
        lastPos = ii.position;
    } /* end of if */

    // Add any new pages we need at the end.
    lastPos = mItems.size() > 0 ? mItems.get(mItems.size() - 1).position : -1;
    if (lastPos < endPos) {
        lastPos++;
        lastPos = lastPos > startPos ? lastPos : startPos;
        while (lastPos <= endPos) {
            if (DEBUG)
                Log.i(TAG, "appending: " + lastPos);
            addNewItem(lastPos, -1, null);
            lastPos++;
        } /* end of while */
    } /* end of if */

    if (DEBUG) {
        Log.i(TAG, "Current page list:");
        for (int i = 0; i < mItems.size(); i++) {
            Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
        } /* end of for */
    } /* end of if */

    ItemInfo curItem = null;
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems.get(i).position == mCurItem) {
            curItem = mItems.get(i);
            break;
        } /* end of if */
    } /* end of for */
    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    if (hasFocus()) {
        View currentFocused = findFocus();
        ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
        if (ii == null || ii.position != mCurItem) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    } /* end of if */
                } /* end of if */
            } /* end of for */
        } /* end of if */
    } /* end of if */
}