Я пытаюсь использовать новый класс RecyclerView
для сценария, где я хочу, чтобы компонент привязывался к определенному элементу при прокрутке (старый Android Gallery
приходит на ум в качестве примера такого списка с центром -блокированный элемент).
Это подход, который я принимаю до сих пор:
У меня есть интерфейс ISnappyLayoutManager
, который содержит метод getPositionForVelocity
, который вычисляет, в какой позиции представление должно заканчивать прокрутку с учетом начальной скорости движения.
public interface ISnappyLayoutManager {
int getPositionForVelocity(int velocityX, int velocityY);
}
Тогда у меня есть класс, SnappyRecyclerView
, который подклассы RecyclerView
и переопределяет его метод fling() таким образом, чтобы отображать точное точное количество:
public final class SnappyRecyclerView extends RecyclerView {
/** other methods deleted **/
@Override
public boolean fling(int velocityX, int velocityY) {
LayoutManager lm = getLayoutManager();
if (lm instanceof ISnappyLayoutManager) {
super.smoothScrollToPosition(((ISnappyLayoutManager) getLayoutManager())
.getPositionForVelocity(velocityX, velocityY));
}
return true;
}
}
Я не очень доволен этим подходом по нескольким причинам. Прежде всего, похоже, что философия "RecyclerView" должна подклассифицировать ее для реализации определенного типа прокрутки. Во-вторых, если я хочу просто использовать значение по умолчанию LinearLayoutManager
, это становится несколько сложным, поскольку я должен возиться со своими внутренностями, чтобы понять его текущее состояние прокрутки и рассчитать, где именно это происходит. Наконец, это даже не позаботится обо всех возможных сценариях прокрутки, как если бы вы переместили список, а затем приостановили, а затем подняли палец, не произошло никакого события перехода (скорость слишком низкая), и поэтому список остается на полпути должность. Об этом можно позаботиться, добавив слушателя состояния прокрутки в RecyclerView
, но это также очень хладнокровно.
Я чувствую, что мне что-то не хватает. Есть ли лучший способ сделать это?