Example usage for android.view View requestFocus

List of usage examples for android.view View requestFocus

Introduction

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

Prototype

public final boolean requestFocus(int direction) 

Source Link

Document

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

Usage

From source file:jp.mixi.android.sdk.MixiDialog.java

private void hideLoading() {
    View progress = findViewById(R.id.progress);
    progress.setVisibility(View.GONE);
    progress.startAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_out));

    View webView = findViewById(R.id.webview);
    webView.setVisibility(View.VISIBLE);
    webView.requestFocus(View.FOCUS_DOWN);

    webView.startAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in));
}

From source file:com.aincc.libtest.activity.flip.FlipViewGroup.java

/**
 *      ? .//from   ww  w  . j a  v a2s. co  m
 * 
 * @since 1.0.0
 */
private void populate() {
    if (adapter == null) {
        return;
    }
    if (populatePending) {
        Logger.i("FlipViewGroup::populate is pending, skipping for now...");
        return;
    }
    if (getWindowToken() == null) {
        return;
    }

    // 
    adapter.startUpdate(this);

    final int startPos = Math.max(0, currentItem - 2);
    final int N = adapter.getCount();
    final int endPos = Math.min(N - 1, currentItem + 2);

    Logger.v("FlipViewGroup::populating: startPos=" + startPos + " endPos=" + endPos);

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

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

    if (Logger.isDebug) {
        Logger.i("FlipViewGroup::Current page list:");
        for (int i = 0; i < items.size(); i++) {
            Logger.i("FlipViewGroup::#" + i + ": page " + items.get(i).position);
        }
    }

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

    // 
    adapter.finishUpdate(this);

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

From source file:im.ene.lab.design.widget.coverflow.FeatureCoverFlow.java

@Override
protected void dispatchDraw(Canvas canvas) {
    mInvalidated = false; //last invalidate which marked redrawInProgress, caused this
    // dispatchDraw. Clear flag to prevent creating loop

    mReverseOrderIndex = -1;//  ww  w .j a  v  a2 s  . c  om

    canvas.getClipBounds(mTempRect);
    mTempRect.top = 0;
    mTempRect.bottom = getHeight();
    canvas.clipRect(mTempRect);

    super.dispatchDraw(canvas);

    if (mScrollToPositionOnNextInvalidate != -1 && mAdapter != null && mAdapter.getCount() > 0) {
        final int lastCenterItemPosition = (mFirstItemPosition + mLastCenterItemIndex) % mAdapter.getCount();
        final int di = lastCenterItemPosition - mScrollToPositionOnNextInvalidate;
        mScrollToPositionOnNextInvalidate = -1;
        if (di != 0) {
            final int dst = (int) (di * mCoverWidth * mSpacing) - mCenterItemOffset;
            scrollBy(-dst, 0);
            shouldRepeat = true;
            postInvalidate();
            return;
        }
    }

    if (mTouchState == TOUCH_STATE_RESTING) {
        if (mAdapter != null) {
            final int lastCenterItemPosition = (mFirstItemPosition + mLastCenterItemIndex)
                    % mAdapter.getCount();
            if (mLastTouchState != TOUCH_STATE_RESTING || mlastCenterItemPosition != lastCenterItemPosition) {
                mLastTouchState = TOUCH_STATE_RESTING;
                mlastCenterItemPosition = lastCenterItemPosition;
                if (mOnScrollPositionListener != null)
                    mOnScrollPositionListener.onScrolledToPosition(lastCenterItemPosition);
            }
        }
    }

    if (mTouchState == TOUCH_STATE_SCROLLING && mLastTouchState != TOUCH_STATE_SCROLLING) {
        mLastTouchState = TOUCH_STATE_SCROLLING;
        if (mOnScrollPositionListener != null)
            mOnScrollPositionListener.onScrolling();
    }
    if (mTouchState == TOUCH_STATE_FLING && mLastTouchState != TOUCH_STATE_FLING) {
        mLastTouchState = TOUCH_STATE_FLING;
        if (mOnScrollPositionListener != null)
            mOnScrollPositionListener.onScrolling();
    }

    //make sure we never stay unaligned after last draw in resting state
    if (mTouchState == TOUCH_STATE_RESTING && mCenterItemOffset != 0) {
        scrollBy(mCenterItemOffset, 0);
        postInvalidate();
    }

    try {
        View v = getChildAt(mLastCenterItemIndex);
        if (v != null)
            v.requestFocus(FOCUS_FORWARD);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.yao.zhihudaily.tool.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;//from w ww  . j a v a 2  s  .  co  m
    }
    // Bail now if we are waiting to populate.  This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a new position until we have finished the scroll to
    // that position, avoiding glitches from happening at that point.
    if (mPopulatePending) {
        if (DEBUG)
            Log.i(TAG, "populate is pending, skipping for now...");
        return;
    }
    // Also, don't populate until we are attached to a window.  This is to
    // avoid trying to populate before we have restored our view hierarchy
    // state and conflicting with what is restored.
    if (getWindowToken() == null) {
        return;
    }
    mAdapter.startUpdate(this);
    final int pageLimit = mOffscreenPageLimit;

    final int startPos = Math.max(0, mCurItem - pageLimit);
    final int N = mAdapter.getCount();
    final int endPos = Math.min(N - 1, mCurItem + pageLimit);
    if (DEBUG)
        Log.v(TAG, "populating: startPos=" + startPos + " endPos=" + endPos);
    // Add and remove pages in the existing list.
    int lastPos = -1;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {
            if (DEBUG)
                Log.i(TAG, "removing: " + ii.position + " @ " + i);
            mItems.remove(i);
            i--;
            mAdapter.destroyItem(this, ii.position, ii.object);
        } else if (lastPos < endPos && ii.position > startPos) {
            // The next item is outside of our range, but we have a gap
            // between it and the last item where we want to have a page
            // shown.  Fill in the gap.
            lastPos++;
            if (lastPos < startPos) {
                lastPos = startPos;
            }
            while (lastPos <= endPos && lastPos < ii.position) {
                if (DEBUG)
                    Log.i(TAG, "inserting: " + lastPos + " @ " + i);
                addNewItem(lastPos, i);
                lastPos++;
                i++;
            }
        }
        lastPos = ii.position;
    }
    // Add any new pages we need at the end.
    lastPos = mItems.size() > 0 ? mItems.get(mItems.size() - 1).position : -1;
    if (lastPos < endPos) {
        lastPos++;
        lastPos = lastPos > startPos ? lastPos : startPos;
        while (lastPos <= endPos) {
            if (DEBUG)
                Log.i(TAG, "appending: " + lastPos);
            addNewItem(lastPos, -1);
            lastPos++;
        }
    }
    if (DEBUG) {
        Log.i(TAG, "Current page list:");
        for (int i = 0; i < mItems.size(); i++) {
            Log.i(TAG, "#" + i + ": page " + mItems.get(i).position);
        }
    }
    ItemInfo curItem = null;
    for (int i = 0; i < mItems.size(); i++) {
        if (mItems.get(i).position == mCurItem) {
            curItem = mItems.get(i);
            break;
        }
    }
    mAdapter.setPrimaryItem(this, mCurItem, curItem != null ? curItem.object : null);
    mAdapter.finishUpdate(this);
    if (hasFocus()) {
        View currentFocused = findFocus();
        ItemInfo ii = currentFocused != null ? infoForAnyChild(currentFocused) : null;
        if (ii == null || ii.position != mCurItem) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                ii = infoForChild(child);
                if (ii != null && ii.position == mCurItem) {
                    if (child.requestFocus(FOCUS_FORWARD)) {
                        break;
                    }
                }
            }
        }
    }
}

