EDIT: Реальная проблема заключалась в том, что мой LinearLayout
был завернут в другой макет, что вызвало неправильное поведение. В принятом ответе Sanvywell есть лучший, более полный пример того, как рисовать цвет под видимым просмотром, чем фрагмент кода, который я предоставил в вопросе.
Теперь, когда RecyclerView виджет имеет встроенную поддержку для прокрутки строки с помощью ItemTouchHelper, я пытаюсь использовать его в приложении, где строки будут вести себя аналогично приложению Google Inbox. То есть, прокручивание влево выполняет одно действие, а прокручивание вправо делает другое.
Реализация самих действий была легко с помощью метода ItemTouchHelper.SimpleCallback onSwiped
. Тем не менее, мне не удалось найти простой способ установить цвет и значок, который должен отображаться под видом, который в настоящее время выполняется (например, в приложении "Входящие" в Google).
Чтобы сделать это, я пытаюсь переопределить метод ItemTouchHelper.SimpleCallback onChildDraw
следующим образом:
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
RecyclerViewAdapter.ViewHolder vh = (RecyclerViewAdapter.ViewHolder) viewHolder;
LinearLayout ll = vh.linearLayout;
Paint p = new Paint();
if(dX > 0) {
p.setARGB(255, 255, 0, 0);
} else {
p.setARGB(255, 0, 255, 0);
}
c.drawRect(ll.getLeft(), ll.getTop(), ll.getRight(), ll.getBottom(), p);
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
Определение направления движения от dX и установка соответствующего цвета работает по назначению, но координаты, которые я получаю от ViewHolder
, всегда соответствуют тому, где был надут первый LinearLayout
.
Как получить правильные координаты для LinearLayout
в текущей строке? Есть ли более простой способ (который не требует переопределения onChildDraw
) для установки цвета фона и значка?