Example usage for android.view View FOCUS_LEFT

List of usage examples for android.view View FOCUS_LEFT

Introduction

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

Prototype

int FOCUS_LEFT

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

Click Source Link

Document

Use with #focusSearch(int) .

Usage

From source file:com.amaze.carbonfilemanager.activities.MainActivity.java

public void updatePath(@NonNull final String news, boolean results, OpenMode openmode, int folder_count,
        int file_count) {

    if (news.length() == 0)
        return;//from w  w w .ja  v a 2  s  .  co m

    switch (openmode) {
    case SMB:
        newPath = mainActivityHelper.parseSmbPath(news);
        break;
    case OTG:
        newPath = mainActivityHelper.parseOTGPath(news);
        break;
    case CUSTOM:
        newPath = mainActivityHelper.getIntegralNames(news);
        break;
    case DROPBOX:
    case BOX:
    case ONEDRIVE:
    case GDRIVE:
        newPath = mainActivityHelper.parseCloudPath(openmode, news);
        break;
    default:
        newPath = news;
    }

    final TextView bapath = (TextView) pathbar.findViewById(R.id.fullpath);
    final TextView animPath = (TextView) pathbar.findViewById(R.id.fullpath_anim);
    TextView textView = (TextView) pathbar.findViewById(R.id.pathname);
    if (!results) {
        textView.setText(folder_count + " " + getResources().getString(R.string.folders) + "" + " " + file_count
                + " " + getResources().getString(R.string.files));
    } else {
        bapath.setText(R.string.searchresults);
        textView.setText(R.string.empty);
        return;
    }
    final String oldPath = bapath.getText().toString();
    if (oldPath.equals(newPath))
        return;

    // implement animation while setting text
    newPathBuilder = new StringBuffer().append(newPath);
    oldPathBuilder = new StringBuffer().append(oldPath);

    final Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in);
    Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out);

    if (newPath.length() > oldPath.length()
            && newPathBuilder.delete(oldPath.length(), newPath.length()).toString().equals(oldPath)
            && oldPath.length() != 0) {

        // navigate forward
        newPathBuilder.delete(0, newPathBuilder.length());
        newPathBuilder.append(newPath);
        newPathBuilder.delete(0, oldPath.length());
        animPath.setAnimation(slideIn);
        animPath.animate().setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        animPath.setVisibility(View.GONE);
                        bapath.setText(newPath);
                    }
                }, PATH_ANIM_END_DELAY);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                animPath.setVisibility(View.VISIBLE);
                animPath.setText(newPathBuilder.toString());
                //bapath.setText(oldPath);

                scroll.post(new Runnable() {
                    @Override
                    public void run() {
                        scroll1.fullScroll(View.FOCUS_RIGHT);
                    }
                });
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                //onAnimationEnd(animation);
            }
        }).setStartDelay(PATH_ANIM_START_DELAY).start();
    } else if (newPath.length() < oldPath.length()
            && oldPathBuilder.delete(newPath.length(), oldPath.length()).toString().equals(newPath)) {

        // navigate backwards
        oldPathBuilder.delete(0, oldPathBuilder.length());
        oldPathBuilder.append(oldPath);
        oldPathBuilder.delete(0, newPath.length());
        animPath.setAnimation(slideOut);
        animPath.animate().setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                animPath.setVisibility(View.GONE);
                bapath.setText(newPath);

                scroll.post(new Runnable() {
                    @Override
                    public void run() {
                        scroll1.fullScroll(View.FOCUS_RIGHT);
                    }
                });
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                animPath.setVisibility(View.VISIBLE);
                animPath.setText(oldPathBuilder.toString());
                bapath.setText(newPath);

                scroll.post(new Runnable() {
                    @Override
                    public void run() {
                        scroll1.fullScroll(View.FOCUS_LEFT);
                    }
                });
            }
        }).setStartDelay(PATH_ANIM_START_DELAY).start();
    } else if (oldPath.isEmpty()) {
        // case when app starts
        // FIXME: COUNTER is incremented twice on app startup
        COUNTER++;
        if (COUNTER == 2) {
            animPath.setAnimation(slideIn);
            animPath.setText(newPath);
            animPath.animate().setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    super.onAnimationStart(animation);
                    animPath.setVisibility(View.VISIBLE);
                    bapath.setText("");
                    scroll.post(new Runnable() {
                        @Override
                        public void run() {
                            scroll1.fullScroll(View.FOCUS_RIGHT);
                        }
                    });
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            animPath.setVisibility(View.GONE);
                            bapath.setText(newPath);
                        }
                    }, PATH_ANIM_END_DELAY);
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    super.onAnimationCancel(animation);
                    //onAnimationEnd(animation);
                }
            }).setStartDelay(PATH_ANIM_START_DELAY).start();
        }
    } else {
        // completely different path
        // first slide out of old path followed by slide in of new path
        animPath.setAnimation(slideOut);
        animPath.animate().setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animator) {
                super.onAnimationStart(animator);
                animPath.setVisibility(View.VISIBLE);
                animPath.setText(oldPath);
                bapath.setText("");

                scroll.post(new Runnable() {
                    @Override
                    public void run() {
                        scroll1.fullScroll(View.FOCUS_LEFT);
                    }
                });
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                super.onAnimationEnd(animator);

                //animPath.setVisibility(View.GONE);
                animPath.setText(newPath);
                bapath.setText("");
                animPath.setAnimation(slideIn);

                animPath.animate().setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                animPath.setVisibility(View.GONE);
                                bapath.setText(newPath);
                            }
                        }, PATH_ANIM_END_DELAY);
                    }

                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        // we should not be having anything here in path bar
                        animPath.setVisibility(View.VISIBLE);
                        bapath.setText("");
                        scroll.post(new Runnable() {
                            @Override
                            public void run() {
                                scroll1.fullScroll(View.FOCUS_RIGHT);
                            }
                        });
                    }
                }).start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                //onAnimationEnd(animation);
            }
        }).setStartDelay(PATH_ANIM_START_DELAY).start();
    }
}

