How to implement scrolling in RecyclerView on Android TV?

I have an application that I need to adapt for Android TV. This app contains a horizontal RecyclerView and does not scroll when I press the D-pad buttons on the remote control. I found this solution , but it fails. Here is the code:

<ru.myapp.package.HorizontalPersistentFocusWrapper
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
       <android.support.v7.widget.RecyclerView
           android:id="@+id/recycler_view"
           android:layout_width="match_parent"
           android:layout_height="250dp"
           android:background="@null"
           android:scrollbars="none"/>
</ru.myapp.package.HorizontalPersistentFocusWrapper>

HorizontalPersistentFocusWrapper is the same as PersistentFocusWrapper , but mPersistFocusVertical = false;

At this point, a crash occurs:

@Override
    public void requestChildFocus(View child, View focused) {
        super.requestChildFocus(child, focused);
        View view = focused;
        while (view != null && view.getParent() != child) {
            view = (View) view.getParent(); <<<------ Crash here
        }
        mSelectedPosition = view == null ? -1 : ((ViewGroup) child).indexOfChild(view);
        if (DEBUG) Log.v(TAG, "requestChildFocus focused " + focused + " mSelectedPosition " + mSelectedPosition);
    }

Crash stacktrace:

java.lang.ClassCastException: android.view.ViewRootImpl cannot be cast to android.view.View
         at ru.myapp.package.HorizontalPersistentFocusWrapper.requestChildFocus(HorizontalPersistentFocusWrapper.java:108)
         at android.view.View.handleFocusGainInternal(View.java:5465)
         at android.view.ViewGroup.handleFocusGainInternal(ViewGroup.java:714)
         at android.view.View.requestFocusNoSearch(View.java:8470)
         at android.view.View.requestFocus(View.java:8449)
         at android.view.ViewGroup.requestFocus(ViewGroup.java:2747)
         at android.view.View.requestFocus(View.java:8416)
         at android.support.v4.widget.NestedScrollView.arrowScroll(NestedScrollView.java:1222)
         at android.support.v4.widget.NestedScrollView.executeKeyEvent(NestedScrollView.java:551)
         at android.support.v4.widget.NestedScrollView.dispatchKeyEvent(NestedScrollView.java:512)
         at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
         at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
+3
source share
2 answers

RecyclerView. , , com.android.support:recyclerview-v7:23.2.0
. :
https://code.google.com/p/android/issues/detail?id=190526&thanks=190526&ts=1445108573

:
RecyclerView (, ). XML:
android:focusable="true"

, .

+4

. .

    @Override
public void requestChildFocus(View child, View focused) {
    super.requestChildFocus(child, focused);
    View view = focused;
    while (view != null && view.getParent() != child) {
        try {
            view = (View) view.getParent();
        } catch (ClassCastException e) {
            view = null;
        }
    }
    mSelectedPosition = view == null ? -1 : ((ViewGroup) child).indexOfChild(view);
    if (DEBUG)
        Log.v(TAG, "requestChildFocus focused " + focused + " mSelectedPosition " + mSelectedPosition);
}
+1

Source: https://habr.com/ru/post/1627313/


All Articles