From source file:com.oguzbabaoglu.cardpager.CardPager.java

/**
 * Check if child needs focus.// w w w .  j  a v a 2s .c o m
 *
 * @param focusDirection focusDirection
 */
private void checkFocus(int focusDirection) {

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

From source file:com.jzh.stuapp.view.MyViewPager.java

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

    if (mPopulatePending) {
        if (DEBUG)
            Log.i(TAG, "populate is pending, skipping for now...");
        return;
    }

    if (getWindowToken() == null) {
        return;
    }

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;

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

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

    int lastPos = -1;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if ((ii.position < startPos || ii.position > endPos) && !ii.scrolling) {
            if (DEBUG)
                Log.i(TAG, "removing: " + ii.position + " @ " + i);
            mItems.remove(i);
            i--;
            mAdapter.destroyItem(this, ii.position, ii.object);
        } else if (lastPos < endPos && ii.position > startPos) {
            lastPos++;
            if (lastPos < startPos) {
                lastPos = startPos;
            }
            while (lastPos <= endPos && lastPos < ii.position) {
                if (DEBUG)
                    Log.i(TAG, "inserting: " + lastPos + " @ " + i);
                addNewItem(lastPos, i);
                lastPos++;
                i++;
            }
        }
        lastPos = ii.position;
    }

    lastPos = mItems.size() > 0 ? mItems.get(mItems.size() - 1).position : -1;
    if (lastPos < endPos) {
        lastPos++;
        lastPos = lastPos > startPos ? lastPos : startPos;
        while (lastPos <= endPos) {
            if (DEBUG)
                Log.i(TAG, "appending: " + lastPos);
            addNewItem(lastPos, -1);
            lastPos++;
        }
    }

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

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

    mAdapter.finishUpdate(this);

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

From source file:io.naotou.beijingnews.view.LazyViewPager.java

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

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

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

    mAdapter.startUpdate(this);
    //0
    final int pageLimit = mOffscreenPageLimit;

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

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

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

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

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

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

    mAdapter.finishUpdate(this);

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

From source file:bw.com.yunifangstore.view.LazyViewPager.java

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

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

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

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;

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

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

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

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

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

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

    mAdapter.finishUpdate(this);

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

From source file:com.agile.news.view.LazyViewPager.java

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

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

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

    mAdapter.startUpdate(this);

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

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

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

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

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

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

    mAdapter.finishUpdate(this);

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

From source file:com.example.show.NoPreloadViewPager.java

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

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

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

    mAdapter.startUpdate(this);

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

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

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

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

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

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

    mAdapter.finishUpdate(this);

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