2016-10-04 49 views
3

我有一个应用程序,我需要适应Android TV。此应用程序包含水平的RecyclerView,当按遥控器上的D-pad按钮时,它不会滚动。我发现,但它崩溃。 下面是代码:如何在Android TV上的RecyclerView中实现滚动?

<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> 

Horizo​​ntalPersistentFocusWrapper相同PersistentFocusWrapper但mPersistFocusVertical = FALSE;

崩溃occure在这个地方:

@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); 
    } 

崩溃堆栈跟踪:

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) 

回答

4

使用最新版本的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"

现在,滚动会去。

+0

谢谢!解决了我的问题...现在我可以在Recyclerview中滚动 – skm

1

试试这个。适用于我。

@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); 
} 
+0

感谢您的回答。使用此代码crashe不会发生。但RecyclerView不会在d-pad上发生反应。 – BArtWell

+1

只是为了确保。 Horizo​​ntalPersistentFocusWrapper用于使用垂直LinearLayout管理器和其他可能的视图来包装RecyclerView。要用水平LineaLayoutManagers包装RecyclerViews,请使用VerticalPersistentFocusWrapper。 –

相关问题