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:com.missionhub.ui.widget.LockableViewPager.java

@Override
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;/*from  w w  w .  j  a  v a2s  . c o  m*/

    boolean locked = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            if (currentFocused != null && nextFocused.getLeft() >= currentFocused.getLeft()) {
                if (mLock == LOCK_BOTH || mLock == LOCK_BACKWARD) {
                    locked = true;
                }
            }
        } else if (direction == View.FOCUS_RIGHT) {
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                if (mLock == LOCK_BOTH || mLock == LOCK_FORWARD) {
                    locked = true;
                }
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        if (mLock == LOCK_BOTH || mLock == LOCK_BACKWARD) {
            locked = true;
        }
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        if (mLock == LOCK_BOTH || mLock == LOCK_FORWARD) {
            locked = true;
        }
    }

    return locked || super.arrowScroll(direction);
}

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

@Override
public View focusSearch(View focused, int direction) {
    // Step.1 search focus by onInterceptFocusSearch
    View result = getLayoutManager().onInterceptFocusSearch(focused, direction);
    if (result != null) {
        return result;
    }//from w  w w. j  a va 2 s .c  o m
    // Step.2 search focus by FocusFinder
    final FocusFinder ff = FocusFinder.getInstance();
    result = ff.findNextFocus(this, focused, direction);
    if (result != null) {
        return result;
    }
    // Step.3 search focus by onFocusSearchFailed
    if (getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager layoutManager = (GridLayoutManager) getLayoutManager();
        if (layoutManager.ensureRecyclerState()) {
            result = layoutManager.onFocusSearchFailed(focused, direction, layoutManager.getRecycler(),
                    layoutManager.getState());
            if (result != null) {
                return result;
            }
        }
    }

    if (direction == FOCUS_DOWN) {
        final int position = getLayoutManager().getPosition(focused);
        if (position < getLayoutManager().getItemCount() - 1) {
            forceRequestLayout();
        }
    }

    return null;
}

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

/**
 * You must override this method if RecyclerView need to page loading and you need to handle
 * the problem of Focus jitter//from  w w w. ja  va2 s  . c om
 *
 * @param focused   focused The view that currently has focus
 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and
 *                  FOCUS_RIGHT, or 0 for not applicable.
 * @return the result of focusSearch
 */
@Override
public View focusSearch(View focused, int direction) {
    // Step.1 search focus by onInterceptFocusSearch
    View result = getLayoutManager().onInterceptFocusSearch(focused, direction);
    if (result != null) {
        return result;
    }
    // Step.2 search focus by FocusFinder
    final FocusFinder ff = FocusFinder.getInstance();
    result = ff.findNextFocus(this, focused, direction);
    if (result != null) {
        return result;
    }
    // Step.3 search focus by onFocusSearchFailed
    if (getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager layoutManager = (GridLayoutManager) getLayoutManager();
        if (layoutManager.ensureRecyclerState()) {
            result = layoutManager.onFocusSearchFailed(focused, direction, layoutManager.getRecycler(),
                    layoutManager.getState());
            if (result != null) {
                return result;
            }
        }
    }

    if (direction == FOCUS_RIGHT) {
        final int position = getLayoutManager().getPosition(focused);
        if (position < getLayoutManager().getItemCount() - 1) {
            forceRequestLayout();
        }
    }

    return null;
}

From source file:com.hippo.widget.BothScrollView.java

/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy.//from w w  w  .  ja  v  a  2  s. co m
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    mTempRect.setEmpty();

    if (!canScrollHorizontally()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this) {
                currentFocused = null;
            }
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_RIGHT);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_RIGHT);
        }
        return false;
    }

    if (!canScrollVertically()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this) {
                currentFocused = null;
            }
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }

    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (!event.isAltPressed()) {
                handled = arrowScrollHorizontally(View.FOCUS_LEFT);
            } else {
                handled = fullScroll(View.FOCUS_LEFT);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (!event.isAltPressed()) {
                handled = arrowScrollHorizontally(View.FOCUS_RIGHT);
            } else {
                handled = fullScroll(View.FOCUS_RIGHT);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            if (!event.isAltPressed()) {
                handled = arrowScrollVertically(View.FOCUS_UP);
            } else {
                handled = fullScroll(View.FOCUS_UP);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (!event.isAltPressed()) {
                handled = arrowScrollVertically(View.FOCUS_DOWN);
            } else {
                handled = fullScroll(View.FOCUS_DOWN);
            }
            break;
        case KeyEvent.KEYCODE_SPACE:
            if (event.isCtrlPressed()) {
                pageScroll(event.isShiftPressed() ? View.FOCUS_LEFT : View.FOCUS_RIGHT);
            } else {
                pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
            }
            break;
        }
    }

    return handled;
}

From source file:com.hxqc.mall.thirdshop.views.CustomScrollView.java

/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy./*from w w w  . j  av a  2 s.co  m*/
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    mTempRect.setEmpty();

    if (!canScroll()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this)
                currentFocused = null;
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }

    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_UP:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_UP);
            } else {
                handled = fullScroll(View.FOCUS_UP);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_DOWN);
            } else {
                handled = fullScroll(View.FOCUS_DOWN);
            }
            break;
        case KeyEvent.KEYCODE_SPACE:
            pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
            break;
        default:
            break;
        }
    }

    return handled;
}

From source file:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy./*from www  .java  2s  .  c o  m*/
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    mTempRect.setEmpty();

    if (!canScroll()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this)
                currentFocused = null;
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }

    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_UP:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_UP);
            } else {
                handled = fullScroll(View.FOCUS_UP);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_DOWN);
            } else {
                handled = fullScroll(View.FOCUS_DOWN);
            }
            break;
        case KeyEvent.KEYCODE_SPACE:
            pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
            break;
        }
    }

    return handled;
}

From source file:com.peerless2012.twowaynestedscrollview.TwoWayNestedScrollView.java

/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy./*from   w  w  w  . j a va  2  s  . c o m*/
 *
 * @param event
 *            The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    mTempRect.setEmpty();

    if (!canVerticalScroll()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this)
                currentFocused = null;
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }
    if (!canHorizontalScroll()) {
        if (isFocused() && event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            View currentFocused = findFocus();
            if (currentFocused == this)
                currentFocused = null;
            View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
            return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
        }
        return false;
    }

    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_UP:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_UP);
            } else {
                handled = fullScroll(View.FOCUS_UP);
            }
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (!event.isAltPressed()) {
                handled = arrowScroll(View.FOCUS_DOWN);
            } else {
                handled = fullScroll(View.FOCUS_DOWN);
            }
            break;
        case KeyEvent.KEYCODE_SPACE:
            pageScroll(event.isShiftPressed() ? View.FOCUS_UP : View.FOCUS_DOWN);
            break;
        }
    }

    return handled;
}

From source file:com.android.launcher3.folder.Folder.java

@Override
public View focusSearch(int direction) {
    // When the folder is focused, further focus search should be within the folder contents.
    return FocusFinder.getInstance().findNextFocus(this, null, direction);
}

From source file:com.owen.tvrecyclerview.widget.TvRecyclerView.java

/**
 * ??view//w  ww .j a  va 2 s.com
 * @param direction
 * @return
 */
private View findNextFocus(int direction) {
    return FocusFinder.getInstance().findNextFocus(this, getFocusedChild(), direction);
}

From source file:gaobq.android.easyslidingmenu.CustomViewAbove.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this)
        currentFocused = null;//w w w .j  ava2s .co m

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            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.
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                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;
}