Android TV 포커스 recycleview에는 여러 포커스 가져오기 컨트롤이 포함되어 있으며, 위아래로 미끄러질 때 포커스가 흐트러집니다.

3104 단어
LinearLayoutManager에서 onFocusSearchFailed를 덮어쓰는 방법이 필요합니다. 현재 화면을 초과할 때 특수 처리를 합니다.
코드는 다음과 같습니다.
package com.**********.utils;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

public class FocusFixedLinearLayoutManagerextends  extends android.support.v7.widget.LinearLayoutManager {
    public FocusFixedLinearLayoutManagerextends(Context context) {
        super(context);
    }

    public FocusFixedLinearLayoutManagerextends(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public FocusFixedLinearLayoutManagerextends(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state) {
        // Need to be called in order to layout new row/column
        View nextFocus = super.onFocusSearchFailed(focused, focusDirection, recycler, state);

        if (nextFocus == null) {
            return null;
        }
        /**
         *          
         */
        int fromPos = getPosition(getFocusedChild());
        /**
         *                
         */
        int nextPos = getNextViewPos(fromPos, focusDirection);

        return findViewByPosition(nextPos);
    }

    private int getNextViewPos(int fromPos, int direction) {
        int offset = getOffset(direction);
        return hitBorder(fromPos, offset) ? fromPos : fromPos + offset;
    }

    private int getOffset(int direction) {
        int spanCount = getSpanCount();
        int orientation = getOrientation();

        if (orientation == VERTICAL) {
            switch (direction) {
                case View.FOCUS_DOWN:
                    return spanCount;
                case View.FOCUS_UP:
                    return -spanCount;
                case View.FOCUS_RIGHT:
                    return 1;
                case View.FOCUS_LEFT:
                    return -1;
            }
        } else if (orientation == HORIZONTAL) {
            switch (direction) {
                case View.FOCUS_DOWN:
                    return 1;
                case View.FOCUS_UP:
                    return -1;
                case View.FOCUS_RIGHT:
                    return spanCount;
                case View.FOCUS_LEFT:
                    return -spanCount;
            }
        }

        return 0;
    }

    private int getSpanCount() {
        return 1;
    }

    private boolean hitBorder(int from, int offset) {
        int spanCount = getSpanCount();

        if (Math.abs(offset) == 1) {
            int spanIndex = from % spanCount;
            int newSpanIndex = spanIndex + offset;
            return newSpanIndex < 0 || newSpanIndex >= spanCount;
        } else {
            int newPos = from + offset;
            return newPos < 0 && newPos >= spanCount;
        }
    }
}

좋은 웹페이지 즐겨찾기