如何在 recyclerview 中添加从左向右滑动 [英] How To add swipe Left to Right in recyclerview

查看:46
本文介绍了如何在 recyclerview 中添加从左向右滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的回收视图添加滑动功能.我正在关注此链接以添加滑动.

I am trying to add swipe function for my recyclerview. I am following this link for adding swipe. https://github.com/nikhilpanju/RecyclerViewEnhanced/blob/master/recyclerviewenhanced/src/main/java/com/nikhilpanju/recyclerviewenhanced/RecyclerTouchListener.java

In this link I can do only Right to left swipe. I want add Left to Right swipe. I tried to add functionality in onInterceptTouchEvent. But I can not do the Left to Right Swipe. Can any one help me to add Left to Right swipe?

解决方案

To implement such behavior in a RecyclerView, you need to declare an ItemTouchHelper, like so:

// The class to detect swipes and drags
private lateinit var itemTouchHelper: ItemTouchHelper

The ItemTouchHelper class has a ItemTouchHelper.SimpleCallback.onChildDraw callback method, where you can detect if a user swept right or left on an item in your recyclerview. So next, we'll now implement this callback, like so:

private fun setupRecyclerView() {
    val myRecyclerView = findViewById<RecyclerView>(R.id.myRecyclerView)
    val customAdapter = CustomAdapter()
    myRecyclerView.adapter = customAdapter

    val simpleCallback = object :
        ItemTouchHelper.SimpleCallback(
            0,
            ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
        ) {
        override fun onMove(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            target: RecyclerView.ViewHolder
        ): Boolean = false

        override fun onChildDraw(
            c: Canvas,
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            dX: Float,
            dY: Float,
            actionState: Int,
            isCurrentlyActive: Boolean
        ) {

           // If you want to add a background, a text, an icon
          //  as the user swipes, this is where to start decorating
          //  I will link you to a library I created for that below

            super.onChildDraw(
                c,
                recyclerView,
                viewHolder,
                dX,
                dY,
                actionState,
                isCurrentlyActive
            )
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
            val position = viewHolder.adapterPosition
            when (direction) {
                ItemTouchHelper.LEFT -> {
                   // Do something when a user swept left
                }
                ItemTouchHelper.RIGHT -> {
                  // Do something when a user swept right
                }
            }
        }
    }
    itemTouchHelper = ItemTouchHelper(simpleCallback)
    itemTouchHelper.attachToRecyclerView(myRecyclerView)
}

Now that swipe behavior will now be listened in your recyclerview. Also, in case you want a background, icon or text to be shown as the user of your app swipes, you can use this awesome library I created just for that usage. you can download it to your project if you're using gradle. Here's the link to the documentation for it: https://github.com/kevingermainbusiness/ItemDecorator

这篇关于如何在 recyclerview 中添加从左向右滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