Example usage for android.view View FOCUS_LEFT

List of usage examples for android.view View FOCUS_LEFT

Introduction

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

Prototype

int FOCUS_LEFT

To view the source code for android.view View FOCUS_LEFT.

Click Source Link

Document

Use with #focusSearch(int) .

Usage

From source file:com.missionhub.ui.widget.LockableViewPager.java

@Override
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;/* w  ww.  j a va  2s. c o  m*/

    boolean locked = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            if (currentFocused != null && nextFocused.getLeft() >= currentFocused.getLeft()) {
                if (mLock == LOCK_BOTH || mLock == LOCK_BACKWARD) {
                    locked = true;
                }
            }
        } else if (direction == View.FOCUS_RIGHT) {
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                if (mLock == LOCK_BOTH || mLock == LOCK_FORWARD) {
                    locked = true;
                }
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        if (mLock == LOCK_BOTH || mLock == LOCK_BACKWARD) {
            locked = true;
        }
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        if (mLock == LOCK_BOTH || mLock == LOCK_FORWARD) {
            locked = true;
        }
    }

    return locked || super.arrowScroll(direction);
}

From source file:com.vuze.android.remote.AndroidUtilsUI.java

public static boolean handleCommonKeyDownEvents(Activity a, int keyCode, KeyEvent event) {
    if (event.getAction() != KeyEvent.ACTION_DOWN) {
        return false;
    }/*from  w w  w .j a  va 2s.  co  m*/
    switch (keyCode) {
    case KeyEvent.KEYCODE_MEDIA_NEXT:
    case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
        ViewGroup vg = (ViewGroup) a.findViewById(android.R.id.content);
        ArrayList list = AndroidUtilsUI.findByClass(vg, ViewPager.class, new ArrayList<View>(0));

        if (list.size() > 0) {
            ViewPager viewPager = (ViewPager) list.get(0);
            viewPager.arrowScroll(View.FOCUS_RIGHT);
        }
        break;
    }

    case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
    case KeyEvent.KEYCODE_MEDIA_REWIND: {
        ViewGroup vg = (ViewGroup) a.findViewById(android.R.id.content);
        ArrayList list = AndroidUtilsUI.findByClass(vg, ViewPager.class, new ArrayList<View>(0));

        if (list.size() > 0) {
            ViewPager viewPager = (ViewPager) list.get(0);
            viewPager.arrowScroll(View.FOCUS_LEFT);
        }
        break;
    }

    case KeyEvent.KEYCODE_DPAD_LEFT: {
        if (a instanceof DrawerActivity) {
            DrawerActivity da = (DrawerActivity) a;
            DrawerLayout drawerLayout = da.getDrawerLayout();
            View viewFocus = a.getCurrentFocus();
            boolean canOpenDrawer = viewFocus != null && "leftmost".equals(viewFocus.getTag());
            if (canOpenDrawer) {
                drawerLayout.openDrawer(Gravity.LEFT);
                drawerLayout.requestFocus();
                return true;
            }
        }
        break;
    }

    }

    return false;
}

From source file:com.numix.calculator.EventListener.java

