Example usage for android.view View requestFocus

List of usage examples for android.view View requestFocus

Introduction

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

Prototype

public final boolean requestFocus(int direction) 

Source Link

Document

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

Usage

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

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

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

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

    mAdapter.startUpdate(this);
    //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:com.adorable.news.view.LazyViewPager.java

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

    // Bail now if we are waiting to populate.  This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a 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:com.kuaiche.freight.logistics.common.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);

    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;
        }
    }
    if (mAdapter != null) {
        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.androids.doctor.view.LazyViewPager.java

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

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

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

    mAdapter.startUpdate(this);

    //0,???
    final int pageLimit = mOffscreenPageLimit;
    //max : 
    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.cmm.worldartapk.ui.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;//from w  w w.j a va 2 s  .  com
    }

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

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

    mAdapter.startUpdate(this);
    //pageLimit = 0
    final int pageLimit = mOffscreenPageLimit;
    //viewpager?mCurItem??
    final int startPos = Math.max(0, mCurItem - pageLimit);
    //viewpager? 3
    final int N = mAdapter.getCount();
    //viewpager??
    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:item.lhd.com.itemdrag.view.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;//  w  w  w  .  j  a v a2 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);
    //pageLimit = 0
    final int pageLimit = mOffscreenPageLimit;
    //viewpagermCurItem
    final int startPos = Math.max(0, mCurItem - pageLimit);
    //viewpager 3
    final int N = mAdapter.getCount();
    //viewpager
    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.devel.newsclient.view.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;/* www. j  ava2 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);

    /**
     *
     * startPos = 0 1
     * endPos = 0 1
     */

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

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

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

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

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

    mAdapter.finishUpdate(this);

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

From source file:cn.d.fesa.wuf.ui.view.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;//w w  w  .  j a va2 s. com
    }

    // Bail now if we are waiting to populate.  This is to hold off
    // on creating views from the time the user releases their finger to
    // fling to a 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;

    //?mCurItem????
    final int startPos = Math.max(0, mCurItem - pageLimit);
    //?viewpager?
    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.demo.newsclient2.View.LazyViewPager.java

void populate() {
    if (mAdapter == null) {
        return;/*from w  w w.j a  va 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);
    /**
      startPos--- 0 1 2
      endPos -----0 1 2
     * 
     */

    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.sundyn.bluesky.view.LazyViewPager.java

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

    /**
     * 
     * startPos = 0 1
     * endPos = 0 1
     */

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