android tv常見問題(四)焦點變化時,Recyclerview是如何進行滾動的

如需轉載請評論或簡信,并注明出處,未經允許不得轉載

系列文章

github地址

https://github.com/Geekholt/TvFocus

目錄

期望結果

Recyclerview滾動時,聚焦的item位置保持在中間。


4.1.gif

實際結果

4.2.gif

問題分析

需要在計算RecyclerView滑動距離的方法中進行重寫,控制每次滑動的距離。先來看看RecyclerView原生的滑動距離計算方法。

RecyclerView#requestChildRectangleOnScreen

當RecyclerView的某個子View需要被定位在屏幕的某個矩形范圍時,調用此方法。

    /**
     * 通過該方法設置選中的item居中
     * <p>
     * 最終計算出的dy,dx的實際意義就是在滾動中上下和左右滑動的距離
     *
     * @param child     發出請求的子View
     * @param rect      子View坐標系內的矩形,即此子View希望在屏幕上的定位
     * @param immediate 設為true,則禁止動畫和平滑移動滾動條
     * @return 進行了滾動操作的這個ViewGroup,是否處理此操作
     */
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect,
        boolean immediate) {
    final int parentLeft = getPaddingLeft();
    final int parentTop = getPaddingTop();
    final int parentRight = getWidth() - getPaddingRight();
    final int parentBottom = getHeight() - getPaddingBottom();
    final int childLeft = child.getLeft() + rect.left - child.getScrollX();
    final int childTop = child.getTop() + rect.top - child.getScrollY();
    final int childRight = childLeft + rect.width();
    final int childBottom = childTop + rect.height();

    final int offScreenLeft = Math.min(0, childLeft - parentLeft);
    final int offScreenTop = Math.min(0, childTop - parentTop);
    final int offScreenRight = Math.max(0, childRight - parentRight);
    final int offScreenBottom = Math.max(0, childBottom - parentBottom);

    // Favor the "start" layout direction over the end when bringing one side or the other
    // of a large rect into view. If we decide to bring in end because start is already
    // visible, limit the scroll such that start won't go out of bounds.
    final int dx;
    if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
        dx = offScreenRight != 0 ? offScreenRight
                : Math.max(offScreenLeft, childRight - parentRight);
    } else {
        dx = offScreenLeft != 0 ? offScreenLeft
                : Math.min(childLeft - parentLeft, offScreenRight);
    }

    // Favor bringing the top into view over the bottom. If top is already visible and
    // we should scroll to make bottom visible, make sure top does not go out of bounds.
    final int dy = offScreenTop != 0 ? offScreenTop
            : Math.min(childTop - parentTop, offScreenBottom);

    if (dx != 0 || dy != 0) {
        if (immediate) {
            parent.scrollBy(dx, dy);
        } else {
            parent.smoothScrollBy(dx, dy);
        }
        return true;
    }
    return false;
}

解決方案

    public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
        //計算偏移量
        int selectedItemOffsetStart = 0;
        int selectedItemOffsetEnd = 0;
        selectedItemOffsetStart = !isVertical() ? (getFreeWidth() - child.getWidth()) : (getFreeHeight() - child.getHeight());
        selectedItemOffsetStart /= 2;
        selectedItemOffsetEnd = selectedItemOffsetStart;
        
        final int parentLeft = getPaddingLeft();
        final int parentTop = getPaddingTop();
        final int parentRight = getWidth() - getPaddingRight();
        final int parentBottom = getHeight() - getPaddingBottom();
        final int childLeft = child.getLeft() + rect.left - child.getScrollX();
        final int childTop = child.getTop() + rect.top - child.getScrollY();
        final int childRight = childLeft + rect.width();
        final int childBottom = childTop + rect.height();


        final int offScreenLeft = Math.min(0, childLeft - parentLeft - mSelectedItemOffsetStart);
        final int offScreenRight = Math.max(0, childRight - parentRight + mSelectedItemOffsetEnd);

        final int offScreenTop = Math.min(0, childTop - parentTop - mSelectedItemOffsetStart);
        final int offScreenBottom = Math.max(0, childBottom - parentBottom + mSelectedItemOffsetEnd);

        // Favor the "start" layout direction over the end when bringing one side or the other
        // of a large rect into view. If we decide to bring in end because start is already
        // visible, limit the scroll such that start won't go out of bounds.
        final int dx;
        if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
            dx = offScreenRight != 0 ? offScreenRight
                    : Math.max(offScreenLeft, childRight - parentRight);
        } else {
            dx = offScreenLeft != 0 ? offScreenLeft
                    : Math.min(childLeft - parentLeft, offScreenRight);
        }

        // Favor bringing the top into view over the bottom. If top is already visible and
        // we should scroll to make bottom visible, make sure top does not go out of bounds.
        final int dy = offScreenTop != 0 ? offScreenTop
                : Math.min(childTop - parentTop, offScreenBottom);

        if (dx != 0 || dy != 0) {
            if (immediate) {
                scrollBy(dx, dy);
            } else {
                smoothScrollBy(dx, dy);
            }
            return true;
        }
        return false;
    }

這里要注意的是,為了適配v7,需要自定義LayoutManager,不然RecyclerView的requestChildRectangleOnScreen可能無法執行。

public class V7LinearLayoutManager extends LinearLayoutManager {
    public V7LinearLayoutManager(Context context) {
        super(context);
    }

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

    public V7LinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr, 0);
    }

    @Override
    public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {
        if(parent instanceof TvRecyclerView) {
            return parent.requestChildRectangleOnScreen(child, rect, immediate);
        }
        return super.requestChildRectangleOnScreen(parent, child, rect, immediate, focusedChildVisible);
    }
}

系列文章總結

  1. 想要改變焦點查找規則,可以關注focusSearch的過程。
  2. 想要監聽焦點變化的回調,可以關注requestFocus的過程。

如果想要實現一套通用焦點框架,個人想法是在Android原生焦點機制的基礎上做一些定制化的操作,或許并不需要完全自己去實現一套焦點框架。

TV端焦點問題的比較復雜的根本問題我認為有兩點:

  1. 主觀因素可能是我們對Android原生的焦點機制還沒有特別的清楚,所以不知道如何下手去處理一些不符合預期的現象。其實這些現象如果跟著源碼去看的話,會發現它的實現都是有一定道理的。
  2. 客觀因素是某些的UI交互比較復雜,Andorid原生的焦點機制只是采用了比較折中的處理方案。沒有什么語言是完美的,也沒有什么框架是完美的,能滿足我們需求才是最好的。所以我認為焦點問題的處理應該建立在我們有一套統一的UI交互的基礎上,然后我們在去基于Android原生焦點機制做一些定制化的操作,具體如何定制化,基本上問題都可以在文中提到的幾個回調接口中去處理。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
禁止轉載,如需轉載請通過簡信或評論聯系作者。