Android SwipeRefreshLayout left and right sliding conflict resolution

created at 07-27-2021 views: 4

I have a page that needs to be pulled down to refresh, so I wrap <SwipeRefreshLayout> in the outer layer of <RecyclerView>, but at the same time I hope that the components in <RecyclerView> can support left and right sliding, for example, sliding right to delete, sliding left to edit.

The answer is to achieve through ItemTouchHelper, such as:


private fun createItemTouchCallback(adapter: BKRecyclerViewAdapter): ItemTouchHelper.SimpleCallback {
    // ...

After the implementation is completed, it is indeed possible to slide left and right, but it will bring a small problem, that is, SwipeRefreshLayout is particularly sensitive. When sliding left and right, with a little downward direction, it will trigger the pull-down refresh, causing the left and right sliding to be cancelled. What is the solution? The solution is to check the amount of left and right sliding, and then when the horizontal offset reaches a certain level, disable the pull-down refresh, when it is restored, or after the sliding is completed, re-enable the pull-down refresh:

override fun onChildDraw(
    c: Canvas,
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder,
    dX: Float,
    dY: Float,
    actionState: Int,
    isCurrentlyActive: Boolean
) {
    val textMargin = resources.getDimension(R.dimen.mdtp_ampm_label_size).roundToInt()
    val width = viewHolder.itemView.width
    val absDX = abs(dX)
    main_swipe_refresh.isEnabled = !(5 <= absDX && absDX <= viewHolder.itemView.width - 5)  // The point is here
    // ...

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
    main_swipe_refresh.isEnabled = true
    // ...

The above, pull-down refresh is disabled if it exceeds 5 pixels. It is a parameter that I adjusted by myself. You can adjust it on the device yourself.

created at:07-27-2021
edited at: 07-27-2021: