I'm working on a grid interface (using VerticalGridSupportFragment
) for Android TV and I'm looking for a way to allow users to move around items in the grid.
The idea is that the grid contains a number of TV channels and that the users should be able to change the order of the TV channels (sort / reorder). My proposed solution is to select a channel by clicking it. The channel then becomes "sticky" allowing you to move it around. When you're happy with the position, you click on the channel again, confirming its new position.
The obvious solution is to do something along the lines of this:
getVerticalGridView()?.let {
it.setOnChildSelectedListener { _, _, position, _ ->
// Move the item in the previous position to the new position
adapter.move(oldPosition, position)
// Set old position to currently selected position.
oldPosition = position
}
}
fun VerticalGridSupportFragment.getVerticalGridView(): VerticalGridView? {
return VerticalGridSupportFragment::class.java.getDeclaredField("mGridViewHolder")?.let {
it.isAccessible = true
return (it.get(this) as VerticalGridPresenter.ViewHolder).gridView
}
}
The problem with this is that adapter.move()
causes another child selected event.
I've tried to circumvent this issue by temporarily removing the selection listener and instead keep a ObjectAdapter.DataObserver to notify me of onItemMoved()
events, in which I set the selected position and once again set a selection listener.
This doesn't seem to work fully either.
It's not possible to use ItemTouchHelper
as that was designed for touch purposes and not using a remote like we do on Android TV.
The official Android TV launcher app is doing something similar to what I need when you rearrange app shortcuts on the homescreen, but I can't think of a way to make it work.