Java Code Examples for android.view.View.isFocusable()

The following are Jave code examples for showing how to use isFocusable() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: FlickLauncher   File: FocusLogic.java   Source Code and License Vote up 7 votes
/**
 * Returns a matrix of size same as the {@link CellLayout} dimension that is initialized with the
 * index of the child view.
 */
// TODO: get rid of the dynamic matrix creation
public static int[][] createSparseMatrix(CellLayout layout) {
    ShortcutAndWidgetContainer parent = layout.getShortcutsAndWidgets();
    final int m = layout.getCountX();
    final int n = layout.getCountY();
    final boolean invert = parent.invertLayoutHorizontally();

    int[][] matrix = createFullMatrix(m, n);

    // Iterate thru the children.
    for (int i = 0; i < parent.getChildCount(); i++ ) {
        View cell = parent.getChildAt(i);
        if (!cell.isFocusable()) {
            continue;
        }
        int cx = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellX;
        int cy = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellY;
        matrix[invert ? (m - cx - 1) : cx][cy] = i;
    }
    if (DEBUG) {
        printMatrix(matrix);
    }
    return matrix;
}
 
Example 2
Project: vlayout   File: BaseLayoutHelper.java   Source Code and License Vote up 7 votes
protected void handleStateOnResult(LayoutChunkResult result, View view) {
    if (view == null) {
        return;
    }

    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

    // Consume the available space if the view is not removed OR changed
    if (params.isItemRemoved() || params.isItemChanged()) {
        result.mIgnoreConsumed = true;
    }

    // used when search a focusable view
    result.mFocusable = result.mFocusable || view.isFocusable();

}
 
Example 3
Project: SimpleUILauncher   File: FocusLogic.java   Source Code and License Vote up 7 votes
/**
 * Returns a matrix of size same as the {@link CellLayout} dimension that is initialized with the
 * index of the child view.
 */
// TODO: get rid of the dynamic matrix creation
public static int[][] createSparseMatrix(CellLayout layout) {
    ShortcutAndWidgetContainer parent = layout.getShortcutsAndWidgets();
    final int m = layout.getCountX();
    final int n = layout.getCountY();
    final boolean invert = parent.invertLayoutHorizontally();

    int[][] matrix = createFullMatrix(m, n);

    // Iterate thru the children.
    for (int i = 0; i < parent.getChildCount(); i++ ) {
        View cell = parent.getChildAt(i);
        if (!cell.isFocusable()) {
            continue;
        }
        int cx = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellX;
        int cy = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellY;
        matrix[invert ? (m - cx - 1) : cx][cy] = i;
    }
    if (DEBUG) {
        printMatrix(matrix);
    }
    return matrix;
}
 
Example 4
Project: GitHub   File: BaseLayoutHelper.java   Source Code and License Vote up 6 votes
/**
 * Helper methods to handle focus states for views
 * FIXME 可变参数性能不好,会引起一次潜在的数组对象创建,在频繁滑动过程中,容易引起GC,如果只有一个View,建议调用上述方法
 * @param result
 * @param views
 */
protected void handleStateOnResult(LayoutChunkResult result, View... views) {
    if (views == null) return;

    for (View view : views) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

        // Consume the available space if the view is not removed OR changed
        if (params.isItemRemoved() || params.isItemChanged()) {
            result.mIgnoreConsumed = true;
        }

        // used when search a focusable view
        result.mFocusable = result.mFocusable || view.isFocusable();

        if (result.mFocusable && result.mIgnoreConsumed) {
            break;
        }
    }
}
 
Example 5
Project: Ghost-Android   File: KeyboardUtils.java   Source Code and License Vote up 6 votes
/**
 * Focus the given view and show the soft keyboard.
 * @param activity the current activity
 * @param view the view to focus
 */
public static void focusAndShowKeyboard(@Nullable Activity activity, @NonNull View view) {
    if (activity == null) return;
    if (view.isFocusable()) {
        view.requestFocus();
    }
    if (view instanceof EditText) {
        showKeyboard(activity);
    }
}
 