From source file:com.appunite.list.HorizontalListView.java

/**
 * Do an arrow scroll based on focus searching.  If a new view is
 * given focus, return the selection delta and amount to scroll via
 * an {@link ArrowScrollFocusResult}, otherwise, return null.
 *
 * @param direction either {@link android.view.View#FOCUS_UP} or
 *        {@link android.view.View#FOCUS_DOWN}.
 * @return The result if focus has changed, or <code>null</code>.
 *///from w  ww  .ja  v a2s  .  c  o m
private ArrowScrollFocusResult arrowScrollFocused(final int direction) {
    final View selectedView = getSelectedView();
    View newFocus;
    if (selectedView != null && selectedView.hasFocus()) {
        View oldFocus = selectedView.findFocus();
        newFocus = FocusFinder.getInstance().findNextFocus(this, oldFocus, direction);
    } else {
        if (direction == View.FOCUS_RIGHT) {
            final boolean topFadingEdgeShowing = (mFirstPosition > 0);
            final int listLeft = mListPadding.left + (topFadingEdgeShowing ? getArrowScrollPreviewLength() : 0);
            final int xSearchPoint = (selectedView != null && selectedView.getLeft() > listLeft)
                    ? selectedView.getLeft()
                    : listLeft;
            mTempRect.set(xSearchPoint, 0, xSearchPoint, 0);
        } else {
            final boolean rightFadingEdgeShowing = (mFirstPosition + getChildCount() - 1) < mItemCount;
            final int listRight = getWidth() - mListPadding.right
                    - (rightFadingEdgeShowing ? getArrowScrollPreviewLength() : 0);
            final int xSearchPoint = (selectedView != null && selectedView.getRight() < listRight)
                    ? selectedView.getRight()
                    : listRight;
            mTempRect.set(xSearchPoint, 0, xSearchPoint, 0);
        }
        newFocus = FocusFinder.getInstance().findNextFocusFromRect(this, mTempRect, direction);
    }

    if (newFocus != null) {
        final int positionOfNewFocus = positionOfNewFocus(newFocus);

        // if the focus change is in a different new position, make sure
        // we aren't jumping over another selectable position
        if (mSelectedPosition != INVALID_POSITION && positionOfNewFocus != mSelectedPosition) {
            final int selectablePosition = lookForSelectablePositionOnScreen(direction);
            if (selectablePosition != INVALID_POSITION
                    && ((direction == View.FOCUS_RIGHT && selectablePosition < positionOfNewFocus)
                            || (direction == View.FOCUS_LEFT && selectablePosition > positionOfNewFocus))) {
                return null;
            }
        }

        int focusScroll = amountToScrollToNewFocus(direction, newFocus, positionOfNewFocus);

        final int maxScrollAmount = getMaxScrollAmount();
        if (focusScroll < maxScrollAmount) {
            // not moving too far, safe to give next view focus
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, focusScroll);
            return mArrowScrollFocusResult;
        } else if (distanceToView(newFocus) < maxScrollAmount) {
            // Case to consider:
            // too far to get entire next focusable on screen, but by going
            // max scroll amount, we are getting it at least partially in view,
            // so give it focus and scroll the max ammount.
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, maxScrollAmount);
            return mArrowScrollFocusResult;
        }
    }
    return null;
}

