android - RecyclerView focus scrolling -


i have recyclerview 2 columns , eight. use lot of d-pad navigation. have problem when scrolling, focused item jump left right. see photos: enter image description here

i noticed if items coming next cached, there no focus problem when scrolling. problem have focus item can appear below sticky header. not desired. feel if made when scrolling have sort of "threshold". way when focus within 1 item of being off screen, scroll. way focus never @ bottom, or top.

with in mind, tried approach no luck:

recyclerview rv;  @override public void onfocuschange(view v, boolean hasfocus) {     if(!v.hasfocus()) {         log.w(tag, "view v did not have focus");         return;     }      final int index = rv.getchildposition(v); //adapter pos     if(index == no_position) {         log.w(tag, "recycler view did not have view");         return;     }      int position = rv.indexofchild(v);  // layout pos     int lastpos = rv.getchildcount();   // layout pos     int span = getlayoutmanager().getspancount();     int threshold = 2 * span;     log.d(tag, string.format("position: %1$d. lastpos: %2$d. span: %3$d. threshold: %4$d", position, lastpos, span, threshold));      if (position >= (lastpos - threshold)) {         //scroll down if possible         int bottomindex = rv.getchildposition(rv.getchildat(lastpos));         if (bottomindex < getitemcount()) {             //scroll down             int scrollby = v.getheight();             recycler.scrollby(0, scrollby);             log.d(tag, string.format("scrolling down %d", scrollby));          }      } else if (position <= threshold) {         //scroll if possible         int topindex = rv.getchildposition(rv.getchildat(0));         if (topindex > 0) {             //scroll             int scrollby = v.getheight();             recycler.scrollby(0, -scrollby);             log.d(tag, string.format("scrolling %d", -scrollby));         }     } } 

i open ideas on how manage focus when scrolling.

i reported bug aosp issue tracker: issue 190526

as can see source issue because gridlayoutmanager uses linearlayoutmanager's implementation of onfocussearchfailed() called when focus approaches inner border of recyclerview. linearlayoutmanager's implementation offers first/last (depends on scrolling direction) element. hence focus jumps first/last element of new row.

my workaround issue.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -