RecyclerView焦點(diǎn)跳轉(zhuǎn)BUG優(yōu)化的方法
我們把RecyclerView寫成GridView樣式,并把RecyclerView的item寫成focusable并且有焦點(diǎn)框的時(shí)候,我們用焦點(diǎn)滾動(dòng)RecyclerView的時(shí)候會(huì)發(fā)現(xiàn)RecyclerView的焦點(diǎn)跳轉(zhuǎn)有bug,跟我們想要的焦點(diǎn)跳轉(zhuǎn)規(guī)則不一致,會(huì)出現(xiàn)的BUG如下圖:

黑色方框代表屏幕,我們從左上角的一個(gè)item往下按焦點(diǎn)的時(shí)候,當(dāng)需要加載新的一行的時(shí)候焦點(diǎn)卻跑到了新的一行的最后一個(gè)item上面了,(如圖,本來是item1獲得焦點(diǎn)的,結(jié)果跑到item2上面了)。
這是RecyclerView的一個(gè)BUG,記得RecyclerView剛出來的時(shí)候滾動(dòng)都還有點(diǎn)卡頓,到了現(xiàn)在滾動(dòng)起來還是非常流暢的,比較一個(gè)全新的藝術(shù)般的空間是需要時(shí)間來沉淀的,這個(gè)BUG我們可以重寫GridLayoutManger來解決。直接看代碼:
package com.wasu.cs.widget;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
/**
* 自定義GridLayoutManager,修改RecyelerView焦點(diǎn)亂跳的BUG
* Created by Danxingxi on 2016/4/1.
*/
public class FocusGridLayoutManager extends GridLayoutManager {
/**
* Constructor used when layout manager is set in XML by RecyclerView attribute
* "layoutManager". If spanCount is not specified in the XML, it defaults to a
* single column.
*
* @param context
* @param attrs
* @param defStyleAttr
* @param defStyleRes
* @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_spanCount
*/
public FocusGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
/**
* Creates a vertical GridLayoutManager
*
* @param context Current context, will be used to access resources.
* @param spanCount The number of columns in the grid
*/
public FocusGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
/**
* @param context Current context, will be used to access resources.
* @param spanCount The number of columns or rows in the grid
* @param orientation Layout orientation. Should be {@link #HORIZONTAL} or {@link
* #VERTICAL}.
* @param reverseLayout When set to true, layouts from end to start.
*/
public FocusGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
/**
* Return the current number of child views attached to the parent RecyclerView.
* This does not include child views that were temporarily detached and/or scrapped.
*
* @return Number of attached children
*/
@Override
public int getChildCount() {
return super.getChildCount();
}
/**
* Return the child view at the given index
*
* @param index Index of child to return
* @return Child view at index
*/
@Override
public View getChildAt(int index) {
return super.getChildAt(index);
}
/**
* Returns the number of items in the adapter bound to the parent RecyclerView.
* @return The number of items in the bound adapter
*/
@Override
public int getItemCount() {
return super.getItemCount();
}
/**
* Returns the item View which has or contains focus.
*
* @return A direct child of RecyclerView which has focus or contains the focused child.
*/
@Override
public View getFocusedChild() {
return super.getFocusedChild();
}
/**
* Returns the adapter position of the item represented by the given View. This does not
* contain any adapter changes that might have happened after the last layout.
*
* @param view The view to query
* @return The adapter position of the item which is rendered by this View.
*/
@Override
public int getPosition(View view) {
return super.getPosition(view);
}
/**
* 獲取列數(shù)
* @return
*/
@Override
public int getSpanCount() {
return super.getSpanCount();
}
/**
* Called when searching for a focusable view in the given direction has failed for the current content of the RecyclerView.
* This is the LayoutManager's opportunity to populate views in the given direction to fulfill the request if it can.
* The LayoutManager should attach and return the view to be focused. The default implementation returns null.
* 防止當(dāng)recyclerview上下滾動(dòng)的時(shí)候焦點(diǎn)亂跳
* @param focused
* @param focusDirection
* @param recycler
* @param state
* @return
*/
@Override
public View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state) {
// Need to be called in order to layout new row/column
View nextFocus = super.onFocusSearchFailed(focused, focusDirection, recycler, state);
if (nextFocus == null) {
return null;
}
/**
* 獲取當(dāng)前焦點(diǎn)的位置
*/
int fromPos = getPosition(focused);
/**
* 獲取我們希望的下一個(gè)焦點(diǎn)的位置
*/
int nextPos = getNextViewPos(fromPos, focusDirection);
return findViewByPosition(nextPos);
}
/**
* Manually detect next view to focus.
*
* @param fromPos from what position start to seek.
* @param direction in what direction start to seek. Your regular {@code View.FOCUS_*}.
* @return adapter position of next view to focus. May be equal to {@code fromPos}.
*/
protected int getNextViewPos(int fromPos, int direction) {
int offset = calcOffsetToNextView(direction);
if (hitBorder(fromPos, offset)) {
return fromPos;
}
return fromPos + offset;
}
/**
* Calculates position offset.
*
* @param direction regular {@code View.FOCUS_*}.
* @return position offset according to {@code direction}.
*/
protected int calcOffsetToNextView(int direction) {
int spanCount = getSpanCount();
int orientation = getOrientation();
if (orientation == VERTICAL) {
switch (direction) {
case View.FOCUS_DOWN:
return spanCount;
case View.FOCUS_UP:
return -spanCount;
case View.FOCUS_RIGHT:
return 1;
case View.FOCUS_LEFT:
return -1;
}
} else if (orientation == HORIZONTAL) {
switch (direction) {
case View.FOCUS_DOWN:
return 1;
case View.FOCUS_UP:
return -1;
case View.FOCUS_RIGHT:
return spanCount;
case View.FOCUS_LEFT:
return -spanCount;
}
}
return 0;
}
/**
* Checks if we hit borders.
*
* @param from from what position.
* @param offset offset to new position.
* @return {@code true} if we hit border.
*/
private boolean hitBorder(int from, int offset) {
int spanCount = getSpanCount();
if (Math.abs(offset) == 1) {
int spanIndex = from % spanCount;
int newSpanIndex = spanIndex + offset;
return newSpanIndex < 0 || newSpanIndex >= spanCount;
} else {
int newPos = from + offset;
return newPos < 0 && newPos >= spanCount;
}
}
}
分析:在我們從第五行往下按的時(shí)候,第六行的view是重新加載的,當(dāng)新的一行的item還沒有加載出來的時(shí)候,去找焦點(diǎn)是找不到的,找不到焦點(diǎn)就會(huì)調(diào)用mLayout.onFocusSearchFailed()方法,