From source file:io.github.clendy.leanback.widget.GridLayoutManager.java

private int getMovement(int direction) {
    int movement = View.FOCUS_LEFT;

    if (mOrientation == HORIZONTAL) {
        switch (direction) {
        case View.FOCUS_LEFT:
            movement = (!mReverseFlowPrimary) ? PREV_ITEM : NEXT_ITEM;
            break;
        case View.FOCUS_RIGHT:
            movement = (!mReverseFlowPrimary) ? NEXT_ITEM : PREV_ITEM;
            break;
        case View.FOCUS_UP:
            movement = PREV_ROW;/*from w w w.j a v  a2  s  .c  om*/
            break;
        case View.FOCUS_DOWN:
            movement = NEXT_ROW;
            break;
        }
    } else if (mOrientation == VERTICAL) {
        switch (direction) {
        case View.FOCUS_LEFT:
            movement = (!mReverseFlowPrimary) ? PREV_ROW : NEXT_ROW;
            break;
        case View.FOCUS_RIGHT:
            movement = (!mReverseFlowPrimary) ? NEXT_ROW : PREV_ROW;
            break;
        case View.FOCUS_UP:
            movement = PREV_ITEM;
            break;
        case View.FOCUS_DOWN:
            movement = NEXT_ITEM;
            break;
        }
    }

    return movement;
}

From source file:com.example.sky.test.view.ViewPager.java

/**
 * Handle scrolling in response to a left or right arrow click.
 *
 * @param direction The direction corresponding to the arrow key that was pressed. It should be
 *                  either {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}.
 * @return Whether the scrolling was handled successfully.
 *///w w  w.  ja  v  a2 s  . c o m
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                .getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                    .getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view "
                    + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft >= currLeft) {
                handled = pageLeft();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_RIGHT) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft <= currLeft) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageRight();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:com.appunite.list.HorizontalListView.java

/**
 * Determine how much we need to scroll in order to get newFocus in view.
 * @param direction either {@link android.view.View#FOCUS_UP} or
 *        {@link android.view.View#FOCUS_DOWN}.
 * @param newFocus The view that would take focus.
 * @param positionOfNewFocus The position of the list item containing newFocus
 * @return The amount to scroll.  Note: this is always positive!  Direction
 *   needs to be taken into account when actually scrolling.
 *//*from   ww  w .  j  a v a2  s . c  o m*/
private int amountToScrollToNewFocus(int direction, View newFocus, int positionOfNewFocus) {
    int amountToScroll = 0;
    newFocus.getDrawingRect(mTempRect);
    offsetDescendantRectToMyCoords(newFocus, mTempRect);
    if (direction == View.FOCUS_LEFT) {
        if (mTempRect.left < mListPadding.left) {
            amountToScroll = mListPadding.left - mTempRect.left;
            if (positionOfNewFocus > 0) {
                amountToScroll += getArrowScrollPreviewLength();
            }
        }
    } else {
        final int listRight = getWidth() - mListPadding.right;
        if (mTempRect.right > listRight) {
            amountToScroll = mTempRect.right - listRight;
            if (positionOfNewFocus < mItemCount - 1) {
                amountToScroll += getArrowScrollPreviewLength();
            }
        }
    }
    return amountToScroll;
}

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

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;//from  w ww  . j a  v a2s. c  o  m
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                .getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                    .getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view "
                    + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft >= currLeft) {
                handled = pageLeft();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_RIGHT) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft <= currLeft) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_UP) {
            final int nextUp = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currUp = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextUp >= currUp) {
                handled = pageLeft();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_DOWN) {
            final int nextUp = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currUp = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextUp <= currUp) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageRight();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:com.viewpagerindicator.MyDirectionalViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;/*  www  . j av a2 s  .  c om*/
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                .getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                    .getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view "
                    + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft >= currLeft) {
                handled = pagePre();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_RIGHT) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft <= currLeft) {
                handled = pageNext();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pagePre();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageNext();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:com.guide.ViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;//  w w w  . java2 s  . c  om
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                .getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun
            // ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                    .getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view "
                    + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page
            // left.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft >= currLeft) {
                handled = pageLeft();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_RIGHT) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page
            // right.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft <= currLeft) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_UP) {
            final int nextUp = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currUp = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextUp >= currUp) {
                handled = pageLeft();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_DOWN) {
            final int nextUp = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currUp = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextUp <= currUp) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageRight();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:android.support.v17.leanback.widget.GridLayoutManager.java