Example 6
Project: GitHub   File: BaseLayoutHelper.java   Source Code and License Vote up 6 votes
protected void handleStateOnResult(LayoutChunkResult result, View view) {
    if (view == null) {
        return;
    }

    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

    // Consume the available space if the view is not removed OR changed
    if (params.isItemRemoved() || params.isItemChanged()) {
        result.mIgnoreConsumed = true;
    }

    // used when search a focusable view
    result.mFocusable = result.mFocusable || view.isFocusable();

}
 
Example 7
Project: FlickLauncher   File: FocusHelper.java   Source Code and License Vote up 6 votes
private static View getFirstFocusableIconInReverseReadingOrder(CellLayout cellLayout,
        boolean isRtl) {
    View icon;
    int countX = cellLayout.getCountX();
    for (int y = cellLayout.getCountY() - 1; y >= 0; y--) {
        int increment = isRtl ? 1 : -1;
        for (int x = isRtl ? 0 : countX - 1; 0 <= x && x < countX; x += increment) {
            if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
                return icon;
            }
        }
    }
    return null;
}
 
Example 8
Project: FlickLauncher   File: FocusHelper.java   Source Code and License Vote up 6 votes
private static View getFirstFocusableIconInReadingOrder(CellLayout cellLayout, boolean isRtl) {
    View icon;
    int countX = cellLayout.getCountX();
    for (int y = 0; y < cellLayout.getCountY(); y++) {
        int increment = isRtl ? -1 : 1;
        for (int x = isRtl ? countX - 1 : 0; 0 <= x && x < countX; x += increment) {
            if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
                return icon;
            }
        }
    }
    return null;
}
 
Example 9
Project: LaunchEnr   File: FocusHelper.java   Source Code and License Vote up 6 votes
private static View getFirstFocusableIconInReverseReadingOrder(CellLayout cellLayout,
        boolean isRtl) {
    View icon;
    int countX = cellLayout.getCountX();
    for (int y = cellLayout.getCountY() - 1; y >= 0; y--) {
        int increment = isRtl ? 1 : -1;
        for (int x = isRtl ? 0 : countX - 1; 0 <= x && x < countX; x += increment) {
            if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
                return icon;
            }
        }
    }
    return null;
}
 
Example 10
Project: LaunchEnr   File: FocusLogic.java   Source Code and License Vote up 6 votes
/**
 * Creates a sparse matrix that merges the icon of previous/next page and last column of
 * current page. When left key is triggered on the leftmost column, sparse matrix is created
 * that combines previous page matrix and an extra column on the right. Likewise, when right
 * key is triggered on the rightmost column, sparse matrix is created that combines this column
 * on the 0th column and the next page matrix.
 *
 * @param pivotX    x coordinate of the focused item in the current page
 * @param pivotY    y coordinate of the focused item in the current page
 */
// TODO: get rid of the dynamic matrix creation
public static int[][] createSparseMatrixWithPivotColumn(CellLayout iconLayout,
        int pivotX, int pivotY) {

    ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();

    int[][] matrix = createFullMatrix(iconLayout.getCountX() + 1, iconLayout.getCountY());

    // Iterate thru the children of the top parent.
    for (int i = 0; i < iconParent.getChildCount(); i++) {
        View cell = iconParent.getChildAt(i);
        if (!cell.isFocusable()) {
            continue;
        }
        int cx = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellX;
        int cy = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellY;
        if (pivotX < 0) {
            matrix[cx - pivotX][cy] = i;
        } else {
            matrix[cx][cy] = i;
        }
    }

    if (pivotX < 0) {
        matrix[0][pivotY] = PIVOT;
    } else {
        matrix[pivotX][pivotY] = PIVOT;
    }
    return matrix;
}
 
Example 11
Project: CalcMate   File: CalculatorEditText.java   Source Code and License Vote up 6 votes
@Override
public View focusSearch(int direction) {
    switch(direction) {
    case View.FOCUS_FORWARD:
        View v = mDisplay.nextView(this);
        while(!v.isFocusable())
            v = mDisplay.nextView(v);
        return v;
    }
    return super.focusSearch(direction);
}
 
