2016-10-04 78 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。 –

相關問題