Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 1차원 DP
- 2차원 dp
- 99클럽
- @Builder
- @GeneratedValue
- @GenericGenerator
- @NoargsConstructor
- @Transactional
- Actions
- Amazon EFS
- amazon fsx
- Android Studio
- ANSI SQL
- ApplicationEvent
- assertThat
- async/await
- AVG
- AWS
- Azure
- bind
- builder
- button
- c++
- c++ builder
- c03
- Callback
- case when
- CCW
- chat GPT
- CICD
Archives
- Today
- Total
기록
androidStudio/kotlin/RecyclerView Swipe, Drag 본문
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
'Moblie > Android' 카테고리의 다른 글
androidStudio/ProgressBar Style Custom (0) | 2022.06.19 |
---|---|
AndroidStudio/나인 패치(9-Path) 이미지 (0) | 2022.06.17 |
[issue] 이미지 해상도 처리하기 (0) | 2022.06.01 |
AndroidStudio/java/ 멀티뷰 타입 Recyclerview (0) | 2022.05.29 |
androidStudio/kotlin/preference (0) | 2022.05.26 |
Comments