-
Notifications
You must be signed in to change notification settings - Fork 727
Touch Support
(Since 2.3.0)
Epoxy helps you easily integrate RecyclerView's ItemTouchHelper with your Epoxy controllers and models. There is support for both dragging and swiping views.
The sample app demonstrates a bit of what is possible:
Check out the code of the sample app for implementation details.
The easiest way to set up touch support is through the EpoxyTouchHelper
class.
You can easily enable long press to drag and rearrange views like this:
EpoxyTouchHelper.initDragging(epoxyController) // an EpoxyController must be used
.withRecyclerView(recyclerView) // The recyclerview the controller is used with
.forVerticalList() // Specify the directions that you want to drag in
.withTarget(MyModel.class) // Specify the type of model or models that should be draggable
.andCallbacks(new DragCallbacks<MyModel>() {
@Override
public void onModelMoved(int fromPosition, int toPosition,
MyModel modelBeingMoved, View itemView) {
// Called when a view has been dragged to a new position.
// Epoxy will automatically update the models in the controller and notify
// the RecyclerView of the move
// You MUST use this callback to update your data to reflect the move
}
// You may optionally implement the below methods as hooks into the drag lifecycle.
// This allows you to style or animate the view as it is dragged.
@Override
public void onDragStarted(MyModel model, View itemView, int adapterPosition) {
}
@Override
public void onDragReleased(MyModel model, View itemView) {
}
@Override
public void clearView(MyModel model, View itemView) {
}
@Override
public boolean isDragEnabledForModel(MyModel model) {
// Override this to toggle disabling dragging for a model
return true;
}
});
Initialize swiping like this:
EpoxyTouchHelper.initSwiping(recyclerView)
.leftAndRight() // Which directions to allow
.withTarget(MyModel.class) // Specify the type of model or models that should be swipable
.andCallbacks(new SwipeCallbacks<MyModel>() {
@Override
public void onSwipeCompleted(MyModel model, View itemView, int position,
int direction) {
// Use must use this callback to update data to remove the item at the given position
// You must also request a model build on the EpoxyController once the data is updated
}
});
// You may optionally implement the below methods as hooks into the swipe lifecycle.
// This allows you to style or animate the view as it is swiped.
@Override
public void onSwipeStarted(MyModel model, View itemView, int adapterPosition) {
}
@Override
public void onSwipeProgressChanged(MyModel model, View itemView, float swipeProgress) {
}
@Override
public void onSwipeReleased(MyModel model, View itemView) {
}
@Override
public void clearView(MyModel model, View itemView) {
}
@Override
public boolean isSwipeEnabledForModel(MyModel model) {
// Override this to toggle disabling swipe for a model
return true;
}
EpoxyTouchHelper
makes it very simple to set up dragging and swiping, and also provides a fair amount of flexibility in configuring them. However, if you want more control over the configuration you may implement EpoxyModelTouchCallback
directly and create your own ItemTouchHelper
instance.
The EpoxyModelTouchCallback
provides a number of wrapper methods to make working with Epoxy models simpler, while still providing direct access to the underlying RecyclerView touch callbacks.
If you want even more control, you could implement EpoxyTouchHelperCallback
, which is just a small wrapper over ItemTouchHelper.Callback
to convert all viewholder parameters to Epoxy view holders.