Android TV Leanback RowsSupportFragment удалить строку
Реализую расширение фрагмента.
Постановка проблемы: у меня есть список удаленных источников
source
который содержит список списков, которые выбираются асинхронно при привязке строки списка. После возвращения списков некоторые из них пусты и их нужно удалить из пользовательского интерфейса: их нужно удалить из ListRowPresenter / ObjectAdapter, но я не могу определить, как полностью удалить строку списка без сбоя приложения. Исключение составляет следующее:
at androidx.leanback.widget.ListRowPresenter.onBindRowViewHolder(ListRowPresenter.java:661)
at androidx.leanback.widget.RowPresenter.onBindViewHolder(RowPresenter.java:601)
at androidx.leanback.widget.Presenter.onBindViewHolder(Presenter.java:140)
at androidx.leanback.widget.ItemBridgeAdapter.onBindViewHolder(ItemBridgeAdapter.java:422)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7308)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6175)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6441)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6281)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6277)
at androidx.leanback.widget.GridLayoutManager.getViewForPosition(GridLayoutManager.java:1102)
at androidx.leanback.widget.GridLayoutManager$2.createItem(GridLayoutManager.java:1628)
at androidx.leanback.widget.SingleRow.appendVisibleItems(SingleRow.java:113)
at androidx.leanback.widget.Grid.appendOneColumnVisibleItems(Grid.java:389)
at androidx.leanback.widget.GridLayoutManager.appendOneColumnVisibleItems(GridLayoutManager.java:1852)
at androidx.leanback.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:2282)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4277)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3980)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4546)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.widget.ScrollView.onLayout(ScrollView.java:1552)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
Вот что я пробовал:
вызов
topLevelRowsAdapter.clear(indexOfRowToRemove)
вылетает приложение.
вызов
topLevelRowsAdapter[indexOfRowToRemove] = ListRow(ArrayObjectAdapter())
удаляет видимую строку, но оставляет ту, на которую может попасть курсор при прокрутке вверх и вниз.
Вот упрощенная версия того, что я пытаюсь сделать:
class MyFragment : RowsSupportFragment() {
private val listRowPresenter = ListRowPresenter(FocusHighlight.ZOOM_FACTOR_NONE)
private val topLevelRowsAdapter = SparseArrayObjectAdapter(listRowPresenter)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
topLevelRowsAdapter.setHasStableIds(true)
source.forEachIndexed { index, listToGet ->
val adapterForRow =
ArrayObjectAdapter()
// the list to get is bound to the UI, and the index corresponds to the ListRow that the list lives in -- this is passed so we can update
adapterForRow.add(Pair(index, listToGet))
topLevelRowsAdapter[newIndex] =
ListRow(HeaderItem(listToGet.headerText), adapterForRow)
}
}
this.adapter = topLevelRowsAdapter
}
// After a row is bound and the network request comes back this is called.
override fun asyncCallback(listToGet: Pair<Int, CustomListObject>) {
onTransitionPrepare()
onTransitionStart()
if (CustomListObject.listsForRow.isNotEmpty()) {
// List from network is not empty, update the UI with the items that came back. This works!
val updatingRow = (topLevelRowsAdapter[listToGet.first] as? ListRow)
(updatingRow?.adapter as ArrayObjectAdapter).setItems(
CustomListObject.listsForRow,
diffUtil
)
} else {
// The list came back empty and it needs to be removed from the UI
// This causes a crash
topLevelRowsAdapter.clear(listToGet.first)
// This works but there is a "blank" row that the cursor can land on
topLevelRowsAdapter[listToGet.first] = ListRow(ArrayObjectAdapter())
}
onTransitionEnd()
}
}
Я также попытался заменить строку пустым подклассом строки
ListRow
как показано ниже, но при прокрутке он все еще фокусируется:
class EmptyRow : ListRow(HeaderItem(null), ArrayObjectAdapter()) {
override fun isRenderedAsRowView(): Boolean {
return false
}
}
Я что-то упускаю или столкнулся с ограничением функции Leanback?
RowsSupportFragment
реализация?