RecyclerView焦点跳转BUG优化的方法

 更新时间:2018年04月07日 11:05:34   作者:Dawish_大D  
这篇文章主要介绍了RecyclerView焦点跳转BUG优化的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

我们把RecyclerView写成GridView样式,并把RecyclerView的item写成focusable并且有焦点框的时候,我们用焦点滚动RecyclerView的时候会发现RecyclerView的焦点跳转有bug,跟我们想要的焦点跳转规则不一致,会出现的BUG如下图:

黑色方框代表屏幕,我们从左上角的一个item往下按焦点的时候,当需要加载新的一行的时候焦点却跑到了新的一行的最后一个item上面了,(如图,本来是item1获得焦点的,结果跑到item2上面了)。

这是RecyclerView的一个BUG,记得RecyclerView刚出来的时候滚动都还有点卡顿,到了现在滚动起来还是非常流畅的,比较一个全新的艺术般的空间是需要时间来沉淀的,这个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焦点乱跳的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); 
  } 
 
  /** 
   * 获取列数 
   * @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. 
   * 防止当recyclerview上下滚动的时候焦点乱跳 
   * @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; 
    } 
    /** 
     * 获取当前焦点的位置 
     */ 
    int fromPos = getPosition(focused); 
    /** 
     * 获取我们希望的下一个焦点的位置 
     */ 
    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; 
    } 
  } 
} 

分析:在我们从第五行往下按的时候,第六行的view是重新加载的,当新的一行的item还没有加载出来的时候,去找焦点是找不到的,找不到焦点就会调用mLayout.onFocusSearchFailed()方法,

onFocusSearchFailed方法是LayoutManager的方法,默认是返回null的,我们在自定义GridLayoutManager的时候重写此方法即可,具体的处理步骤请看到代码。在RecyclerView源代码中,onFocusSearchFailed是内部抽象类LayoutManager的一个成员方法,默认返回null。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • 清楚详解Android 进程间图传递图形buffer原理

    清楚详解Android 进程间图传递图形buffer原理

    这篇文章主要为大家清楚的详解了Android 进程间图传递图形buffer原理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android Studio error: Unable to start the daemon process的解决方法

    Android Studio error: Unable to start the daemon process的解决方

    这篇文章主要介绍了在 Android Studio 上新建项目,出现 Unable to start the daemon process问题的几种的解决方法,需要的朋友可以参考下
    2020-10-10
  • Android实现图片轮播效果

    Android实现图片轮播效果

    这篇文章主要为大家详细介绍了Android实现图片轮播效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-12-12
  • Android中如何使用Glide加载图像

    Android中如何使用Glide加载图像

    大家好,本篇文章主要讲的是Android中如何使用Glide加载图像,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • Rxjava+Retrofit+Okhttp进行网络访问及数据解析

    Rxjava+Retrofit+Okhttp进行网络访问及数据解析

    这篇文章主要介绍了Rxjava+Retrofit+Okhttp进行网络访问及数据解析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-08-08
  • Android数据加密之Rsa加密的简单实现

    Android数据加密之Rsa加密的简单实现

    下面小编就为大家带来一篇Android数据加密之Rsa加密的简单实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10
  • Android自定义单例AlertDialog详解

    Android自定义单例AlertDialog详解

    这篇文章主要为大家详细介绍了Android自定义单例AlertDialog的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Android将项目导出为Library并在项目中使用教程

    Android将项目导出为Library并在项目中使用教程

    这篇文章主要介绍了Android将项目导出为Library并在项目中使用教程,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • Android实现图片转高斯模糊以及高斯模糊布局

    Android实现图片转高斯模糊以及高斯模糊布局

    这篇文章主要为大家详细介绍了Android实现图片转高斯模糊的方法,以及高斯模糊布局,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Android中ScrollView 滑到头部或尾部可伸缩放大效果

    Android中ScrollView 滑到头部或尾部可伸缩放大效果

    最近做项目遇到这样的需求S当crollView 滑动到顶部,不能在滑动的时候,图片可以下拉放大,松开又恢复,滑到底部没有内容的时候,也有伸缩效果,下面通过实例代码给大家介绍Android ScrollView 滑到头部或尾部可伸缩放大功能,一起学习吧
    2017-03-03

最新评论