Example 12
Project: firefox-tv   File: ViewUtils.java   Source Code and License Vote up 5 votes
@Override
public void run() {
    if (tries <= 0) {
        return;
    }

    final View view = viewReferemce.get();
    if (view == null) {
        // The view is gone. No need to continue.
        return;
    }

    if (!view.isFocusable() || !view.isFocusableInTouchMode()) {
        // The view is not focusable - we can't show the keyboard for it.
        return;
    }

    if (!view.requestFocus()) {
        // Focus this view first.
        post();
        return;
    }

    final Activity activity = (Activity) view.getContext();
    if (activity == null) {
        return;
    }

    final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm == null) {
        return;
    }

    if (!imm.isActive(view)) {
        // This view is not the currently active view for the input method yet.
        post();
        return;
    }

    if (!imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)) {
        // Showing they keyboard failed. Try again later.
        post();
    }
}
 
Example 13
Project: GitHub   File: ExposeLinearLayoutManagerEx.java   Source Code and License Vote up 5 votes
@Override
public View onFocusSearchFailed(View focused, int focusDirection,
                                RecyclerView.Recycler recycler, RecyclerView.State state) {
    myResolveShouldLayoutReverse();
    if (getChildCount() == 0) {
        return null;
    }

    final int layoutDir = convertFocusDirectionToLayoutDirectionExpose(focusDirection);
    if (layoutDir == LayoutState.INVALID_LAYOUT) {
        return null;
    }

    View referenceChild = null;
    if (layoutDir == LayoutState.LAYOUT_START) {
        referenceChild = myFindReferenceChildClosestToStart(state);
    } else {
        referenceChild = myFindReferenceChildClosestToEnd(state);

    }

    if (referenceChild == null) {
        if (DEBUG) {
            Log.d(TAG,
                    "Cannot find a child with a valid position to be used for focus search.");
        }
        return null;
    }
    ensureLayoutStateExpose();
    final int maxScroll = (int) (MAX_SCROLL_FACTOR * mOrientationHelper.getTotalSpace());
    updateLayoutStateExpose(layoutDir, maxScroll, false, state);
    mLayoutState.mScrollingOffset = LayoutState.SCOLLING_OFFSET_NaN;
    mLayoutState.mRecycle = false;
    mLayoutState.mOnRefresLayout = false;
    fill(recycler, mLayoutState, state, true);
    final View nextFocus;
    if (layoutDir == LayoutState.LAYOUT_START) {
        nextFocus = getChildClosestToStartExpose();
    } else {
        nextFocus = getChildClosestToEndExpose();
    }
    if (nextFocus == referenceChild || !nextFocus.isFocusable()) {
        return null;
    }
    return nextFocus;
}
 
