Как переместить элементы сетки на Android TV?

Вот чистая реализация python желаемой функциональности, которая обрабатывает любые числовые ряды (int или float) с положительными, отрицательными или смешанными значениями:

def computeTicks (x, step = 5):
    """
    Computes domain with given step encompassing series x
    @ params
    x    - Required - A list-like object of integers or floats
    step - Optional - Tick frequency
    """
    import math as Math
    xMax, xMin = Math.ceil(max(x)), Math.floor(min(x))
    dMax, dMin = xMax + abs((xMax % step) - step) + (step if (xMax % step != 0) else 0), xMin - abs((xMin % step))
    return range(dMin, dMax, step)

Образец вывода:

# Negative to Positive
series = [-2, 18, 24, 29, 43]
print(list(computeTicks(series)))

[-5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45]

# Negative to 0
series = [-30, -14, -10, -9, -3, 0]
print(list(computeTicks(series)))

[-30, -25, -20, -15, -10, -5, 0]

# 0 to Positive
series = [19, 23, 24, 27]
print(list(computeTicks(series)))

[15, 20, 25, 30]

# Floats
series = [1.8, 12.0, 21.2]
print(list(computeTicks(series)))

[0, 5, 10, 15, 20, 25]

# Step – 100
series = [118.3, 293.2, 768.1]
print(list(computeTicks(series, step = 100)))

[100, 200, 300, 400, 500, 600, 700, 800]

и пример использования:

import matplotlib.pyplot as plt

x = [0,5,9,10,15]
y = [0,1,2,3,4]
plt.plot(x,y)
plt.xticks(computeTicks(x))
plt.show()
1
задан Michell Bak 17 January 2019 в 12:52
поделиться

1 ответ

Нашел решение, которое, похоже, и используется 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)
            }
        }
    }
0
ответ дан Michell Bak 17 January 2019 в 12:52
поделиться
Другие вопросы по тегам:

Похожие вопросы: