原生RecyclerView使用時(shí)的局限性
在手機(jī)端使用RecyclerView相信都很簡(jiǎn)單,但是在TV和盒子端就會(huì)出現(xiàn)不少的問(wèn)題抹剩,例如焦點(diǎn)顯示不全,無(wú)法定位到某個(gè)position等等
下面總結(jié)下在TV端使用使用RecyclerView時(shí)踩過(guò)的坑
(一) 解決RecyclerView刷新數(shù)據(jù)的時(shí)候纵装,焦點(diǎn)錯(cuò)亂問(wèn)題
下面是google官方給出的解決方案
1. adapter調(diào)用setHasStableIds(true)方法
2. 重寫(xiě)getItemId()方法,讓每個(gè)view都有各自的id
public abstract class BaseRecyclerAdapter<VH extends BaseRecyclerViewHolder, T> extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
public LayoutInflater mInflate;
public List<T> mDataList;
public BaseRecyclerAdapter(Context context, List<T> dataList) {
this.mInflate = LayoutInflater.from(context);
this.mDataList = dataList;
setHasStableIds(true);
}
public void setDataList(List<T> dataList) {
this.mDataList = dataList;
}
public void addDataList(List<T> dataList) {
if (dataList != null && dataList.size()>0) {
this.mDataList.addAll(dataList);
}
}
@Override
public int getItemCount() {
return mDataList != null ? mDataList.size() : 0;
}
@Override
public long getItemId(int position) {
return position;
}
public List<T> getDataList() {
return mDataList;
}
@Override
public void onBindViewHolder(BaseRecyclerViewHolder holder, int position) {
onBindBaseViewHolder((VH) holder, position);
}
public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
protected abstract void onBindBaseViewHolder(VH viewHolder, int position);
/**
* @param hasStableIds 有多個(gè)observer的話會(huì)報(bào)錯(cuò)
*/
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(hasStableIds);
}
}
3. RecyclerView的動(dòng)畫(huà)必須去掉
setItemAnimator(null);
(二)解決長(zhǎng)按遙控器叭爱,讓RecyclerView快速滑動(dòng)酵使,焦點(diǎn)錯(cuò)亂問(wèn)題
1. 由于長(zhǎng)按遙控器的時(shí)候盖桥,RecyclerView找不到findFocusView為null革屠,此時(shí)我就把RecyclerView的onKeyUp事件給屏蔽了
2. 通過(guò)FocusFind類找到view锚赤,并讓view請(qǐng)求焦點(diǎn)匹舞;
3.focusFind焦點(diǎn)失敗之后,如果是讓其滑動(dòng)item的寬度(左后滑動(dòng))或者高度(上下滑動(dòng))线脚,因?yàn)橛锌赡芑较旅孢€有可能有item赐稽,否則RecyclerView將不會(huì)繼續(xù)滑動(dòng)
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (mInterceptLister != null && mInterceptLister.onIntercept(event)) {
return true;
}
boolean result = super.dispatchKeyEvent(event);
View focusView = this.findFocus();
if (focusView == null) {
return result;
} else {
int dy = 0;
int dx = 0;
if (getChildCount() > 0) {
View firstView = this.getChildAt(0);
dy = firstView.getHeight();
dx = firstView.getWidth();
}
if (event.getAction() == KeyEvent.ACTION_UP) {
return true;
} else {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_RIGHT:
View rightView = FocusFinder.getInstance().findNextFocus(this, focusView, View.FOCUS_RIGHT);
Log.i(TAG, "rightView is null:" + (rightView == null));
if (rightView != null) {
rightView.requestFocus();
return true;
} else {
this.smoothScrollBy(dx, 0);
return true;
}
case KeyEvent.KEYCODE_DPAD_LEFT:
View leftView = FocusFinder.getInstance().findNextFocus(this, focusView, View.FOCUS_LEFT);
Log.i(TAG, "leftView is null:" + (leftView == null));
if (leftView != null) {
leftView.requestFocus();
return true;
} else {
this.smoothScrollBy(-dx, 0);
return true;
}
case KeyEvent.KEYCODE_DPAD_DOWN:
View downView = FocusFinder.getInstance().findNextFocus(this, focusView, View.FOCUS_DOWN);
Log.i(TAG, " downView is null:" + (downView == null));
if (downView != null) {
downView.requestFocus();
return true;
} else {
this.smoothScrollBy(0, dy);
return true;
}
case KeyEvent.KEYCODE_DPAD_UP:
View upView = FocusFinder.getInstance().findNextFocus(this, focusView, View.FOCUS_UP);
Log.i(TAG, "upView is null:" + (upView == null));
if (event.getAction() == KeyEvent.ACTION_UP) {
return true;
} else {
if (upView != null) {
upView.requestFocus();
return true;
} else {
this.smoothScrollBy(0, -dy);
return true;
}
}
}
}
}
return result;
}
(三)解決RecyclerView 定位到某個(gè)item不獲取焦點(diǎn)問(wèn)題
RecyclerView僅僅提供了一個(gè)smoothScrollToPosition(int position)方法叫榕,而且該方法,能滑到指定的position位置姊舵,但是該position處的item并沒(méi)有獲取焦點(diǎn)晰绎。
想起在使用LeanBack中的RecyclerView時(shí),你會(huì)發(fā)現(xiàn)它提供了專門(mén)定位到某個(gè)position括丁,并獲取焦點(diǎn)的方法荞下。但是然后沉下心把源碼給看了,終于明白了其中的原理史飞,原來(lái)它在定位到指定位置的item后尖昏,找到目標(biāo)的item,并讓其主動(dòng)請(qǐng)求焦點(diǎn)构资。完美解決
(1) RecyclerView的smoothScrollToPosition方法抽诉,最終執(zhí)行的是LayoutManger的smoothScrollToPosition方法。下面是RecyclerView的
smoothScrollToPosition的源代碼
public void smoothScrollToPosition(int position) {
if (mLayoutFrozen) {
return;
}
if (mLayout == null) {
Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. " +
"Call setLayoutManager with a non-null argument.");
return;
}
mLayout.smoothScrollToPosition(this, mState, position);
}
(2) RecyclerView彈性滑動(dòng)是通過(guò)SmoothScroller來(lái)實(shí)現(xiàn)的吐绵,SmoothScroller中有onStart和onStop的回調(diào)迹淌,大膽猜測(cè)一下,onStop肯定是滑動(dòng)結(jié)束的回調(diào)己单,于是找到targetView唉窃,并讓其請(qǐng)求焦點(diǎn),問(wèn)題得以解決
/**
* Created by liuyu on 17/2/8.
* fix issue: RecyclerView 進(jìn)行item定位的時(shí)候,item沒(méi)有獲取焦點(diǎn)
*/
public class TvGridLayoutManager extends GridLayoutManager {
public TvGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public TvGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public TvGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
/**
* Base class which scrolls to selected view in onStop().
*/
abstract class GridLinearSmoothScroller extends LinearSmoothScroller {
public GridLinearSmoothScroller(Context context) {
super(context);
}
/**
* 滑動(dòng)完成后,讓該targetPosition 處的item獲取焦點(diǎn)
*/
@Override
protected void onStop() {
super.onStop();
View targetView = findViewByPosition(getTargetPosition());
if (targetView != null) {
targetView.requestFocus();
}
super.onStop();
}
}
/**
* RecyclerView的smoothScrollToPosition方法最終會(huì)執(zhí)行smoothScrollToPosition
* @param recyclerView
* @param state
* @param position
*/
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
GridLinearSmoothScroller linearSmoothScroller =
new GridLinearSmoothScroller(recyclerView.getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return computeVectorForPosition(targetPosition);
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
public PointF computeVectorForPosition(int targetPosition) {
return super.computeScrollVectorForPosition(targetPosition);
}
}
為了方便大家我把代碼上傳到了github:https://github.com/Fredlxy/TVRecyclerView