Android TVアプリケーションを実装しており、Leanback library
のHorizontalGridView
を使用しています。私はカスタムレイアウトを持っています。Android LeanbackライブラリHorizontalGridView scrollToPositionが機能しない
アクティビティが作成された後にHorizontalGridView
を特定の位置にスクロールする必要がありますが、残念ながらscrollToPositio(position)
メソッドはこのレイアウトではまったく動作しません。それは何もしません。私が具体的にlayout manager
をLinearLayoutManager
に設定すると、それが機能することがわかりました。しかし問題は、私がleanback
のデフォルト値HorizontalGridView LayoutManager
を使用していないときは、Dパッドを使って次のアイテムに焦点を合わせることに問題があるということです。私はRecyclerView
ノーマルを使用している場合
は基本的には、Dパッドで制御が期待通りに動作していないので、私は、この問題はこれまでのところ、私はそれがscrollToPosition
方法で動作させることができない解決されているが、leanback
実装、一緒に行くことにしました。
アイデア?
私のコードのスニペット:
レイアウト:
<android.support.v17.leanback.widget.HorizontalGridView
android:id="@+id/photo_gallery_recycler"
android:layout_width="match_parent"
android:layout_height="@dimen/gallery_image_size"
android:clipChildren="false"
app:itemView="@{viewModel.photoItemView}"
app:items="@{viewModel.photosUrl}"/>
コード[Kotlin]:
binding.photoGalleryRecycler.scrollToPosition(position)
binding.photoGalleryRecycler.getChildAt(position)?.requestFocus()
そして私はまた、このようないくつかのハックを試してみました:
// save default leanback layout manager
var defaultLayoutManager = binding.photoGalleryRecycler.layoutManager
// set LinearLayoutManager
binding.photoGalleryRecycler.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
// scroll to position and request focus
binding.photoGalleryRecycler.scrollToPosition(position)
binding.photoGalleryRecycler.getChildAt(position)?.requestFocus()
// set default layout manager back to the view
binding.photoGalleryRecycler.layoutManager = defaultLayoutManager
を試すことができます。 –
実際には、モバイルバージョンでは、水平のLinearLayoutManagerを使用して通常のRecyclerViewを表示できます。残念なことに、テレビのDパッドを使ったコントロールは、RecyclerViewの右アイテムに焦点を当てるといくつかの問題を抱えています。そのため、次の項目のフォーカスを数えることでこの問題を解決したleanbackライブラリから実装を使用する必要がありましたが、何らかの理由でscrollToPositionが。 – Sandak
@Dhina:Your @Dhina:(setSelectedPosition(int))(http://developer.android.com/intl/zh-cn/reference/android/support/v17/leanback/widget/HorizontalGridView.html#setSelectedPosition(int)) – Dhina