Example usage for android.view View FOCUS_FORWARD

List of usage examples for android.view View FOCUS_FORWARD

Introduction

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

Prototype

int FOCUS_FORWARD

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

Click Source Link

Document

Use with #focusSearch(int) .

Usage

From source file:org.chromium.chrome.browser.payments.ui.EditorView.java

/**
 * Builds the editor view.// w  ww  . j a  v a 2s . c  om
 *
 * @param activity        The activity on top of which the UI should be displayed.
 * @param observerForTest Optional event observer for testing.
 */
public EditorView(Activity activity, PaymentRequestObserverForTest observerForTest) {
    super(activity, R.style.FullscreenWhite);
    // Sets transparent background for animating content view.
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    mContext = activity;
    mObserverForTest = observerForTest;
    mHandler = new Handler();
    mEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                mDoneButton.performClick();
                return true;
            } else if (actionId == EditorInfo.IME_ACTION_NEXT) {
                View next = v.focusSearch(View.FOCUS_FORWARD);
                if (next != null) {
                    next.requestFocus();
                    return true;
                }
            }
            return false;
        }
    };

    mHalfRowMargin = activity.getResources().getDimensionPixelSize(R.dimen.payments_section_large_spacing);
    mFieldViews = new ArrayList<>();
    mEditableTextFields = new ArrayList<>();
    mDropdownFields = new ArrayList<>();

    final Pattern cardNumberPattern = Pattern.compile("^[\\d- ]*$");
    mCardNumberInputFilter = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
                int dend) {
            // Accept deletions.
            if (start == end)
                return null;

            // Accept digits, "-", and spaces.
            if (cardNumberPattern.matcher(source.subSequence(start, end)).matches()) {
                return null;
            }

            // Reject everything else.
            return "";
        }
    };

    mCardNumberFormatter = new CreditCardNumberFormattingTextWatcher();
    new AsyncTask<Void, Void, PhoneNumberFormattingTextWatcher>() {
        @Override
        protected PhoneNumberFormattingTextWatcher doInBackground(Void... unused) {
            return new PhoneNumberFormattingTextWatcher();
        }

        @Override
        protected void onPostExecute(PhoneNumberFormattingTextWatcher result) {
            mPhoneFormatter = result;
            if (mPhoneInput != null) {
                mPhoneInput.addTextChangedListener(mPhoneFormatter);
            }
        }
    }.execute();
}

From source file:com.android.calculator2.EventListener.java

