기록

androidStudio/kotlin/RecyclerView Swipe, Drag 본문

Moblie/Android

androidStudio/kotlin/RecyclerView Swipe, Drag

youngyin 2022. 6. 6. 10:00

1. 개요

(1) recyclerview 만들기

(2) data controller 추가

(3) callBack custom

(4) Swipe시 bakcground 설정

(5) adapter에 listener 연결하기

2. code

(1) recyclerview 만들기

2022.03.29 - [안드로이드/예제] - androidStudio/kotliln/recyclerview 사용하기

(2) data controller 추가

class MyAdapter
    : RecyclerView.Adapter<MyAdapter.ViewHolder>(){

    ...
    
    fun swapData(fromPos: Int, toPos: Int){
        Collections.swap(dataset, fromPos, toPos)
        notifyItemMoved(fromPos, toPos)
    }

    fun removeData(pos : Int){
        dataset.removeAt(pos)
        notifyItemRemoved(pos)
    }
}

(3) callBack custom

class MyItemTouchCallback(
    val adapter : MyAdapter,
    val context: Context
) : ItemTouchHelper.SimpleCallback(
ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.LEFT){
    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        val fromPos: Int = viewHolder.adapterPosition
        val toPos: Int = target.adapterPosition
        adapter.swapData(fromPos, toPos)
        return true
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        adapter.removeData(viewHolder.layoutPosition)
    }

    fun dpToPx(dp: Int):Float{
        val density = context.resources.displayMetrics.density
        val px = (dp * density).roundToInt()
        return px.toFloat()
    }
}

(4) Swipe시 bakcground 설정

참고

더보기
class MyItemTouchCallback(
    val adapter : MyAdapter,
    val context: Context,
    val resources: Resources?
) : ItemTouchHelper.SimpleCallback(
ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.LEFT){
    ...

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

        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
            if (dX < 0){
                // rect loc
                val rectDst = RectF(
                    itemView.right.toFloat()+dX+0f,
                    itemView.top.toFloat()+0f,
                    itemView.right.toFloat()+0f,
                    itemView.bottom.toFloat()+0f)

                // draw rect
                paint.style = Paint.Style.STROKE
                paint.strokeWidth = dpToPx(3)
                paint.color = Color.parseColor("#FF3D55C2")
                c.drawRect(rectDst, paint)

                // close icon loc
                val icon = BitmapFactory.decodeResource(resources, R.drawable.notice_close)
                val iconSize = dpToPx(24)
                val width = rectDst.right+rectDst.left
                val height = rectDst.bottom+rectDst.top
                val centerHor = width/2
                val centerVer = height/2
                val iconDst = RectF(
                    centerHor-iconSize/2,
                    centerVer-iconSize/2,
                    centerHor+iconSize/2,
                    centerVer+iconSize/2)

                // draw icon
                if (rectDst.right-rectDst.left>=iconSize) {
                    c.drawBitmap(icon, null, iconDst, null)
                }
            }
        }

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

(5) adapter에 listener 연결하기

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        ...

        // add touch listener
        val itemTouchCallback = MyItemTouchCallback(noticeAdapter, baseContext, resources)
        ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recycler)
    }
}

3. 결과

4. 더 공부할 것

  • canvas, paint
  • ItemTouchHelper

https://developer.android.com/reference/androidx/recyclerview/widget/ItemTouchHelper

 

ItemTouchHelper  |  Android Developers

androidx.health.platform.client.error

developer.android.com

Comments