android tv常見問題(四)焦點(diǎn)變化時(shí),Recyclerview是如何進(jìn)行滾動(dòng)的

如需轉(zhuǎn)載請?jiān)u論或簡信苞尝,并注明出處,未經(jīng)允許不得轉(zhuǎn)載

系列文章

github地址

https://github.com/Geekholt/TvFocus

目錄

期望結(jié)果

Recyclerview滾動(dòng)時(shí),聚焦的item位置保持在中間徽级。


4.1.gif

實(shí)際結(jié)果

4.2.gif

問題分析

需要在計(jì)算RecyclerView滑動(dòng)距離的方法中進(jìn)行重寫餐抢,控制每次滑動(dòng)的距離旷痕。先來看看RecyclerView原生的滑動(dòng)距離計(jì)算方法顽冶。

RecyclerView#requestChildRectangleOnScreen

當(dāng)RecyclerView的某個(gè)子View需要被定位在屏幕的某個(gè)矩形范圍時(shí)强重,調(diào)用此方法。

    /**
     * 通過該方法設(shè)置選中的item居中
     * <p>
     * 最終計(jì)算出的dy佃声,dx的實(shí)際意義就是在滾動(dòng)中上下和左右滑動(dòng)的距離
     *
     * @param child     發(fā)出請求的子View
     * @param rect      子View坐標(biāo)系內(nèi)的矩形圾亏,即此子View希望在屏幕上的定位
     * @param immediate 設(shè)為true碗誉,則禁止動(dòng)畫和平滑移動(dòng)滾動(dòng)條
     * @return 進(jìn)行了滾動(dòng)操作的這個(gè)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) {
        //計(jì)算偏移量
        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可能無法執(zhí)行。

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);
    }
}

系列文章總結(jié)

  1. 想要改變焦點(diǎn)查找規(guī)則蓖乘,可以關(guān)注focusSearch的過程嘉抒。
  2. 想要監(jiān)聽焦點(diǎn)變化的回調(diào)袍暴,可以關(guān)注requestFocus的過程。

如果想要實(shí)現(xiàn)一套通用焦點(diǎn)框架岗宣,個(gè)人想法是在Android原生焦點(diǎn)機(jī)制的基礎(chǔ)上做一些定制化的操作狈定,或許并不需要完全自己去實(shí)現(xiàn)一套焦點(diǎn)框架习蓬。

TV端焦點(diǎn)問題的比較復(fù)雜的根本問題我認(rèn)為有兩點(diǎn):

  1. 主觀因素可能是我們對Android原生的焦點(diǎn)機(jī)制還沒有特別的清楚,所以不知道如何下手去處理一些不符合預(yù)期的現(xiàn)象芦缰。其實(shí)這些現(xiàn)象如果跟著源碼去看的話枫慷,會(huì)發(fā)現(xiàn)它的實(shí)現(xiàn)都是有一定道理的。
  2. 客觀因素是某些的UI交互比較復(fù)雜探孝,Andorid原生的焦點(diǎn)機(jī)制只是采用了比較折中的處理方案誉裆。沒有什么語言是完美的,也沒有什么框架是完美的粱腻,能滿足我們需求才是最好的斩跌。所以我認(rèn)為焦點(diǎn)問題的處理應(yīng)該建立在我們有一套統(tǒng)一的UI交互的基礎(chǔ)上,然后我們在去基于Android原生焦點(diǎn)機(jī)制做一些定制化的操作柬批,具體如何定制化,基本上問題都可以在文中提到的幾個(gè)回調(diào)接口中去處理锻霎。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
禁止轉(zhuǎn)載揪漩,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者奄容。
  • 序言:七十年代末产徊,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子戈盈,更是在濱河造成了極大的恐慌谆刨,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件刁岸,死亡現(xiàn)場離奇詭異虹曙,居然都是意外死亡番舆,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進(jìn)店門疏哗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拴事,“玉大人刃宵,你說我怎么就攤上這事∩ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵等太,是天一觀的道長蛮放。 經(jīng)常有香客問我,道長瞻想,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮岳悟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘呵俏。我一直安慰自己春瞬,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布随常。 她就那樣靜靜地躺著绪氛,像睡著了一般涝影。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上序目,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天伯襟,我揣著相機(jī)與錄音,去河邊找鬼叛赚。 笑死,一個(gè)胖子當(dāng)著我的面吹牛俺附,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播步鉴,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼唠叛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起册舞,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤调鲸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后藐石,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年驱证,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了恋腕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,117評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡伙单,死狀恐怖吻育,靈堂內(nèi)的尸體忽然破棺而出牡彻,到底是詐尸還是另有隱情出爹,我是刑警寧澤缎除,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布梢为,位于F島的核電站,受9級特大地震影響铸董,放射性物質(zhì)發(fā)生泄漏肴沫。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一悲幅、第九天 我趴在偏房一處隱蔽的房頂上張望汰具。 院中可真熱鬧菱魔,春花似錦、人聲如沸聚蝶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恰聘。三九已至吸占,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間矾屯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工孙技, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人牵啦。 一個(gè)月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓楞件,卻偏偏與公主長得像裳瘪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子彭羹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評論 2 355