如需轉(zhuǎn)載請?jiān)u論或簡信苞尝,并注明出處,未經(jīng)允許不得轉(zhuǎn)載
系列文章
- android tv常見問題(一)焦點(diǎn)查找規(guī)律
- android tv常見問題(二)如何監(jiān)聽ViewGroup子View的焦點(diǎn)狀態(tài)
- android tv常見問題(三)RecyclerView的焦點(diǎn)記憶
- android tv常見問題(四)焦點(diǎn)變化時(shí)踪旷,Recyclerview是如何進(jìn)行滾動(dòng)的
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é)
- 想要改變焦點(diǎn)查找規(guī)則蓖乘,可以關(guān)注focusSearch的過程嘉抒。
- 想要監(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):
- 主觀因素可能是我們對Android原生的焦點(diǎn)機(jī)制還沒有特別的清楚,所以不知道如何下手去處理一些不符合預(yù)期的現(xiàn)象芦缰。其實(shí)這些現(xiàn)象如果跟著源碼去看的話枫慷,會(huì)發(fā)現(xiàn)它的實(shí)現(xiàn)都是有一定道理的。
- 客觀因素是某些的UI交互比較復(fù)雜探孝,Andorid原生的焦點(diǎn)機(jī)制只是采用了比較折中的處理方案誉裆。沒有什么語言是完美的,也沒有什么框架是完美的粱腻,能滿足我們需求才是最好的斩跌。所以我認(rèn)為焦點(diǎn)問題的處理應(yīng)該建立在我們有一套統(tǒng)一的UI交互的基礎(chǔ)上,然后我們在去基于Android原生焦點(diǎn)機(jī)制做一些定制化的操作柬批,具體如何定制化,基本上問題都可以在文中提到的幾個(gè)回調(diào)接口中去處理锻霎。