@Override
public void onClick(View view) {
    View v;//from w ww  .  j  a v a 2  s .c  om
    EditText active;
    int id = view.getId();
    switch (id) {
    case R.id.del:
        mHandler.onDelete();
        break;

    case R.id.clear:
        mHandler.onClear();
        break;

    case R.id.equal:
        if (mHandler.getText().contains(mX) || mHandler.getText().contains(mY)) {
            if (!mHandler.getText().contains("=")) {
                mHandler.insert("=");
                returnToBasic();
            }
            break;
        }
        mHandler.onEnter();
        break;

    case R.id.hex:
        mHandler.setText(mHandler.mBaseModule.setMode(Mode.HEXADECIMAL));
        view.setSelected(true);
        ((View) view.getParent()).findViewById(R.id.bin).setSelected(false);
        ((View) view.getParent()).findViewById(R.id.dec).setSelected(false);
        applyAllBannedResources(mHandler.mBaseModule, Mode.HEXADECIMAL);
        break;

    case R.id.bin:
        mHandler.setText(mHandler.mBaseModule.setMode(Mode.BINARY));
        view.setSelected(true);
        ((View) view.getParent()).findViewById(R.id.hex).setSelected(false);
        ((View) view.getParent()).findViewById(R.id.dec).setSelected(false);
        applyAllBannedResources(mHandler.mBaseModule, Mode.BINARY);
        break;

    case R.id.dec:
        mHandler.setText(mHandler.mBaseModule.setMode(Mode.DECIMAL));
        view.setSelected(true);
        ((View) view.getParent()).findViewById(R.id.bin).setSelected(false);
        ((View) view.getParent()).findViewById(R.id.hex).setSelected(false);
        applyAllBannedResources(mHandler.mBaseModule, Mode.DECIMAL);
        break;

    case R.id.matrix:
        mHandler.insert(MatrixView.getPattern(mContext));
        returnToBasic();
        break;

    case R.id.matrix_inverse:
        mHandler.insert(MatrixInverseView.PATTERN);
        returnToBasic();
        break;

    case R.id.matrix_transpose:
        mHandler.insert(MatrixTransposeView.PATTERN);
        returnToBasic();
        break;

    case R.id.plus_row:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().addRow();
        break;

    case R.id.minus_row:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().removeRow();
        break;

    case R.id.plus_col:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().addColumn();
        break;

    case R.id.minus_col:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().removeColumn();
        break;

    case R.id.next:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        active = mHandler.mDisplay.getActiveEditText();
        if (active.getSelectionStart() == active.getText().length()) {
            v = mHandler.mDisplay.getActiveEditText().focusSearch(View.FOCUS_FORWARD);
            if (v != null)
                v.requestFocus();
            active = mHandler.mDisplay.getActiveEditText();
            active.setSelection(0);
        } else {
            active.setSelection(active.getSelectionStart() + 1);
        }
        break;

    // +/-, changes the sign of the current number. Might be useful later
    // (but removed for now)
    // case R.id.sign:
    // if(mHandler.getText().equals(mErrorString)) mHandler.setText("");
    // active = mHandler.mDisplay.getActiveEditText();
    // int selection = active.getSelectionStart();
    // if(active.getText().toString().matches(Logic.NUMBER)) {
    // if(active.getText().toString().startsWith(String.valueOf(Logic.MINUS)))
    // {
    // active.setText(active.getText().toString().substring(1));
    // selection--;
    // }
    // else {
    // active.setText(Logic.MINUS + active.getText().toString());
    // selection++;
    // }
    // if(selection > active.length()) selection--;
    // if(selection < 0) selection = 0;
    // active.setSelection(selection);
    // }
    // break;

    case R.id.parentheses:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        if (mHandler.getText().contains("=")) {
            String[] equation = mHandler.getText().split("=");
            if (equation.length > 1) {
                mHandler.setText(equation[0] + "=(" + equation[1] + ")");
            } else {
                mHandler.setText(equation[0] + "=()");
            }
        } else {
            mHandler.setText("(" + mHandler.getText() + ")");
        }
        returnToBasic();
        break;

    case R.id.mod:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        if (mHandler.getText().contains("=")) {
            String[] equation = mHandler.getText().split("=");
            if (equation.length > 1) {
                mHandler.setText(equation[0] + "=" + mModString + "(" + equation[1] + ",");
            } else {
                mHandler.insert(mModString + "(");
            }
        } else {
            if (mHandler.getText().length() > 0) {
                mHandler.setText(mModString + "(" + mHandler.getText() + ",");
            } else {
                mHandler.insert(mModString + "(");
            }
        }
        returnToBasic();
        break;

    case R.id.easter:
        Toast.makeText(mContext, R.string.easter_egg, Toast.LENGTH_SHORT).show();
        break;

    default:
        if (view instanceof Button) {
            String text = ((Button) view).getText().toString();
            if (text.equals(mDX) || text.equals(mDY)) {
                // Do nothing
            } else if (text.length() >= 2) {
                // Add paren after sin, cos, ln, etc. from buttons
                text += "(";
            }
            mHandler.insert(text);
            returnToBasic();
        }
    }
}

From source file:com.android2.calculator3.EventListener.java