private int getMovement(int direction) {
    int movement = View.FOCUS_LEFT;

    if (mOrientation == HORIZONTAL) {
        switch (direction) {
        case View.FOCUS_LEFT:
            movement = (!mReverseFlowPrimary) ? PREV_ITEM : NEXT_ITEM;
            break;
        case View.FOCUS_RIGHT:
            movement = (!mReverseFlowPrimary) ? NEXT_ITEM : PREV_ITEM;
            break;
        case View.FOCUS_UP:
            movement = PREV_ROW;//from  ww w.  jav  a2s.  c o m
            break;
        case View.FOCUS_DOWN:
            movement = NEXT_ROW;
            break;
        }
    } else if (mOrientation == VERTICAL) {
        switch (direction) {
        case View.FOCUS_LEFT:
            movement = (!mReverseFlowSecondary) ? PREV_ROW : NEXT_ROW;
            break;
        case View.FOCUS_RIGHT:
            movement = (!mReverseFlowSecondary) ? NEXT_ROW : PREV_ROW;
            break;
        case View.FOCUS_UP:
            movement = PREV_ITEM;
            break;
        case View.FOCUS_DOWN:
            movement = NEXT_ITEM;
            break;
        }
    }

    return movement;
}

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

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;/*from ww w  . j av a 2  s .  c om*/
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                .getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                    .getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view "
                    + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (mOrientation == HORIZONTAL) {
        if (nextFocused != null && nextFocused != currentFocused) {
            if (direction == View.FOCUS_LEFT) {
                // If there is nothing to the left, or this is causing us to
                // jump to the right, then what we really want to do is page left.
                final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
                final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
                if (currentFocused != null && nextLeft >= currLeft) {
                    handled = pageLeft();
                } else {
                    handled = nextFocused.requestFocus();
                }
            } else if (direction == View.FOCUS_RIGHT) {
                // If there is nothing to the right, or this is causing us to
                // jump to the left, then what we really want to do is page right.
                final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
                final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
                if (currentFocused != null && nextLeft <= currLeft) {
                    handled = pageRight();
                } else {
                    handled = nextFocused.requestFocus();
                }
            }
        } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
            // Trying to move left and nothing there; try to page.
            handled = pageLeft();
        } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
            // Trying to move right and nothing there; try to page.
            handled = pageRight();
        }
    } else {
        if (nextFocused != null && nextFocused != currentFocused) {
            if (direction == View.FOCUS_UP) {
                // If there is nothing to the left, or this is causing us to
                // jump to the right, then what we really want to do is page left.
                final int nextUp = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
                final int currUp = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;

                if (currentFocused != null && nextUp >= currUp) {
                    handled = pageUp();
                } else {
                    handled = nextFocused.requestFocus();
                }
            } else if (direction == View.FOCUS_DOWN) {
                // If there is nothing to the right, or this is causing us to
                // jump to the left, then what we really want to do is page right.
                final int nextDown = getChildRectInPagerCoordinates(mTempRect, nextFocused).bottom;
                final int currDown = getChildRectInPagerCoordinates(mTempRect, currentFocused).bottom;
                if (currentFocused != null && nextDown <= currDown) {
                    handled = pageDown();
                } else {
                    handled = nextFocused.requestFocus();
                }
            }
        } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
            // Trying to move left and nothing there; try to page.
            handled = pageUp();
        } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
            // Trying to move right and nothing there; try to page.
            handled = pageDown();
        }
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}