Example usage for android.view FocusFinder getInstance

List of usage examples for android.view FocusFinder getInstance

Introduction

In this page you can find the example usage for android.view FocusFinder getInstance.

Prototype

public static FocusFinder getInstance() 

Source Link

Document

Get the focus finder for this thread.

Usage

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

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;//  w w w .  j a  v a  2s.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:com.viewpagerindicator.MyDirectionalViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;/*from   w  w  w.j  av a 2s . 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 = 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:android.improving.utils.views.cardsview.OrientedViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;/*from  w  ww . j a v a2s  .  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_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.
            if (mOrientation == Orientation.VERTICAL) {
                final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
                final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
                if (currentFocused != null && nextTop >= currTop) {
                    handled = pageBack();
                } else {
                    handled = nextFocused.requestFocus();
                }
            } else {
                final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
                final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
                if (currentFocused != null && nextLeft >= currLeft) {
                    handled = pageBack();
                } 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.
            if (mOrientation == Orientation.VERTICAL) {
                final int nextDown = getChildRectInPagerCoordinates(mTempRect, nextFocused).bottom;
                final int currDown = getChildRectInPagerCoordinates(mTempRect, currentFocused).bottom;
                if (currentFocused != null && nextDown <= currDown) {
                    handled = pageForward();
                } else {
                    handled = nextFocused.requestFocus();
                }
            } else {
                final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
                final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
                if (currentFocused != null && nextLeft <= currLeft) {
                    handled = pageForward();
                } else {
                    handled = nextFocused.requestFocus();
                }
            }
        }
    } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageBack();
    } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageForward();
    }
    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;/*  ww  w.java 2s.com*/
    } 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:dev.dworks.libs.widget.ViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;/* w  w  w.  j a  v a 2 s .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 (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;
}