PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/util/SwipeAndDragHelper.java

74 lines
2.3 KiB
Java
Raw Normal View History

2019-07-22 15:26:37 +00:00
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package code.name.monkey.retromusic.util;
import android.graphics.Canvas;
2019-10-29 14:49:28 +00:00
import androidx.annotation.NonNull;
2019-07-22 15:26:37 +00:00
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
2019-07-22 15:26:37 +00:00
public class SwipeAndDragHelper extends ItemTouchHelper.Callback {
2021-10-11 06:17:55 +00:00
private final ActionCompletionContract contract;
2019-07-22 15:26:37 +00:00
2020-10-06 08:46:04 +00:00
public SwipeAndDragHelper(@NonNull ActionCompletionContract contract) {
this.contract = contract;
}
2019-07-22 15:26:37 +00:00
2020-10-06 08:46:04 +00:00
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
2020-10-06 08:46:04 +00:00
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
return makeMovementFlags(dragFlags, 0);
}
2019-07-22 15:26:37 +00:00
2020-10-06 08:46:04 +00:00
@Override
public boolean onMove(
@NonNull RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
2020-10-06 08:46:04 +00:00
contract.onViewMoved(viewHolder.getLayoutPosition(), target.getLayoutPosition());
return true;
}
2019-07-22 15:26:37 +00:00
2020-10-06 08:46:04 +00:00
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {}
2019-07-22 15:26:37 +00:00
2020-10-06 08:46:04 +00:00
@Override
public boolean isLongPressDragEnabled() {
return false;
}
2019-07-22 15:26:37 +00:00
2020-10-06 08:46:04 +00:00
@Override
public void onChildDraw(
@NonNull Canvas c,
@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder,
float dX,
float dY,
int actionState,
boolean isCurrentlyActive) {
2020-10-06 08:46:04 +00:00
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
float alpha = 1 - (Math.abs(dX) / recyclerView.getWidth());
viewHolder.itemView.setAlpha(alpha);
2019-07-22 15:26:37 +00:00
}
2020-10-06 08:46:04 +00:00
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
2019-07-22 15:26:37 +00:00
2020-10-06 08:46:04 +00:00
public interface ActionCompletionContract {
void onViewMoved(int oldPosition, int newPosition);
}
2019-07-22 15:26:37 +00:00
}