@Override
public boolean onLongClick(View view) {
    switch (view.getId()) {
    case R.id.del:
        mHandler.onClear();//from  ww  w. j ava  2 s  .  c  o m
        return true;

    case R.id.next:
        // Handle back
        EditText active = mHandler.mDisplay.getActiveEditText();
        if (active.getSelectionStart() == 0) {
            View v = mHandler.mDisplay.getActiveEditText().focusSearch(View.FOCUS_LEFT);
            if (v != null)
                v.requestFocus();
            active = mHandler.mDisplay.getActiveEditText();
            active.setSelection(active.getText().length());
        } else {
            active.setSelection(active.getSelectionStart() - 1);
        }
        return true;
    }
    if (view.getTag() != null) {
        String text = (String) view.getTag();
        if (!text.isEmpty()) {
            Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
            return true;
        }
    }
    if (view instanceof TextView && ((TextView) view).getHint() != null) {
        String text = ((TextView) view).getHint().toString();
        if (text.length() >= 2) {
            // Add paren after sin, cos, ln, etc. from buttons
            text += "(";
        }
        mHandler.insert(text);
        returnToBasic();
        return true;
    }
    return false;
}

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  w  w . ja v a2  s .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:com.oguzbabaoglu.cardpager.CardPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;//  w w  w.ja v a  2 s  .c  o m
    int focusDirection = View.FOCUS_FORWARD;
    if (currentItem != newCurrentItem) {
        focusDirection = currentItem < newCurrentItem ? View.FOCUS_RIGHT : View.FOCUS_LEFT;
        oldCurInfo = infoForPosition(currentItem);
        currentItem = newCurrentItem;
    }

    if (pagerAdapter == null) {
        sortChildDrawingOrder();
        return;
    }

    // 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 (populatePending) {
        sortChildDrawingOrder();
        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;
    }

    pagerAdapter.startUpdate(this);

    final int pageLimit = offscreenPageLimit;
    final int startPos = Math.max(0, currentItem - pageLimit);
    final int adapterCount = pagerAdapter.getCount();
    final int endPos = Math.min(adapterCount - 1, currentItem + pageLimit);

    // Locate the currently focused item or add it if needed.
    int curIndex;
    ItemInfo curItem = null;
    for (curIndex = 0; curIndex < items.size(); curIndex++) {
        final ItemInfo ii = items.get(curIndex);
        if (ii.position == currentItem) {
            curItem = ii;
            break;
        }
    }

    if (curItem == null && adapterCount > 0) {
        curItem = addNewItem(currentItem, curIndex);
    }

    // Fill 3x the available width or up to the number of offscreen
    // pages requested to either side, whichever is larger.
    // If we have no current item we have no work to do.
    if (curItem != null) {
        float extraWidthLeft = 0.f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? items.get(itemIndex) : null;
        final int clientWidth = getClientWidth();
        final float leftWidthNeeded = clientWidth <= 0 ? 0
                : 2.f - curItem.widthFactor + (float) getPaddingLeft() / (float) clientWidth;
        for (int pos = currentItem - 1; pos >= 0; pos--) {
            if (extraWidthLeft >= leftWidthNeeded && pos < startPos) {
                if (ii == null) {
                    break;
                }
                if (pos == ii.position && !ii.scrolling) {
                    items.remove(itemIndex);
                    pagerAdapter.destroyItem(this, pos, ii.object);

                    itemIndex--;
                    curIndex--;
                    ii = itemIndex >= 0 ? items.get(itemIndex) : null;
                }
            } else if (ii != null && pos == ii.position) {
                extraWidthLeft += ii.widthFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? items.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraWidthLeft += ii.widthFactor;
                curIndex++;
                ii = itemIndex >= 0 ? items.get(itemIndex) : null;
            }
        }

        float extraWidthRight = curItem.widthFactor;
        itemIndex = curIndex + 1;
        if (extraWidthRight < 2.f) {
            ii = itemIndex < items.size() ? items.get(itemIndex) : null;
            final float rightWidthNeeded = clientWidth <= 0 ? 0
                    : (float) getPaddingRight() / (float) clientWidth + 2.f;
            for (int pos = currentItem + 1; pos < adapterCount; pos++) {
                if (extraWidthRight >= rightWidthNeeded && pos > endPos) {
                    if (ii == null) {
                        break;
                    }
                    if (pos == ii.position && !ii.scrolling) {
                        items.remove(itemIndex);
                        pagerAdapter.destroyItem(this, pos, ii.object);

                        ii = itemIndex < items.size() ? items.get(itemIndex) : null;
                    }
                } else if (ii != null && pos == ii.position) {
                    extraWidthRight += ii.widthFactor;
                    itemIndex++;
                    ii = itemIndex < items.size() ? items.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraWidthRight += ii.widthFactor;
                    ii = itemIndex < items.size() ? items.get(itemIndex) : null;
                }
            }
        }

        calculatePageOffsets(curItem, curIndex, oldCurInfo);
    }

    pagerAdapter.setPrimaryItem(this, currentItem, curItem != null ? curItem.object : null);

    pagerAdapter.finishUpdate(this);

    // Check width measurement of current pages and drawing sort order.
    // Update LayoutParams as needed.
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        lp.childIndex = i;
        if (lp.widthFactor == 0.f) {
            // 0 means requery the adapter for this, it doesn't have a valid width.
            final ItemInfo ii = infoForChild(child);
            if (ii != null) {
                lp.widthFactor = ii.widthFactor;
                lp.position = ii.position;
            }
        }
    }

    sortChildDrawingOrder();
    checkFocus(focusDirection);
}

