An easy to use helper for RecyclerViews. StackLayoutManager and ItemTouchHelperCallback.
Artifact | Version |
---|---|
recycler-swipe-stack |
implementation("com.kroegerama:recycler-swipe-stack:$VERSION")
val config = SwiperConfig(
showCount = 5,
swipeDirections = SwipeDirection.LEFT or SwipeDirection.RIGHT or SwipeDirection.UP,
stackDirection = StackDirection.Down,
itemTranslate = 0.02f,
itemRotation = 15f,
itemScale = .0f
)
private val listener = object : SwipeListener {
override fun onSwipe(
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
direction: Int
) {
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
Timber.d("onSwiped($direction)")
//example: remove item from Adapter (and call notifyItemRemoved,
//if your adapter doesn't do it automatically)
itemAdapter.removeAt(viewHolder.bindingAdapterPosition)
}
override fun onSwipeEnd(viewHolder: RecyclerView.ViewHolder) {
Timber.d("onSwipeEnd()")
}
override fun isSwipeAllowed(viewHolder: RecyclerView.ViewHolder) = viewHolder is MyViewHolder
}
recycler.setupStack(
config,
listener
) {
itemAnimator = DefaultItemAnimator().apply {
addDuration = 200
removeDuration = 200
}
adapter = myAdapter
}
val itemTouchHelper = ItemTouchHelper(StackSwipeTouchHelperCallback(listener, config))
val layoutManager = StackLayoutManager(config)
recycler.layoutManager = layoutManager
itemTouchHelper.attachToRecyclerView(recycler)