Skip to content

Touch Support

Eli Hart edited this page Aug 15, 2017 · 12 revisions

(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:

Sample touch support gif

Check out the code of the sample app for implementation details.

Basic Usage

The easiest way to set up touch support is through the EpoxyTouchHelper class.

Dragging

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 DragCallback<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(T model, View itemView, int adapterPosition) {
    
        }
    
        @Override
        public void onDragReleased(T model, View itemView) {
    
        }
    
        @Override
        public void clearView(T model, View itemView) {
    
        }
});

Swiping

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(T model, View itemView, int adapterPosition) {
    
        }
    
        @Override
        public void onSwipeProgressChanged(T model, View itemView, float swipeProgress) {
    
        }
    
        @Override
        public void onSwipeReleased(T model, View itemView) {
    
        }
    
        @Override
        public void clearView(T model, View itemView) {
    
        }

Further Configuration

Clone this wiki locally