Example 14
Project: PlusGram   File: LinearLayoutManager.java   Source Code and License Vote up 5 votes
void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State state,
        LayoutState layoutState, LayoutChunkResult result) {
    View view = layoutState.next(recycler);
    if (view == null) {
        if (DEBUG && layoutState.mScrapList == null) {
            throw new RuntimeException("received null view when unexpected");
        }
        // if we are laying out views in scrap, this may return null which means there is
        // no more items to layout.
        result.mFinished = true;
        return;
    }
    LayoutParams params = (LayoutParams) view.getLayoutParams();
    if (layoutState.mScrapList == null) {
        if (mShouldReverseLayout == (layoutState.mLayoutDirection
                == LayoutState.LAYOUT_START)) {
            addView(view);
        } else {
            addView(view, 0);
        }
    } else {
        if (mShouldReverseLayout == (layoutState.mLayoutDirection
                == LayoutState.LAYOUT_START)) {
            addDisappearingView(view);
        } else {
            addDisappearingView(view, 0);
        }
    }
    measureChildWithMargins(view, 0, 0);
    result.mConsumed = mOrientationHelper.getDecoratedMeasurement(view);
    int left, top, right, bottom;
    if (mOrientation == VERTICAL) {
        if (isLayoutRTL()) {
            right = getWidth() - getPaddingRight();
            left = right - mOrientationHelper.getDecoratedMeasurementInOther(view);
        } else {
            left = getPaddingLeft();
            right = left + mOrientationHelper.getDecoratedMeasurementInOther(view);
        }
        if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) {
            bottom = layoutState.mOffset;
            top = layoutState.mOffset - result.mConsumed;
        } else {
            top = layoutState.mOffset;
            bottom = layoutState.mOffset + result.mConsumed;
        }
    } else {
        top = getPaddingTop();
        bottom = top + mOrientationHelper.getDecoratedMeasurementInOther(view);

        if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) {
            right = layoutState.mOffset;
            left = layoutState.mOffset - result.mConsumed;
        } else {
            left = layoutState.mOffset;
            right = layoutState.mOffset + result.mConsumed;
        }
    }
    // We calculate everything with View's bounding box (which includes decor and margins)
    // To calculate correct layout position, we subtract margins.
    layoutDecorated(view, left + params.leftMargin, top + params.topMargin,
            right - params.rightMargin, bottom - params.bottomMargin);
    if (DEBUG) {
        Log.d(TAG, "laid out child at position " + getPosition(view) + ", with l:"
                + (left + params.leftMargin) + ", t:" + (top + params.topMargin) + ", r:"
                + (right - params.rightMargin) + ", b:" + (bottom - params.bottomMargin));
    }
    // Consume the available space if the view is not removed OR changed
    if (params.isItemRemoved() || params.isItemChanged()) {
        result.mIgnoreConsumed = true;
    }
    result.mFocusable = view.isFocusable();
}
 
Example 15
Project: GitHub   File: ExposeLinearLayoutManagerEx.java   Source Code and License Vote up 5 votes
@Override
public View onFocusSearchFailed(View focused, int focusDirection,
                                RecyclerView.Recycler recycler, RecyclerView.State state) {
    myResolveShouldLayoutReverse();
    if (getChildCount() == 0) {
        return null;
    }

    final int layoutDir = convertFocusDirectionToLayoutDirectionExpose(focusDirection);
    if (layoutDir == LayoutState.INVALID_LAYOUT) {
        return null;
    }

    View referenceChild = null;
    if (layoutDir == LayoutState.LAYOUT_START) {
        referenceChild = myFindReferenceChildClosestToStart(state);
    } else {
        referenceChild = myFindReferenceChildClosestToEnd(state);

    }

    if (referenceChild == null) {
        if (DEBUG) {
            Log.d(TAG,
                    "Cannot find a child with a valid position to be used for focus search.");
        }
        return null;
    }
    ensureLayoutStateExpose();
    final int maxScroll = (int) (MAX_SCROLL_FACTOR * mOrientationHelper.getTotalSpace());
    updateLayoutStateExpose(layoutDir, maxScroll, false, state);
    mLayoutState.mScrollingOffset = LayoutState.SCOLLING_OFFSET_NaN;
    mLayoutState.mRecycle = false;
    mLayoutState.mOnRefresLayout = false;
    fill(recycler, mLayoutState, state, true);
    final View nextFocus;
    if (layoutDir == LayoutState.LAYOUT_START) {
        nextFocus = getChildClosestToStartExpose();
    } else {
        nextFocus = getChildClosestToEndExpose();
    }
    if (nextFocus == referenceChild || !nextFocus.isFocusable()) {
        return null;
    }
    return nextFocus;
}
 
