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.cnpeng.cnpeng_mydemosfrom2016_12.a_12_GetLocalFiles_VP_FM.CustomNoPreLoadViewPager.java

void populate() {
    if (mAdapter == null) {
        return;/*from   w ww .  j av 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 || 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:me.jinkun.common.viewpager.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;//from  ww w  .ja  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);

    //pageLimit?viewpager??1
    final int pageLimit = mOffscreenPageLimit;

    //startPosviewpager??
    //mCurItem?viewpager?

    final int startPos = Math.max(0, mCurItem - pageLimit);

    //Nviewpager?
    final int N = mAdapter.getCount();

    //endPosviewpager??
    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:com.vincestyling.traversaless.ViewPager.java

void populate() {
    if (mDisableTraversal || mAdapter == null || mAdapter.getCount() == 0)
        return;/*from  w w  w .  ja 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 destroy pages in the existing list.
    for (int pos = 0; pos < mItems.size(); pos++) {
        if (pos < startPos || pos > endPos) {
            ItemInfo ii = mItems.get(pos);
            if (ii != null) {
                if (DEBUG)
                    Log.i(TAG, "removing: @ " + pos);
                mAdapter.destroyItem(this, pos, ii.object);
                mItems.set(pos, null);
            }
        } else {
            if (DEBUG)
                Log.i(TAG, "determinating position: @ " + pos);
            ensureItem(pos);
        }
    }

    if (DEBUG) {
        Log.i(TAG, "Current page list:");
        for (ItemInfo mItem : mItems) {
            if (mItem != null)
                Log.i(TAG, "page " + mItem.position);
        }
    }

    ItemInfo curItem = mItems.get(mCurItem);
    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.hxqc.mall.thirdshop.views.CustomScrollView.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  w  w . j  a  v a2s . co  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 (!canScroll()) {
        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;
        default:
            break;
        }
    }

    return handled;
}

From source file:com.wevalue.view.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;/*from w w w  . j ava2 s.  com*/
    }

    // 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 news 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 news 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.yu.common.widget.ViewPagerLazy.java

void populate() {
    if (mAdapter == null) {
        return;/*from   w  w  w . ja 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:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.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  w w  . ja 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 (!canScroll()) {
        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.chenglong.muscle.ui.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;/*from ww w. 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);
    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:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

/**
 * <p>Scrolls the view to make the area defined by <code>top</code> and
 * <code>bottom</code> visible. This method attempts to give the focus
 * to a component visible in this area. If no component can be focused in
 * the new visible area, the focus is reclaimed by this ScrollView.</p>
 *
 * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
 *                  to go upward, {@link android.view.View#FOCUS_DOWN} to downward
 * @param top       the top offset of the new area to be made visible
 * @param bottom    the bottom offset of the new area to be made visible
 * @return true if the key event is consumed by this method, false otherwise
 */// w w  w  . j a v  a 2 s  . c om
private boolean scrollAndFocus(int direction, int top, int bottom) {
    boolean handled = true;

    int height = getHeight();
    int containerTop = getScrollY();
    int containerBottom = containerTop + height;
    boolean up = direction == View.FOCUS_UP;

    View newFocused = findFocusableViewInBounds(up, top, bottom);
    if (newFocused == null) {
        newFocused = this;
    }

    if (top >= containerTop && bottom <= containerBottom) {
        handled = false;
    } else {
        int delta = up ? (top - containerTop) : (bottom - containerBottom);
        doScrollY(delta);
    }

    if (newFocused != findFocus())
        newFocused.requestFocus(direction);

    return handled;
}

From source file:com.hiapk.firewall.viewpager.ViewPager.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);

    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;
                    }
                }
            }
        }
    }
}