@Override
public void onClick(View view) {
    vibrate();/* w  ww .j  a  v  a 2s. co  m*/
    View v;
    EditText active;
    int id = view.getId();
    switch (id) {
    case R.id.del:
        mHandler.onDelete();
        break;

    case R.id.clear:
        mHandler.onClear();
        break;

    case R.id.equal:
        if (mHandler.getText().contains(mX) || mHandler.getText().contains(mY)) {
            if (!mHandler.getText().contains("=")) {
                mHandler.insert("=");
                returnToBasic();
            }
            break;
        }
        mHandler.onEnter();
        break;

    case R.id.hex:
        try {
            mHandler.setText(mHandler.getBaseModule().setBase(mHandler.getText().toString(), Base.HEXADECIMAL));
        } catch (SyntaxException e) {
            e.printStackTrace();
        }
        applyBannedResources(Base.HEXADECIMAL);
        break;

    case R.id.bin:
        try {
            mHandler.setText(mHandler.getBaseModule().setBase(mHandler.getText().toString(), Base.BINARY));
        } catch (SyntaxException e) {
            e.printStackTrace();
        }
        applyBannedResources(Base.BINARY);
        break;

    case R.id.dec:
        try {
            mHandler.setText(mHandler.getBaseModule().setBase(mHandler.getText().toString(), Base.DECIMAL));
        } catch (SyntaxException e) {
            e.printStackTrace();
        }
        applyBannedResources(Base.DECIMAL);
        break;

    case R.id.matrix:
        mHandler.insert(MatrixView.getPattern());
        returnToBasic();
        break;

    case R.id.matrix_inverse:
        mHandler.insert(MatrixInverseView.PATTERN);
        returnToBasic();
        break;

    case R.id.matrix_transpose:
        mHandler.insert(MatrixTransposeView.PATTERN);
        returnToBasic();
        break;

    case R.id.plus_row:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().addRow();
        break;

    case R.id.minus_row:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().removeRow();
        break;

    case R.id.plus_col:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().addColumn();
        break;

    case R.id.minus_col:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().removeColumn();
        break;

    case R.id.next:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        active = mHandler.mDisplay.getActiveEditText();
        if (active.getSelectionStart() == active.getText().length()) {
            v = mHandler.mDisplay.getActiveEditText().focusSearch(View.FOCUS_FORWARD);
            if (v != null)
                v.requestFocus();
            active = mHandler.mDisplay.getActiveEditText();
            active.setSelection(0);
        } else {
            active.setSelection(active.getSelectionStart() + 1);
        }
        break;

    case R.id.parentheses:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        if (mHandler.getText().contains("=")) {
            String[] equation = mHandler.getText().split("=");
            if (equation.length > 1) {
                mHandler.setText(equation[0] + "=(" + equation[1] + ")");
            } else {
                mHandler.setText(equation[0] + "=()");
            }
        } else {
            mHandler.setText("(" + mHandler.getText() + ")");
        }
        returnToBasic();
        break;

    case R.id.mod:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        if (mHandler.getText().contains("=")) {
            String[] equation = mHandler.getText().split("=");
            if (equation.length > 1) {
                mHandler.setText(equation[0] + "=" + mModString + "(" + equation[1] + ",");
            } else {
                mHandler.insert(mModString + "(");
            }
        } else {
            if (mHandler.getText().length() > 0) {
                mHandler.setText(mModString + "(" + mHandler.getText() + ",");
            } else {
                mHandler.insert(mModString + "(");
            }
        }
        returnToBasic();
        break;

    case R.id.easter:
        Toast.makeText(mContext, R.string.easter_egg, Toast.LENGTH_SHORT).show();
        break;

    case R.id.zoomIn:
        mGraphDisplay.zoomIn();
        break;

    case R.id.zoomOut:
        mGraphDisplay.zoomOut();
        break;

    case R.id.zoomReset:
        mGraphDisplay.zoomReset();
        break;

    default:
        if (view instanceof Button) {
            String text = ((Button) view).getText().toString();
            if (text.equals(mDX) || text.equals(mDY)) {
                // Do nothing
            } else if (text.length() >= 2) {
                // Add paren after sin, cos, ln, etc. from buttons
                text += "(";
            }
            mHandler.insert(text);
            returnToBasic();
        }
    }
}

From source file:com.KittleApps.app.TransCalc.EventListener.java