From source file:com.aviary.android.feather.sdk.widget.AviaryWorkspace.java

@Override
public boolean dispatchUnhandledMove(View focused, int direction) {

    if (direction == View.FOCUS_LEFT) {
        if (getCurrentScreen() > 0) {
            snapToScreen(getCurrentScreen() - 1);
            return true;
        }/* w  w  w.  j a v a  2s . c  o m*/
    } else if (direction == View.FOCUS_RIGHT) {
        if (getCurrentScreen() < mItemCount - 1) {
            snapToScreen(getCurrentScreen() + 1);
            return true;
        }
    }
    return super.dispatchUnhandledMove(focused, direction);
}

From source file:com.aviary.android.feather.sdk.widget.AviaryWorkspace.java

@Override
public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {

    if (isEnabled()) {
        View child = getChildAt(mCurrentScreen);
        if (null != child) {
            child.addFocusables(views, direction);
        }// ww w  .  j av  a 2s .  co  m

        if (direction == View.FOCUS_LEFT) {
            if (mCurrentScreen > 0) {
                child = getChildAt(mCurrentScreen - 1);
                if (null != child) {
                    child.addFocusables(views, direction);
                }
            }
        } else if (direction == View.FOCUS_RIGHT) {
            if (mCurrentScreen < mItemCount - 1) {
                child = getChildAt(mCurrentScreen + 1);
                if (null != child) {
                    child.addFocusables(views, direction);
                }
            }
        }
    }
}