Example 16
Project: ShortcutMenu   File: StrongGridLayoutManager.java   Source Code and License Vote up 5 votes
@Override
public View onFocusSearchFailed(View focused, int focusDirection,
                                RecyclerView.Recycler recycler, RecyclerView.State state) {
    View prevFocusedChild = findContainingItemView(focused);
    if (prevFocusedChild == null) {
        return null;
    }
    StrongGridLayoutManager.LayoutParams lp = (StrongGridLayoutManager.LayoutParams) prevFocusedChild.getLayoutParams();
    final int prevSpanStart = lp.mSpanIndex;
    final int prevSpanEnd = lp.mSpanIndex + lp.mSpanSize;
    View view = super.onFocusSearchFailed(focused, focusDirection, recycler, state);
    if (view == null) {
        return null;
    }
    // LinearLayoutManager finds the last child. What we want is the child which has the same
    // spanIndex.
    final int layoutDir = convertFocusDirectionToLayoutDirection(focusDirection);
    final boolean ascend = (layoutDir == LayoutState.LAYOUT_END) != mShouldReverseLayout;
    final int start, inc, limit;
    if (ascend) {
        start = getChildCount() - 1;
        inc = -1;
        limit = -1;
    } else {
        start = 0;
        inc = 1;
        limit = getChildCount();
    }
    final boolean preferLastSpan = mOrientation == VERTICAL && isLayoutRTL();
    View weakCandidate = null; // somewhat matches but not strong
    int weakCandidateSpanIndex = -1;
    int weakCandidateOverlap = 0; // how many spans overlap

    for (int i = start; i != limit; i += inc) {
        View candidate = getChildAt(i);
        if (candidate == prevFocusedChild) {
            break;
        }
        if (!candidate.isFocusable()) {
            continue;
        }
        final StrongGridLayoutManager.LayoutParams candidateLp = (StrongGridLayoutManager.LayoutParams) candidate.getLayoutParams();
        final int candidateStart = candidateLp.mSpanIndex;
        final int candidateEnd = candidateLp.mSpanIndex + candidateLp.mSpanSize;
        if (candidateStart == prevSpanStart && candidateEnd == prevSpanEnd) {
            return candidate; // perfect match
        }
        boolean assignAsWeek = false;
        if (weakCandidate == null) {
            assignAsWeek = true;
        } else {
            int maxStart = Math.max(candidateStart, prevSpanStart);
            int minEnd = Math.min(candidateEnd, prevSpanEnd);
            int overlap = minEnd - maxStart;
            if (overlap > weakCandidateOverlap) {
                assignAsWeek = true;
            } else if (overlap == weakCandidateOverlap &&
                    preferLastSpan == (candidateStart > weakCandidateSpanIndex)) {
                assignAsWeek = true;
            }
        }

        if (assignAsWeek) {
            weakCandidate = candidate;
            weakCandidateSpanIndex = candidateLp.mSpanIndex;
            weakCandidateOverlap = Math.min(candidateEnd, prevSpanEnd) -
                    Math.max(candidateStart, prevSpanStart);
        }
    }
    return weakCandidate;
}
 
Example 17
Project: boohee_v5.6   File: LinearLayoutManager.java   Source Code and License Vote up 5 votes
void layoutChunk(Recycler recycler, State state, LayoutState layoutState, LayoutChunkResult result) {
    View view = layoutState.next(recycler);
    if (view == null) {
        result.mFinished = true;
        return;
    }
    int right;
    int left;
    int bottom;
    int top;
    LayoutParams params = (LayoutParams) view.getLayoutParams();
    if (layoutState.mScrapList == null) {
        if (this.mShouldReverseLayout == (layoutState.mLayoutDirection == -1)) {
            addView(view);
        } else {
            addView(view, 0);
        }
    } else {
        if (this.mShouldReverseLayout == (layoutState.mLayoutDirection == -1)) {
            addDisappearingView(view);
        } else {
            addDisappearingView(view, 0);
        }
    }
    measureChildWithMargins(view, 0, 0);
    result.mConsumed = this.mOrientationHelper.getDecoratedMeasurement(view);
    if (this.mOrientation == 1) {
        if (isLayoutRTL()) {
            right = getWidth() - getPaddingRight();
            left = right - this.mOrientationHelper.getDecoratedMeasurementInOther(view);
        } else {
            left = getPaddingLeft();
            right = left + this.mOrientationHelper.getDecoratedMeasurementInOther(view);
        }
        if (layoutState.mLayoutDirection == -1) {
            bottom = layoutState.mOffset;
            top = layoutState.mOffset - result.mConsumed;
        } else {
            top = layoutState.mOffset;
            bottom = layoutState.mOffset + result.mConsumed;
        }
    } else {
        top = getPaddingTop();
        bottom = top + this.mOrientationHelper.getDecoratedMeasurementInOther(view);
        if (layoutState.mLayoutDirection == -1) {
            right = layoutState.mOffset;
            left = layoutState.mOffset - result.mConsumed;
        } else {
            left = layoutState.mOffset;
            right = layoutState.mOffset + result.mConsumed;
        }
    }
    layoutDecorated(view, left + params.leftMargin, top + params.topMargin, right - params.rightMargin, bottom - params.bottomMargin);
    if (params.isItemRemoved() || params.isItemChanged()) {
        result.mIgnoreConsumed = true;
    }
    result.mFocusable = view.isFocusable();
}
 