onFocusSearchFailed方法是LayoutManager的方法,默認(rèn)是返回null的,我們?cè)谧远xGridLayoutManager的時(shí)候重寫此方法即可,具體的處理步驟請(qǐng)看到代碼。在RecyclerView源代碼中,onFocusSearchFailed是內(nèi)部抽象類LayoutManager的一個(gè)成員方法,默認(rèn)返回null。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android ListView用EditText實(shí)現(xiàn)搜索功能效果
本篇文章主要介紹了Android ListView用EditText實(shí)現(xiàn)搜索功能效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Android超詳細(xì)講解組件LinearLayout的使用
LinearLayout又稱作線性布局,是一種非常常用的布局。正如它的名字所描述的一樣,這個(gè)布局會(huì)將它所包含的控件在線性方向上依次排列。既然是線性排列,肯定就不僅只有一個(gè)方向,這里一般只有兩個(gè)方向:水平方向和垂直方向2022-03-03
Android繪制旋轉(zhuǎn)動(dòng)畫方法詳解
這篇文章主要介紹了Android如何采用RotateAnimation繪制一個(gè)旋轉(zhuǎn)動(dòng)畫,文中的實(shí)現(xiàn)方法講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試2022-01-01
Android中GridView布局實(shí)現(xiàn)整體居中方法示例
最近在工作中遇到了GridView布局的相關(guān)問題,通過查找相關(guān)資料終于解決了,所以下面這篇文章主要給大家介紹了關(guān)于Android中GridView布局實(shí)現(xiàn)整體居中的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒。2017-09-09
viewPager+fragment刷新緩存fragment的方法
這篇文章主要介紹了viewPager+fragment刷新緩存fragment的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
OKhttp攔截器實(shí)現(xiàn)實(shí)踐環(huán)節(jié)源碼解析
這篇文章主要為大家介紹了OKhttp攔截器實(shí)現(xiàn)實(shí)踐環(huán)節(jié)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android ImageView Src 和Background 區(qū)別
這篇文章主要介紹了Android ImageView Src 和Background 區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-09-09
詳解Android權(quán)限管理之Android 6.0運(yùn)行時(shí)權(quán)限及解決辦法
本篇文章主要介紹Android權(quán)限管理之Android 6.0運(yùn)行時(shí)權(quán)限及解決辦法,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-11-11
Android中使用orc實(shí)現(xiàn)文字識(shí)別實(shí)例
這篇文章主要介紹了Android中使用orc實(shí)現(xiàn)文字識(shí)別實(shí)例,詳細(xì)的介紹了orc的簡(jiǎn)介和用法,有興趣的可以了解一下2017-05-05