From source file:com.dian.diabetes.widget.VerticalViewPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;/* w ww. j  av a 2 s. c  o m*/
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        focusDirection = mCurItem < newCurrentItem ? View.FOCUS_RIGHT : View.FOCUS_LEFT;
        oldCurInfo = infoForPosition(mCurItem);
        mCurItem = newCurrentItem;
    }

    if (mAdapter == null) {
        sortChildDrawingOrder();
        return;
    }

    // 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...");
        sortChildDrawingOrder();
        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 (N != mExpectedAdapterCount) {
        String resName;
        try {
            resName = getResources().getResourceName(getId());
        } catch (Resources.NotFoundException e) {
            resName = Integer.toHexString(getId());
        }
        throw new IllegalStateException("The application's PagerAdapter changed the adapter's"
                + " contents without calling PagerAdapter#notifyDataSetChanged!"
                + " Expected adapter item count: " + mExpectedAdapterCount + ", found: " + N + " Pager id: "
                + resName + " Pager class: " + getClass() + " Problematic adapter: " + mAdapter.getClass());
    }

    // Locate the currently focused item or add it if needed.
    int curIndex = -1;
    ItemInfo curItem = null;
    for (curIndex = 0; curIndex < mItems.size(); curIndex++) {
        final ItemInfo ii = mItems.get(curIndex);
        if (ii.position >= mCurItem) {
            if (ii.position == mCurItem)
                curItem = ii;
            break;
        }
    }

    if (curItem == null && N > 0) {
        curItem = addNewItem(mCurItem, curIndex);
    }

    // Fill 3x the available width or up to the number of offscreen
    // pages requested to either side, whichever is larger.
    // If we have no current item we have no work to do.
    if (curItem != null) {
        float extraHeightTop = 0.f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
        final int clientHeight = getClientHeight();
        final float topHeightNeeded = clientHeight <= 0 ? 0
                : 2.f - curItem.heightFactor + (float) getPaddingTop() / (float) clientHeight;

        for (int pos = mCurItem - 1; pos >= 0; pos--) {
            if (extraHeightTop >= topHeightNeeded && pos < startPos) {
                if (ii == null) {
                    break;
                }
                if (pos == ii.position && !ii.scrolling) {
                    mItems.remove(itemIndex);
                    mAdapter.destroyItem(this, pos, ii.object);
                    if (DEBUG) {
                        Log.i(TAG,
                                "populate() - destroyItem() with pos: " + pos + " view: " + ((View) ii.object));
                    }
                    itemIndex--;
                    curIndex--;
                    ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
                }
            } else if (ii != null && pos == ii.position) {
                extraHeightTop += ii.heightFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraHeightTop += ii.heightFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            }
        }

        float extraHeightBottom = curItem.heightFactor;
        itemIndex = curIndex + 1;
        if (extraHeightBottom < 2.f) {
            ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
            final float bottomHeightNeeded = clientHeight <= 0 ? 0
                    : (float) getPaddingBottom() / (float) clientHeight + 2.f;
            for (int pos = mCurItem + 1; pos < N; pos++) {
                if (extraHeightBottom >= bottomHeightNeeded && pos > endPos) {
                    if (ii == null) {
                        break;
                    }
                    if (pos == ii.position && !ii.scrolling) {
                        mItems.remove(itemIndex);
                        mAdapter.destroyItem(this, pos, ii.object);
                        if (DEBUG) {
                            Log.i(TAG, "populate() - destroyItem() with pos: " + pos + " view: "
                                    + ((View) ii.object));
                        }
                        ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                    }
                } else if (ii != null && pos == ii.position) {
                    extraHeightBottom += ii.heightFactor;
                    itemIndex++;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraHeightBottom += ii.heightFactor;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                }
            }
        }

        calculatePageOffsets(curItem, curIndex, oldCurInfo);
    }

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

    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    // Check width measurement of current pages and drawing sort order.
    // Update LayoutParams as needed.
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        lp.childIndex = i;
        if (!lp.isDecor && lp.heightFactor == 0.f) {
            // 0 means requery the adapter for this, it doesn't have a valid width.
            final ItemInfo ii = infoForChild(child);
            if (ii != null) {
                lp.heightFactor = ii.heightFactor;
                lp.position = ii.position;
            }
        }
    }
    sortChildDrawingOrder();

    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(focusDirection)) {
                        break;
                    }
                }
            }
        }
    }
}

