RecyclerView Fokus scrollen

Ich habe eine RecyclerView mit in der Regel zwei Spalten und bis zu acht. Wir verwenden eine Menge von D-PAD navigation. Wir haben ein problem beim scrollen, der fokussierte Punkt springt von Links nach rechts. Siehe Fotos:
RecyclerView Fokus scrollen

Bemerkte ich, dass, wenn die Elemente, die nächste zwischengespeichert werden, gibt es kein Fokus-problem beim scrollen. Ein weiteres problem das ich habe ist mein Fokus Artikel kann erscheinen unter meinem sticky-header. Dies ist nicht erwünscht. Also ich denke, wenn ich es vorgenommen, sodass beim scrollen, es hätte eine Art "Schwelle". Auf diese Weise, wenn der Fokus in ein Element des seins außerhalb des Bildschirms, wird es Blättern. Auf diese Weise liegt der Fokus nie ganz unten, oder ganz oben.

In diesem Sinne habe ich versucht, dieses Konzept mit kein 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 das verwalten der Fokus beim scrollen.

InformationsquelleAutor Chad Bingham | 2015-07-23
Schreibe einen Kommentar