热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

RecyclerView焦点跳转BUG优化的方法

这篇文章主要介绍了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。


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


推荐阅读
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
author-avatar
拍友2502908871
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有