Example 18
Project: PlusGram   File: GridLayoutManager.java   Source Code and License Vote up 5 votes
@Override
public View onFocusSearchFailed(View focused, int focusDirection,
        RecyclerView.Recycler recycler, RecyclerView.State state) {
    View prevFocusedChild = findContainingItemView(focused);
    if (prevFocusedChild == null) {
        return null;
    }
    LayoutParams lp = (LayoutParams) prevFocusedChild.getLayoutParams();
    final int prevSpanStart = lp.mSpanIndex;
    final int prevSpanEnd = lp.mSpanIndex + lp.mSpanSize;
    View view = super.onFocusSearchFailed(focused, focusDirection, recycler, state);
    if (view == null) {
        return null;
    }
    // LinearLayoutManager finds the last child. What we want is the child which has the same
    // spanIndex.
    final int layoutDir = convertFocusDirectionToLayoutDirection(focusDirection);
    final boolean ascend = (layoutDir == LayoutState.LAYOUT_END) != mShouldReverseLayout;
    final int start, inc, limit;
    if (ascend) {
        start = getChildCount() - 1;
        inc = -1;
        limit = -1;
    } else {
        start = 0;
        inc = 1;
        limit = getChildCount();
    }
    final boolean preferLastSpan = mOrientation == VERTICAL && isLayoutRTL();
    View weakCandidate = null; // somewhat matches but not strong
    int weakCandidateSpanIndex = -1;
    int weakCandidateOverlap = 0; // how many spans overlap

    for (int i = start; i != limit; i += inc) {
        View candidate = getChildAt(i);
        if (candidate == prevFocusedChild) {
            break;
        }
        if (!candidate.isFocusable()) {
            continue;
        }
        final LayoutParams candidateLp = (LayoutParams) candidate.getLayoutParams();
        final int candidateStart = candidateLp.mSpanIndex;
        final int candidateEnd = candidateLp.mSpanIndex + candidateLp.mSpanSize;
        if (candidateStart == prevSpanStart && candidateEnd == prevSpanEnd) {
            return candidate; // perfect match
        }
        boolean assignAsWeek = false;
        if (weakCandidate == null) {
            assignAsWeek = true;
        } else {
            int maxStart = Math.max(candidateStart, prevSpanStart);
            int minEnd = Math.min(candidateEnd, prevSpanEnd);
            int overlap = minEnd - maxStart;
            if (overlap > weakCandidateOverlap) {
                assignAsWeek = true;
            } else if (overlap == weakCandidateOverlap &&
                    preferLastSpan == (candidateStart > weakCandidateSpanIndex)) {
                assignAsWeek = true;
            }
        }

        if (assignAsWeek) {
            weakCandidate = candidate;
            weakCandidateSpanIndex = candidateLp.mSpanIndex;
            weakCandidateOverlap = Math.min(candidateEnd, prevSpanEnd) -
                    Math.max(candidateStart, prevSpanStart);
        }
    }
    return weakCandidate;
}
 