@Override
public void onClick(View view) {
    View v;/* www.j  a  va  2  s.  c o m*/
    EditText active;
    int id = view.getId();
    switch (id) {
    case R.id.del:
        mHandler.onDelete();
        break;

    case R.id.clear:
        mHandler.onClear();
        break;

    case R.id.equal:
        if (mHandler.getText().contains(mX) || mHandler.getText().contains(mY)) {
            if (!mHandler.getText().contains("=")) {
                mHandler.insert("=");
                returnToBasic();
            }
            break;
        }
        mHandler.onEnter();
        break;

    case R.id.hex:
        mHandler.setText(mHandler.mBaseModule.setMode(Mode.HEXADECIMAL));
        view.setBackgroundResource(R.color.pressed_color);
        ((View) view.getParent()).findViewById(R.id.bin).setBackgroundResource(R.drawable.btn_function);
        ((View) view.getParent()).findViewById(R.id.dec).setBackgroundResource(R.drawable.btn_function);
        for (int i : mHandler.mBaseModule.bannedResourceInBinary) {
            if (mPager != null) {
                v = mPager.findViewById(i);
            } else {
                v = mSmallPager.findViewById(i);
                if (v == null)
                    v = mLargePager.findViewById(i);
            }
            v.setEnabled(true);
        }
        break;

    case R.id.bin:
        mHandler.setText(mHandler.mBaseModule.setMode(Mode.BINARY));
        view.setBackgroundResource(R.color.pressed_color);
        ((View) view.getParent()).findViewById(R.id.hex).setBackgroundResource(R.drawable.btn_function);
        ((View) view.getParent()).findViewById(R.id.dec).setBackgroundResource(R.drawable.btn_function);
        for (int i : mHandler.mBaseModule.bannedResourceInBinary) {
            if (mPager != null) {
                v = mPager.findViewById(i);
            } else {
                v = mSmallPager.findViewById(i);
                if (v == null)
                    v = mLargePager.findViewById(i);
            }
            v.setEnabled(false);
        }
        break;

    case R.id.dec:
        mHandler.setText(mHandler.mBaseModule.setMode(Mode.DECIMAL));
        view.setBackgroundResource(R.color.pressed_color);
        ((View) view.getParent()).findViewById(R.id.bin).setBackgroundResource(R.drawable.btn_function);
        ((View) view.getParent()).findViewById(R.id.hex).setBackgroundResource(R.drawable.btn_function);
        for (int i : mHandler.mBaseModule.bannedResourceInBinary) {
            if (mPager != null) {
                v = mPager.findViewById(i);
            } else {
                v = mSmallPager.findViewById(i);
                if (v == null)
                    v = mLargePager.findViewById(i);
            }
            v.setEnabled(true);
        }
        for (int i : mHandler.mBaseModule.bannedResourceInDecimal) {
            if (mPager != null) {
                v = mPager.findViewById(i);
            } else {
                v = mSmallPager.findViewById(i);
                if (v == null)
                    v = mLargePager.findViewById(i);
            }
            v.setEnabled(false);
        }
        break;

    case R.id.matrix:
        mHandler.insert(MatrixView.PATTERN);
        returnToBasic();
        break;

    case R.id.matrix_inverse:
        mHandler.insert(MatrixInverseView.PATTERN);
        returnToBasic();
        break;

    case R.id.matrix_transpose:
        mHandler.insert(MatrixTransposeView.PATTERN);
        returnToBasic();
        break;

    case R.id.plus_row:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().addRow();
        break;

    case R.id.minus_row:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().removeRow();
        break;

    case R.id.plus_col:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().addColumn();
        break;

    case R.id.minus_col:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().removeColumn();
        break;

    case R.id.next:
        active = mHandler.mDisplay.getActiveEditText();
        if (active.getSelectionStart() == active.getText().length()) {
            v = mHandler.mDisplay.getActiveEditText().focusSearch(View.FOCUS_FORWARD);
            if (v != null)
                v.requestFocus();
            active = mHandler.mDisplay.getActiveEditText();
            active.setSelection(0);
        } else {
            active.setSelection(active.getSelectionStart() + 1);
        }
        break;

    case R.id.sign:
        active = mHandler.mDisplay.getActiveEditText();
        int selection = active.getSelectionStart();
        if (active.getText().toString().matches(Logic.NUMBER)) {
            if (active.getText().toString().startsWith(String.valueOf(Logic.MINUS))) {
                active.setText(active.getText().toString().substring(1));
                selection--;
            } else {
                active.setText(Logic.MINUS + active.getText().toString());
                selection++;
            }
            if (selection > active.length())
                selection--;
            if (selection < 0)
                selection = 0;
            active.setSelection(selection);
        }
        break;

    case R.id.parentheses:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        if (mHandler.getText().contains("=")) {
            String[] equation = mHandler.getText().split("=");
            if (equation.length > 1) {
                mHandler.setText(equation[0] + "=(" + equation[1] + ")");
            } else {
                mHandler.setText(equation[0] + "=()");
            }
        } else {
            mHandler.setText("(" + mHandler.getText() + ")");
        }
        returnToBasic();
        break;

    case R.id.mod:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        if (mHandler.getText().contains("=")) {
            String[] equation = mHandler.getText().split("=");
            if (equation.length > 1) {
                mHandler.setText(equation[0] + "=" + mModString + "(" + equation[1] + ",");
            } else {
                mHandler.insert(mModString + "(");
            }
        } else {
            if (mHandler.getText().length() > 0) {
                mHandler.setText(mModString + "(" + mHandler.getText() + ",");
            } else {
                mHandler.insert(mModString + "(");
            }
        }
        returnToBasic();
        break;

    case R.id.easter:
        Toast.makeText(mContext, R.string.easter_egg, Toast.LENGTH_SHORT).show();
        break;

    default:
        if (view instanceof Button) {
            if (mHandler.getText().equals(mErrorString))
                mHandler.setText("");
            String text = ((Button) view).getText().toString();
            if (text.equals(mDX) || text.equals(mDY)) {
                // Do nothing
            } else if (text.length() >= 2) {
                // Add paren after sin, cos, ln, etc. from buttons
                text += "(";
            }
            mHandler.insert(text);
            returnToBasic();
        }
    }
}

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

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

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

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

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

    pagerAdapter.startUpdate(this);

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

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

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

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

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

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

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

        calculatePageOffsets(curItem, curIndex, oldCurInfo);
    }

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

    pagerAdapter.finishUpdate(this);

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

    sortChildDrawingOrder();
    checkFocus(focusDirection);
}

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

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

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

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

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

    mAdapter.startUpdate(this);

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

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

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

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

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

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

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

        calculatePageOffsets(curItem, curIndex, oldCurInfo);
    }

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

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

    mAdapter.finishUpdate(this);

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

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

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

