Ich habe einen RecyclerView mit normalerweise zwei Spalten und bis zu acht. Wir benutzen eine Menge D-PAD Navigation. Beim Scrollen haben wir ein Problem, das fokussierte Element springt von links nach rechts. Siehe Fotos: RecyclerView Fokus Scrollen
Ich bemerkte, dass wenn die Elemente, die als nächstes kommen, zwischengespeichert werden, beim Scrollen kein Fokusproblem auftritt. Ein anderes Problem, das ich habe, ist, dass mein Fokusgegenstand unter meiner klebrigen Überschrift erscheinen kann. Dies ist nicht erwünscht. Ich habe das Gefühl, wenn ich es beim Scrollen so machen würde, hätte es eine Art "Schwelle". Auf diese Weise scrollt es, wenn sich der Fokus innerhalb eines Objekts befindet. Auf diese Weise ist der Fokus nie ganz unten oder ganz oben.
, die mit im Auge habe ich versucht, diesen Ansatz ohne Glück:
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 by %d", scrollBy));
}
} else if (position <= threshold) {
//scroll up if possible
int topIndex = rv.getChildPosition(rv.getChildAt(0));
if (topIndex > 0) {
//scroll up
int scrollBy = v.getHeight();
recycler.scrollBy(0, -scrollBy);
Log.d(TAG, String.format("Scrolling up by %d", -scrollBy));
}
}
}
Ich bin offen für alle Ideen, wie man den Fokus verwalten beim Scrollen.
Ich habe so etwas gemacht, aber stattdessen habe ich 'intercepFocusSearch()' verwendet. Danke für den Fehler! –
Arbeitete für mich! Vielen Dank! – Crisic