Example usage for android.view View FOCUS_DOWN

List of usage examples for android.view View FOCUS_DOWN

Introduction

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

Prototype

int FOCUS_DOWN

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

Click Source Link

Document

Use with #focusSearch(int) .

Usage

From source file:android.improving.utils.views.cardsview.OrientedViewPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;//from   w w  w  .  j  a  va 2  s.  c om
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        focusDirection = mCurItem < newCurrentItem ? View.FOCUS_DOWN : View.FOCUS_UP;
        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 extraSizeTopLeft = 0.f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
        final int clientSize = getClientSize();
        final float topLeftSizeNeeded = clientSize <= 0 ? 0
                : 2.f - curItem.sizeFactor + (float) getPaddingLeft() / (float) clientSize;
        for (int pos = mCurItem - 1; pos >= 0; pos--) {
            if (extraSizeTopLeft >= topLeftSizeNeeded && 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) {
                extraSizeTopLeft += ii.sizeFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraSizeTopLeft += ii.sizeFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            }
        }

        float extraSizeBottomRight = curItem.sizeFactor;
        itemIndex = curIndex + 1;
        if (extraSizeBottomRight < 2.f) {
            ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
            final float bottomRightSizeNeeded = clientSize <= 0 ? 0
                    : (float) getPaddingRight() / (float) clientSize + 2.f;
            for (int pos = mCurItem + 1; pos < N; pos++) {
                if (extraSizeBottomRight >= bottomRightSizeNeeded && 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) {
                    extraSizeBottomRight += ii.sizeFactor;
                    itemIndex++;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraSizeBottomRight += ii.sizeFactor;
                    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();
    if (mOrientation == Orientation.VERTICAL) {
        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.sizeFactor;
                    lp.position = ii.position;
                }
            }
        }
    } else {
        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.sizeFactor;
                    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:VerticalViewPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;// ww w  .jav  a  2  s. c  om
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        focusDirection = mCurItem < newCurrentItem ? View.FOCUS_DOWN : View.FOCUS_UP;
        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 extraHeightLeft = 0f;
        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 ((extraHeightLeft >= 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) {
                extraHeightLeft += ii.heightFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraHeightLeft += 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:ti.modules.titanium.ui.widget.TiUIScrollView.java

public void scrollToBottom() {
    View view = this.scrollView;
    if (view instanceof TiHorizontalScrollView) {
        ((TiHorizontalScrollView) view).fullScroll(View.FOCUS_RIGHT);
    } else if (view instanceof TiVerticalScrollView) {
        ((TiVerticalScrollView) view).fullScroll(View.FOCUS_DOWN);
    }/*from  www  . j a va 2 s.  c  om*/
}

From source file:eu.kanade.tachiyomi.ui.reader.viewer.pager.vertical.VerticalViewPagerImpl.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;/*from w  ww  . j  av  a2 s  .com*/
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        focusDirection = mCurItem < newCurrentItem ? View.FOCUS_DOWN : View.FOCUS_UP;
        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;
    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) getPaddingLeft() / (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) getPaddingRight() / (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 && child.requestFocus(focusDirection)) {
                    break;
                }
            }
        }
    }
}

From source file:cn.androidy.materialdesignsample.ryanharterviewpager.ViewPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;/*  w w  w . ja  v  a 2s  .  co  m*/
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        if (isOrientationHorizontal()) {
            focusDirection = mCurItem < newCurrentItem ? View.FOCUS_RIGHT : View.FOCUS_LEFT;
        } else {
            focusDirection = mCurItem < newCurrentItem ? View.FOCUS_DOWN : View.FOCUS_UP;
        }
        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 extraSizeStart = 0.f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
        final int paddingStart = isOrientationHorizontal() ? getPaddingLeft() : getPaddingTop();
        final int clientSize = isOrientationHorizontal() ? getClientWidth() : getClientHeight();
        final float startSizeNeeded = clientSize <= 0 ? 0
                : 2.f - curItem.sizeFactor + (float) paddingStart / (float) clientSize;
        for (int pos = mCurItem - 1; pos >= 0; pos--) {
            if (extraSizeStart >= startSizeNeeded && 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) {
                extraSizeStart += ii.sizeFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraSizeStart += ii.sizeFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            }
        }

        float extraSizeEnd = curItem.sizeFactor;
        itemIndex = curIndex + 1;
        if (extraSizeEnd < 2.f) {
            ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
            final int paddingEnd = isOrientationHorizontal() ? getPaddingRight() : getPaddingBottom();
            final float endSizeNeeded = clientSize <= 0 ? 0 : (float) paddingEnd / (float) clientSize + 2.f;
            for (int pos = mCurItem + 1; pos < N; pos++) {
                if (extraSizeEnd >= endSizeNeeded && 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) {
                    extraSizeEnd += ii.sizeFactor;
                    itemIndex++;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraSizeEnd += ii.sizeFactor;
                    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 size 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.sizeFactor == 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.sizeFactor = ii.sizeFactor;
                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:space.qssq.viewpager.ViewPagerX.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;/*ww w.  j  a va 2s  . c  o  m*/
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        if (isOrientationHorizontal()) {
            focusDirection = mCurItem < newCurrentItem ? View.FOCUS_RIGHT : View.FOCUS_LEFT;
        } else {
            focusDirection = mCurItem < newCurrentItem ? View.FOCUS_DOWN : View.FOCUS_UP;
        }
        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 PagerAdapterX changed the adapter's"
                + " contents without calling PagerAdapterX#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 extraSizeStart = 0.f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
        final int paddingStart = isOrientationHorizontal() ? getPaddingLeft() : getPaddingTop();
        final int clientSize = isOrientationHorizontal() ? getClientWidth() : getClientHeight();
        final float startSizeNeeded = clientSize <= 0 ? 0
                : 2.f - curItem.sizeFactor + (float) paddingStart / (float) clientSize;
        for (int pos = mCurItem - 1; pos >= 0; pos--) {
            if (extraSizeStart >= startSizeNeeded && 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) {
                extraSizeStart += ii.sizeFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraSizeStart += ii.sizeFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            }
        }

        float extraSizeEnd = curItem.sizeFactor;
        itemIndex = curIndex + 1;
        if (extraSizeEnd < 2.f) {
            ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
            final int paddingEnd = isOrientationHorizontal() ? getPaddingRight() : getPaddingBottom();
            final float endSizeNeeded = clientSize <= 0 ? 0 : (float) paddingEnd / (float) clientSize + 2.f;
            for (int pos = mCurItem + 1; pos < N; pos++) {
                if (extraSizeEnd >= endSizeNeeded && 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) {
                    extraSizeEnd += ii.sizeFactor;
                    itemIndex++;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraSizeEnd += ii.sizeFactor;
                    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 size 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.sizeFactor == 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.sizeFactor = ii.sizeFactor;
                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:android.support.v17.leanback.app.DetailsSupportFragment.java

/**
 * This method does the following//from   ww  w . j av a  2 s. c  o m
 * <ul>
 * <li>sets up focus search handling logic in the root view to enable transitioning between
 * half screen/full screen/no video mode.</li>
 *
 * <li>Sets up the key listener in the root view to intercept events like UP/DOWN and
 * transition to appropriate mode like half/full screen video.</li>
 * </ul>
 */
void setupDpadNavigation() {
    mRootView.setOnChildFocusListener(new BrowseFrameLayout.OnChildFocusListener() {

        @Override
        public boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
            return false;
        }

        @Override
        public void onRequestChildFocus(View child, View focused) {
            if (child != mRootView.getFocusedChild()) {
                if (child.getId() == R.id.details_fragment_root) {
                    if (!mPendingFocusOnVideo) {
                        slideInGridView();
                        showTitle(true);
                    }
                } else if (child.getId() == R.id.video_surface_container) {
                    slideOutGridView();
                    showTitle(false);
                } else {
                    showTitle(true);
                }
            }
        }
    });
    mRootView.setOnFocusSearchListener(new BrowseFrameLayout.OnFocusSearchListener() {
        @Override
        public View onFocusSearch(View focused, int direction) {
            if (mRowsSupportFragment.getVerticalGridView() != null
                    && mRowsSupportFragment.getVerticalGridView().hasFocus()) {
                if (direction == View.FOCUS_UP) {
                    if (mDetailsBackgroundController != null
                            && mDetailsBackgroundController.canNavigateToVideoSupportFragment()
                            && mVideoSupportFragment != null && mVideoSupportFragment.getView() != null) {
                        return mVideoSupportFragment.getView();
                    } else if (getTitleView() != null && getTitleView().hasFocusable()) {
                        return getTitleView();
                    }
                }
            } else if (getTitleView() != null && getTitleView().hasFocus()) {
                if (direction == View.FOCUS_DOWN) {
                    if (mRowsSupportFragment.getVerticalGridView() != null) {
                        return mRowsSupportFragment.getVerticalGridView();
                    }
                }
            }
            return focused;
        }
    });

    // If we press BACK on remote while in full screen video mode, we should
    // transition back to half screen video playback mode.
    mRootView.setOnDispatchKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // This is used to check if we are in full screen video mode. This is somewhat
            // hacky and relies on the behavior of the video helper class to update the
            // focusability of the video surface view.
            if (mVideoSupportFragment != null && mVideoSupportFragment.getView() != null
                    && mVideoSupportFragment.getView().hasFocus()) {
                if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) {
                    if (getVerticalGridView().getChildCount() > 0) {
                        getVerticalGridView().requestFocus();
                        return true;
                    }
                }
            }

            return false;
        }
    });
}

From source file:dev.dworks.libs.widget.ViewPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;//from  w  w w  .java  2  s .  c  o m
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        focusDirection = mCurItem < newCurrentItem
                ? mOrientation == HORIZONTAL ? View.FOCUS_RIGHT : View.FOCUS_UP
                : mOrientation == HORIZONTAL ? View.FOCUS_LEFT : View.FOCUS_DOWN;
        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 extraFactorStart = 0.f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
        final int clientFactor = mOrientation == HORIZONTAL ? getClientWidth() : getClientHeight();
        final float startFactorNeeded = clientFactor <= 0 ? 0
                : 2.f - (mOrientation == HORIZONTAL ? curItem.widthFactor : curItem.heightFactor)
                        + (float) (mOrientation == HORIZONTAL ? getPaddingLeft() : getPaddingTop())
                                / (float) clientFactor;
        for (int pos = mCurItem - 1; pos >= 0; pos--) {
            if (extraFactorStart >= startFactorNeeded && 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) {
                extraFactorStart += mOrientation == HORIZONTAL ? ii.widthFactor : ii.heightFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraFactorStart += mOrientation == HORIZONTAL ? ii.widthFactor : ii.heightFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            }
        }

        float extraFactorEnd = mOrientation == HORIZONTAL ? curItem.widthFactor : curItem.heightFactor;
        itemIndex = curIndex + 1;
        if (extraFactorEnd < 2.f) {
            ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
            final float endFactorNeeded = clientFactor <= 0 ? 0
                    : (float) (mOrientation == HORIZONTAL ? getPaddingRight() : getPaddingBottom())
                            / (float) clientFactor + 2.f;
            for (int pos = mCurItem + 1; pos < N; pos++) {
                if (extraFactorEnd >= endFactorNeeded && 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) {
                    extraFactorEnd += mOrientation == HORIZONTAL ? ii.widthFactor : ii.heightFactor;
                    itemIndex++;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraFactorEnd += mOrientation == HORIZONTAL ? ii.widthFactor : 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 (mOrientation == HORIZONTAL) {
            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;
                }
            }
        } else {
            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.guide.ViewPager.java

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;/*from   ww  w.  j a va  2  s  . co  m*/
    int focusDirection = View.FOCUS_FORWARD;
    if (mCurItem != newCurrentItem) {
        if (isOrientationHorizontal()) {
            focusDirection = mCurItem < newCurrentItem ? View.FOCUS_RIGHT : View.FOCUS_LEFT;
        } else {
            focusDirection = mCurItem < newCurrentItem ? View.FOCUS_DOWN : View.FOCUS_UP;
        }
        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 extraSizeStart = 0.f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
        final int paddingStart = isOrientationHorizontal() ? getPaddingLeft() : getPaddingTop();
        final int clientSize = isOrientationHorizontal() ? getClientWidth() : getClientHeight();
        final float startSizeNeeded = clientSize <= 0 ? 0
                : 2.f - curItem.sizeFactor + (float) paddingStart / (float) clientSize;
        for (int pos = mCurItem - 1; pos >= 0; pos--) {
            if (extraSizeStart >= startSizeNeeded && 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) {
                extraSizeStart += ii.sizeFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraSizeStart += ii.sizeFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            }
        }

        float extraSizeEnd = curItem.sizeFactor;
        itemIndex = curIndex + 1;
        if (extraSizeEnd < 2.f) {
            ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
            final int paddingEnd = isOrientationHorizontal() ? getPaddingRight() : getPaddingBottom();
            final float endSizeNeeded = clientSize <= 0 ? 0 : (float) paddingEnd / (float) clientSize + 2.f;
            for (int pos = mCurItem + 1; pos < N; pos++) {
                if (extraSizeEnd >= endSizeNeeded && 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) {
                    extraSizeEnd += ii.sizeFactor;
                    itemIndex++;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraSizeEnd += ii.sizeFactor;
                    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 size 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.sizeFactor == 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.sizeFactor = ii.sizeFactor;
                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.intel.xdk.device.Device.java

public void showRemoteSite(final String strURL, final int closeX_pt, final int closeY_pt, final int closeX_ls,
        final int closeY_ls, final int closeW, final int closeH, final String closeImage) {
    if (strURL == null || strURL.length() == 0)
        return;//from   ww w . ja  v a2  s.c  o m

    remoteCloseXPort = closeX_pt;
    remoteCloseYPort = closeY_pt;
    remoteCloseXLand = closeX_ls;
    remoteCloseYLand = closeY_ls;

    //hack to adjust image size
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

    remoteCloseW = (int) ((float) closeW * dm.density);
    remoteCloseH = (int) ((float) closeH * dm.density);

    //Set position, width, height of closeImage according to currentOrientation
    if (this.getOrientation() == 0 || this.getOrientation() == 180) {
        //Portrait
        AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
                remoteCloseW == 0 ? 48 : remoteCloseW, remoteCloseH == 0 ? 48 : remoteCloseH, remoteCloseXPort,
                remoteCloseYPort);
        remoteClose.setLayoutParams(params);
    } else {
        AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
                remoteCloseW == 0 ? 48 : remoteCloseW, remoteCloseH == 0 ? 48 : remoteCloseH, remoteCloseXLand,
                remoteCloseYLand);
        remoteClose.setLayoutParams(params);
    }

    activity.runOnUiThread(new Runnable() {

        public void run() {

            if (remoteView == null) {

                remoteView = new WebView(activity);
                remoteView.setInitialScale(0);
                remoteView.setVerticalScrollBarEnabled(false);
                remoteView.setWebViewClient(new WebViewClient());
                final WebSettings settings = remoteView.getSettings();
                settings.setJavaScriptEnabled(true);
                settings.setJavaScriptCanOpenWindowsAutomatically(true);
                settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);

                remoteLayout.addView(remoteView,
                        new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER));

                remoteView.requestFocusFromTouch();

                remoteView.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if (event.getAction() != KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
                            remoteClose.performClick();
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
            }
            // load the url
            remoteView.loadUrl(strURL);
            // show the view
            remoteLayout.setVisibility(View.VISIBLE);
            // set the flag
            isShowingRemoteSite = true;
            // isShowingRemoteSite = true;
            // get focus
            remoteView.requestFocus(View.FOCUS_DOWN);
            remoteClose.bringToFront();
        }
    });

}