I have an application which I need adapt for Android TV. This application contains horizontal RecyclerView and it doesn't scroll when I press D-pad buttons on remote control. I found this solution, but it crashes. 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;
Crash occure in this place:
@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)
To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .
Does RecyclerView automatically scroll? There is a method in RecyclerView which is smoothScrollToPosition() which take position of recyclerview to scroll that position to current position.
getHeight(); recyclerView. smoothScrollToPosition(height); recyclerView. postDelayed(new Runnable() { public void run() { recyclerView. smoothScrollToPosition(0); } },200);
How can show all items in RecyclerView without scrolling? In RecyclerView use android:nestedSrollingEnabled="false" and use NestedScrollView as a parent Scroll View. This was the only answer that worked for me - I had a ScrollView wrapping two RecyclerViews.
Use the lastest version of RecyclerView.
Or use at least com.android.support:recyclerview-v7:23.2.0
See this link for more info:
https://code.google.com/p/android/issues/detail?id=190526&thanks=190526&ts=1445108573
Now for the important part:
New versions of RecyclerView started to obey the rules of its children (like height and width).
You must set your root view in child item XML to: android:focusable="true"
Now, scrolling will go like it was intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With