ViewPager2在TV端適配

先貼代碼椰拒,后續(xù)分析

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (mRecyclerView.getScrollState() != SCROLL_STATE_IDLE) {
        return true;
    }
    return super.dispatchKeyEvent(event) || executeKeyEvent(event);
}

private boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                handled = arrowScroll(FOCUS_LEFT);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                handled = arrowScroll(FOCUS_RIGHT);
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                handled = arrowScroll(FOCUS_UP);
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                handled = arrowScroll(FOCUS_DOWN);
                break;
        }
    }
    return handled;
}

private boolean arrowScroll(int direction) {
    if (mRecyclerView.getScrollState() != SCROLL_STATE_IDLE) {
        return false;
    }
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
             parent = parent.getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
                 parent = parent.getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e("ViewPagerTV", "arrowScroll tried to find focus based on non-child " +
                    "current focused view " + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;
    ViewGroup viewByPosition = (ViewGroup) mLayoutManager.findViewByPosition(mCurrentItem);
    View nextFocused = FocusFinder.getInstance().findNextFocus(mRecyclerView, currentFocused,
            direction);
    if (getOrientation() == ORIENTATION_HORIZONTAL) {
        if (nextFocused != null && nextFocused != currentFocused) {
            if (direction == View.FOCUS_LEFT) {
                // If there is nothing to the left, or this is causing us to
                // jump to the right, then what we really want to do is page left.
                final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
                final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;

                Log.d(TAG, "FOCUS_LEFT: nextLeft=" + nextLeft + ", currLeft=" + currLeft);
                Log.d(TAG, "FOCUS_LEFT: nextLeft=" + nextFocused + ", currLeft=" + currentFocused);
                if (currentFocused != null && nextLeft < 0 || nextLeft >= currLeft) {
                    nextFocused.requestLayout();
                    nextFocused.requestFocus();
                    handled = pageLeft();
                } else {
                    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.
                final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
                final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
                Log.d(TAG, "FOCUS_LEFT: nextLeft=" + nextLeft + ", currLeft=" + currLeft);

                int maxWidth;
                if (viewByPosition == null) {
                    maxWidth = (int) widthPixels;
                } else {
                    maxWidth = viewByPosition.getMeasuredWidth();
                }
                if (currentFocused != null && nextLeft > maxWidth) {
                    nextFocused.requestLayout();
                    nextFocused.requestFocus();
                    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();
        }
    } else {
        if (nextFocused != null && nextFocused != currentFocused) {
            if (direction == View.FOCUS_UP) {
                // 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.
                final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
                final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
                if (currentFocused != null && nextTop >= currTop) {
                    handled = pageUp();
                } else {
                    handled = nextFocused.requestFocus();
                }
            } else if (direction == View.FOCUS_DOWN) {
                // 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.
                final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
                final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
                if (currentFocused != null && nextTop <= currTop) {
                    handled = pageDown();
                } else {
                    handled = nextFocused.requestFocus();
                }
            }
        } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
            // Trying to move left and nothing there; try to page.
            handled = pageUp();
        } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
            // Trying to move right and nothing there; try to page.
            handled = pageDown();
        }
    }
//        if (handled) {
//                  playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
//        }
    return handled;
}

private Rect getChildRectInPagerCoordinates(Rect outRect, View child) {
    if (outRect == null) {
        outRect = new Rect();
    }
    if (child == null) {
        outRect.set(0, 0, 0, 0);
        return outRect;
    }
    outRect.left = child.getLeft();
    outRect.right = child.getRight();
    outRect.top = child.getTop();
    outRect.bottom = child.getBottom();

    ViewParent parent = child.getParent();
    while (parent instanceof ViewGroup && parent != this) {
        final ViewGroup group = (ViewGroup) parent;
        outRect.left += group.getLeft();
        outRect.right += group.getRight();
        outRect.top += group.getTop();
        outRect.bottom += group.getBottom();

        parent = group.getParent();
    }

    Log.d(TAG, "getChildRectInPagerCoordinates: " + outRect.toString());

    return outRect;
}

boolean pageLeft() {
    Log.d(TAG, "pageLeft: ");
    if (mCurrentItem > 0) {
        setCurrentItem(mCurrentItem - 1, true);
        return true;
    }
    return false;
}

boolean pageRight() {
    Log.d(TAG, "pageRight: ");
    if (getAdapter() != null && mCurrentItem < (mAdapter.getItemCount() - 1)) {
        setCurrentItem(mCurrentItem + 1, true);
        return true;
    }
    return false;
}


boolean pageUp() {
    if (mCurrentItem > 0) {
        setCurrentItem(mCurrentItem - 1, true);
        return true;
    }
    return false;
}

boolean pageDown() {
    if (mAdapter != null && mCurrentItem < (mAdapter.getItemCount() - 1)) {
        setCurrentItem(mCurrentItem + 1, true);
        return true;
    }
    return false;
}

笔咽、、茬暇、

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市寡喝,隨后出現(xiàn)的幾起案子糙俗,更是在濱河造成了極大的恐慌,老刑警劉巖预鬓,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件巧骚,死亡現(xiàn)場離奇詭異,居然都是意外死亡格二,警方通過查閱死者的電腦和手機(jī)劈彪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來顶猜,“玉大人沧奴,你說我怎么就攤上這事〕ふ” “怎么了滔吠?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長挠日。 經(jīng)常有香客問我疮绷,道長,這世上最難降的妖魔是什么嚣潜? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任冬骚,我火速辦了婚禮,結(jié)果婚禮上懂算,老公的妹妹穿的比我還像新娘只冻。我一直安慰自己,他們只是感情好计技,可當(dāng)我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布属愤。 她就那樣靜靜地躺著,像睡著了一般酸役。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上驾胆,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天涣澡,我揣著相機(jī)與錄音,去河邊找鬼丧诺。 笑死入桂,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的驳阎。 我是一名探鬼主播抗愁,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼馁蒂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蜘腌?” 一聲冷哼從身側(cè)響起沫屡,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎撮珠,沒想到半個月后沮脖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡芯急,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年勺届,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片娶耍。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡免姿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出榕酒,到底是詐尸還是另有隱情胚膊,我是刑警寧澤,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布奈应,位于F島的核電站澜掩,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏杖挣。R本人自食惡果不足惜肩榕,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望惩妇。 院中可真熱鬧株汉,春花似錦、人聲如沸歌殃。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽氓皱。三九已至路召,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間波材,已是汗流浹背股淡。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留廷区,地道東北人唯灵。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像隙轻,于是被迫代替她去往敵國和親埠帕。 傳聞我的和親對象是個殘疾皇子垢揩,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容