void populate(int newCurrentItem) {
    ItemInfo oldCurInfo = null;//from   www  .  ja va2s.  c o m
    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:com.tunebrains.views.InfiniteViewPager.java

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

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

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

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

    mAdapter.startUpdate(this);

    final int pageLimit = mOffscreenPageLimit;
    final int startPos = mCurItem - pageLimit;
    final int N = mAdapter.getCount();
    final int endPos = mCurItem + pageLimit;

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

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

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

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

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

        calculatePageOffsets(curItem, curIndex, oldCurInfo);
    }

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

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

    mAdapter.finishUpdate(this);

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

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

From source file:com.example.mvpdemo.widget.NarrowParentViewPager.java

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

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

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

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

    mAdapter.startUpdate(this);

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

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

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

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

    // Fill 3x the available width or up to the number of offscreen
    // pages requested to either side, whichever is larger.
    // If we have no current item we have no work to do.
    if (curItem != null) {
        float extraWidthLeft = 0.f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
        final int clientWidth = getClientWidth();
        final float leftWidthNeeded = clientWidth <= 0 ? 0
                : 2.f - curItem.widthFactor + (float) getPaddingLeft() / (float) clientWidth;
        for (int pos = mCurItem - 1; pos >= 0; pos--) {
            if (extraWidthLeft >= leftWidthNeeded && 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: " + (ii.object));
                    }
                    itemIndex--;
                    curIndex--;
                    ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
                }
            } else if (ii != null && pos == ii.position) {
                extraWidthLeft += ii.widthFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraWidthLeft += ii.widthFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            }
        }

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

        calculatePageOffsets(curItem, curIndex, oldCurInfo);
    }

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

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

    mAdapter.finishUpdate(this);

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

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

From source file:com.bloomimpact.bancus.ui.views.VerticalViewPager.java

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

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

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

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

    mAdapter.startUpdate(this);

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

    if (N != mExpectedAdapterCount) {
        String resName;
        try {
            resName = getResources().getResourceName(getId());
        } catch (Resources.NotFoundException e) {
            resName = Integer.toHexString(getId());
        }
        throw new IllegalStateException("The application's PagerAdapter changed the adapter's"
                + " contents without calling PagerAdapter#notifyDataSetChanged!"
                + " Expected adapter item count: " + mExpectedAdapterCount + ", found: " + N + " Pager id: "
                + resName + " Pager class: " + ((Object) this).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 extraHeightAbove = 0.f;
        int itemIndex = curIndex - 1;
        ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
        final int clientHeight = getClientHeight();
        final float aboveHeightNeeded = clientHeight <= 0 ? 0
                : 2.f - curItem.heightFactor + (float) getPaddingTop() / (float) clientHeight;
        for (int pos = mCurItem - 1; pos >= 0; pos--) {
            if (extraHeightAbove >= aboveHeightNeeded && 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) {
                extraHeightAbove += ii.heightFactor;
                itemIndex--;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            } else {
                ii = addNewItem(pos, itemIndex + 1);
                extraHeightAbove += ii.heightFactor;
                curIndex++;
                ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
            }
        }

        float extraHeightBelow = curItem.heightFactor;
        itemIndex = curIndex + 1;
        if (extraHeightBelow < 2.f) {
            ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
            final float belowHeightNeeded = clientHeight <= 0 ? 0
                    : (float) getPaddingBottom() / (float) clientHeight + 2.f;
            for (int pos = mCurItem + 1; pos < N; pos++) {
                if (extraHeightBelow >= belowHeightNeeded && 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) {
                    extraHeightBelow += ii.heightFactor;
                    itemIndex++;
                    ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
                } else {
                    ii = addNewItem(pos, itemIndex);
                    itemIndex++;
                    extraHeightBelow += 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;
                    }
                }
            }
        }
    }
}