Example 19
Project: vlayout   File: ExposeLinearLayoutManagerEx.java   Source Code and License Vote up 5 votes
@Override
public View onFocusSearchFailed(View focused, int focusDirection,
                                RecyclerView.Recycler recycler, RecyclerView.State state) {
    myResolveShouldLayoutReverse();
    if (getChildCount() == 0) {
        return null;
    }

    final int layoutDir = convertFocusDirectionToLayoutDirectionExpose(focusDirection);
    if (layoutDir == LayoutState.INVALID_LAYOUT) {
        return null;
    }

    View referenceChild = null;
    if (layoutDir == LayoutState.LAYOUT_START) {
        referenceChild = myFindReferenceChildClosestToStart(state);
    } else {
        referenceChild = myFindReferenceChildClosestToEnd(state);

    }

    if (referenceChild == null) {
        if (DEBUG) {
            Log.d(TAG,
                    "Cannot find a child with a valid position to be used for focus search.");
        }
        return null;
    }
    ensureLayoutStateExpose();
    final int maxScroll = (int) (MAX_SCROLL_FACTOR * mOrientationHelper.getTotalSpace());
    updateLayoutStateExpose(layoutDir, maxScroll, false, state);
    mLayoutState.mScrollingOffset = LayoutState.SCOLLING_OFFSET_NaN;
    mLayoutState.mRecycle = false;
    mLayoutState.mOnRefresLayout = false;
    fill(recycler, mLayoutState, state, true);
    final View nextFocus;
    if (layoutDir == LayoutState.LAYOUT_START) {
        nextFocus = getChildClosestToStartExpose();
    } else {
        nextFocus = getChildClosestToEndExpose();
    }
    if (nextFocus == referenceChild || !nextFocus.isFocusable()) {
        return null;
    }
    return nextFocus;
}
 
Example 20
Project: letv   File: GridLayoutManager.java   Source Code and License Vote up 5 votes
public View onFocusSearchFailed(View focused, int focusDirection, Recycler recycler, State state) {
    View prevFocusedChild = findContainingItemView(focused);
    if (prevFocusedChild == null) {
        return null;
    }
    LayoutParams lp = (LayoutParams) prevFocusedChild.getLayoutParams();
    int prevSpanStart = lp.mSpanIndex;
    int prevSpanEnd = lp.mSpanIndex + lp.mSpanSize;
    if (super.onFocusSearchFailed(focused, focusDirection, recycler, state) == null) {
        return null;
    }
    int start;
    int inc;
    int limit;
    if ((convertFocusDirectionToLayoutDirection(focusDirection) == 1) != this.mShouldReverseLayout) {
        start = getChildCount() - 1;
        inc = -1;
        limit = -1;
    } else {
        start = 0;
        inc = 1;
        limit = getChildCount();
    }
    boolean preferLastSpan = this.mOrientation == 1 && isLayoutRTL();
    View weakCandidate = null;
    int weakCandidateSpanIndex = -1;
    int weakCandidateOverlap = 0;
    for (int i = start; i != limit; i += inc) {
        View candidate = getChildAt(i);
        if (candidate == prevFocusedChild) {
            break;
        }
        if (candidate.isFocusable()) {
            LayoutParams candidateLp = (LayoutParams) candidate.getLayoutParams();
            int candidateStart = candidateLp.mSpanIndex;
            int candidateEnd = candidateLp.mSpanIndex + candidateLp.mSpanSize;
            if (candidateStart == prevSpanStart && candidateEnd == prevSpanEnd) {
                return candidate;
            }
            boolean assignAsWeek = false;
            if (weakCandidate == null) {
                assignAsWeek = true;
            } else {
                int overlap = Math.min(candidateEnd, prevSpanEnd) - Math.max(candidateStart, prevSpanStart);
                if (overlap > weakCandidateOverlap) {
                    assignAsWeek = true;
                } else if (overlap == weakCandidateOverlap) {
                    if (preferLastSpan == (candidateStart > weakCandidateSpanIndex)) {
                        assignAsWeek = true;
                    }
                }
            }
            if (assignAsWeek) {
                weakCandidate = candidate;
                weakCandidateSpanIndex = candidateLp.mSpanIndex;
                weakCandidateOverlap = Math.min(candidateEnd, prevSpanEnd) - Math.max(candidateStart, prevSpanStart);
            }
        }
    }
    return weakCandidate;
}