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

时间:2019-01-17 12:34:28

标签: android android-recyclerview leanback

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

这个想法是网格包含许多电视频道,并且用户应该能够更改电视频道的顺序(排序/重新排序)。我建议的解决方案是通过单击选择一个频道。通道然后变为“粘性”,可让您四处移动。对位置感到满意后,请再次单击该频道,以确认其新位置。

显而易见的解决方案是按照以下步骤进行操作:

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()导致另一个子级选择事件。

我试图通过暂时删除选择侦听器,而保留ObjectAdapter.DataObserver来通知我onItemMoved()事件,以解决此问题,在该事件中,我设置了所选位置,并再次设置了一个选择侦听器。

这似乎也无法完全正常工作。

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

当您在主屏幕上重新排列应用程序快捷方式时,官方的Android TV启动器应用程序正在执行与我所需的操作类似的操作,但是我想不出一种使之起作用的方法。

1 个答案:

答案 0 :(得分:0)

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

简而言之:创建一个自定义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)
            }
        }
    }