私は Android TV アプリを実装してHorizontalGridViewおり、Leanback library. カスタムレイアウトがあります。
アクティビティが作成された後、特定の位置にスクロールする必要がありますHorizontalGridViewが、残念ながら、scrollToPositio(position)このレイアウトではメソッドがまったく機能していません。何もしないだけです。を具体的に設定すると、それが機能することがわかりましlayout managerたLinearLayoutManager。leanbackしかし、問題は、 defaultを使用していない場合HorizontalGridView LayoutManager、D-pad を使用して次の項目にフォーカスする際に問題があることです。
基本的にノーマルを使うと十字キーRecyclerViewでの操作が思い通りに動かないのでleanback実装で解決することにしたのですが、今のところscrollToPositionメソッドで動かせません。
何か案は?
私のコードのスニペット:
レイアウト:
<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