From source file:com.tunebrains.views.InfiniteViewPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;/*w w w.  j av a 2  s  .  c  o  m*/
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        focusDirection = mCurItem < newCurrentItem ? View.FOCUS_RIGHT : View.FOCUS_LEFT;
        oldCurInfo = infoForPosition(mCurItem);
        mCurItem = newCurrentItem;
    }

    if (mAdapter == null) {
        sortChildDrawingOrder();
        return;
    }

    // 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...");
        sortChildDrawingOrder();
        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 = mCurItem - pageLimit;
    final int N = mAdapter.getCount();
    final int endPos = mCurItem + pageLimit;

    if (N != mExpectedAdapterCount) {
        String resName;
        try {
            resName = getResources().getResourceName(getId());
        } catch (Resources.NotFoundException e) {
            resName = Integer.toHexString(getId());
        }
        throw new IllegalStateException("The application's PagerAdapter changed the adapter's"
                + " contents without calling PagerAdapter#notifyDataSetChanged!"
                + " Expected adapter item count: " + mExpectedAdapterCount + ", found: " + N + " Pager id: "
                + resName + " Pager class: " + getClass() + " Problematic adapter: " + mAdapter.getClass());
    }

    // Locate the currently focused item or add it if needed.
    int curIndex = -1;
    ItemInfo curItem = null;
    for (curIndex = 0; curIndex < mItems.size(); curIndex++) {
        final ItemInfo ii = mItems.get(curIndex);
        if (ii.position >= mCurItem) {
            if (ii.position == mCurItem)
                curItem = ii;
            break;
        }
    }

    if (curItem == null && N > 0) {
        curItem = addNewItem(mCurItem, curIndex);
    }

    // Fill 3x the available width or up to the number of offscreen
    // pages requested to either side, whichever is larger.
    // If we have no current item we have no work to do.
    if (curItem != null) {
        float extraWidthLeft = 0.f;
        int itemIndex = curIndex - 1;
        int realIntex = getRealItemIndex(itemIndex);
        ItemInfo ii = realIntex >= 0 ? mItems.get(realIntex) : null;
        final int clientWidth = getClientWidth();
        final float leftWidthNeeded = clientWidth <= 0 ? 0
                : 2.f - curItem.widthFactor + (float) getPaddingLeft() / (float) clientWidth;
        for (int pos = mCurItem - 1; pos >= mCurItem - pageLimit; pos--) {
            if (extraWidthLeft >= leftWidthNeeded && pos < startPos) {
                if (ii == null) {
                    break;
                }
                if (pos == ii.position && !ii.scrolling) {
                    mItems.remove(realIntex);
                    mAdapter.destroyItem(this, pos, ii.object);
                    if (DEBUG) {
                        Log.i(TAG,
                                "populate() - destroyItem() with pos: " + pos + " view: " + ((View) ii.object));
                    }
                    itemIndex--;
                    curIndex--;
                    ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
                }
            } else if (ii != null && pos == ii.position) {
                extraWidthLeft += ii.widthFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(realIntex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraWidthLeft += ii.widthFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(realIntex) : null;
            }
        }

        float extraWidthRight = curItem.widthFactor;
        itemIndex = curIndex + 1;
        if (extraWidthRight < 2.f) {
            ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
            final float rightWidthNeeded = clientWidth <= 0 ? 0
                    : (float) getPaddingRight() / (float) clientWidth + 2.f;
            for (int pos = mCurItem + 1; pos < mCurItem + 1 + pageLimit; pos++) {
                if (extraWidthRight >= rightWidthNeeded && pos > endPos) {
                    if (ii == null) {
                        break;
                    }
                    if (pos == ii.position && !ii.scrolling) {
                        mItems.remove(itemIndex);
                        mAdapter.destroyItem(this, pos, ii.object);
                        if (DEBUG) {
                            Log.i(TAG, "populate() - destroyItem() with pos: " + pos + " view: "
                                    + ((View) ii.object));
                        }
                        ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                    }
                } else if (ii != null && pos == ii.position) {
                    extraWidthRight += ii.widthFactor;
                    itemIndex++;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraWidthRight += ii.widthFactor;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                }
            }
        }

        calculatePageOffsets(curItem, curIndex, oldCurInfo);
    }

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

    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);

    mAdapter.finishUpdate(this);

    // Check width measurement of current pages and drawing sort order.
    // Update LayoutParams as needed.
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        lp.childIndex = i;
        if (!lp.isDecor && lp.widthFactor == 0.f) {
            // 0 means requery the adapter for this, it doesn't have a valid width.
            final ItemInfo ii = infoForChild(child);
            if (ii != null) {
                lp.widthFactor = ii.widthFactor;
                lp.position = ii.position;
            }
        }
    }
    sortChildDrawingOrder();

    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(focusDirection)) {
                        break;
                    }
                }
            }
        }
    }
}

From source file:com.android.launcher2.PagedView.java

@Override
public boolean dispatchUnhandledMove(View focused, int direction) {
    if (direction == View.FOCUS_LEFT) {
        if (getCurrentPage() > 0) {
            snapToPage(getCurrentPage() - 1);
            return true;
        }/*from  www  .  ja  v  a 2  s  . com*/
    } else if (direction == View.FOCUS_RIGHT) {
        if (getCurrentPage() < getPageCount() - 1) {
            snapToPage(getCurrentPage() + 1);
            return true;
        }
    }
    return super.dispatchUnhandledMove(focused, direction);
}