如何在 Android 电视上移动网格项目?

How do I move grid items on Android TV?

我正在为 Android TV 开发网格界面(使用 VerticalGridSupportFragment),我正在寻找一种允许用户在网格中移动项目的方法。

想法是网格包含多个电视频道并且用户应该能够更改电视频道的顺序(排序/重新排序)。我建议的解决方案是通过单击 select 频道。然后频道变为 "sticky" 允许您移动它。当您对位置感到满意时,您再次单击该频道,确认其新位置。

显而易见的解决方案是按照以下方式做一些事情:

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
    }
}

问题是 adapter.move() 导致另一个子 selected 事件。

我试图通过暂时删除 selection 侦听器来避免这个问题,而是保留 ObjectAdapter.DataObserver 来通知我 onItemMoved() 事件,我在其中设置 selected 位置并再次设置 selection 侦听器。

这似乎也不完全有效。

无法使用 ItemTouchHelper,因为它是为触摸目的而设计的,而不是像我们在 Android 电视上那样使用遥控器。

官方 Android 电视启动器应用程序正在做一些类似于我在主屏幕上重新排列应用程序快捷方式时需要做的事情,但我想不出一种方法来让它工作。

找到了一个解决方案,这似乎也是 Google 用于 Android 电视启动器的解决方案。

简而言之:创建自定义 VerticalGridView 并覆盖其 focusSearch() 方法以确定如何移动/交换项目。

与此类似的内容:

class EditableVerticalGridView @JvmOverloads constructor(context: Context,
                                                         attrs: AttributeSet? = null,
                                                         defStyle: Int = 0) :
        VerticalGridView(context, attrs, defStyle) {

    override fun focusSearch(focused: View, direction: Int): View {
        return if (focused.isSelected) {
            swapItemsIfNeeded(focused, direction)
        } else super.focusSearch(focused, direction)
    }

    private fun swapItemsIfNeeded(focused: View, direction: Int): View {
        val position = getChildAdapterPosition(focused)
        if (!itemAnimator.isRunning) {
            if (canMoveInDirection(position, direction)) {
                when (direction) {
                    FOCUS_LEFT -> moveChannel(position, position - 1)
                    FOCUS_UP -> moveChannel(position, position - NUM_COLUMN)
                    FOCUS_RIGHT -> moveChannel(position, position + 1)
                    FOCUS_DOWN -> moveChannel(position, position + NUM_COLUMN)
                }
            }
        }
        return focused
    }

    private fun canMoveInDirection(position: Int, direction: Int): Boolean {
        when (direction) {
            FOCUS_LEFT -> {
                return position % NUM_COLUMN > 0
            }
            FOCUS_UP -> {
                return position - NUM_COLUMN >= 0
            }
            FOCUS_RIGHT -> {
                return !(position % NUM_COLUMN >= (NUM_COLUMN - 1) ||
                        position >= adapter.itemCount - 1)
            }
            FOCUS_DOWN -> {
                return position + NUM_COLUMN <= adapter.itemCount - 1
            }
            else -> {
                return false
            }
        }
    }

    private fun moveChannel(fromPosition: Int, toPosition: Int) {
        (adapter as AllowedChannelAdapter).moveChannel(fromPosition, toPosition)
    }

    companion object {

        private const val NUM_COLUMN: Int = 6

    }

}

... 和 moveChannel() 函数:

fun moveChannel(from: Int, to: Int) {
        var offset = 1
        if (from >= 0 && from <= channelItems.size - 1 && to >= 0 && to <= channelItems.size - 1) {
            val fromItem = channelItems[from]
            channelItems[from] = channelItems[to]
            channelItems[to] = fromItem
            notifyItemMoved(from, to)

            val positionDifference = to - from
            if (Math.abs(positionDifference) > 1) {
                if (positionDifference > 0) {
                    offset = -1
                }
                notifyItemMoved(to + offset, from)
            }
        }
    }