Skip to content

Commit a67b617

Browse files
committed
2 parents b559b64 + 998c6b8 commit a67b617

4 files changed

Lines changed: 56 additions & 23 deletions

File tree

HoldableSwipeHelper/src/main/java/com/yeonkyu/HoldableSwipeHelper/HoldableSwipeHandler.kt

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class HoldableSwipeHandler private constructor(builder: Builder) :
1717
private val buttonAction: SwipeButtonAction = builder.buttonAction!!
1818
private var firstItemDismissFlag: Boolean = builder.firstItemDismissFlag
1919
private val excludeViewTypeSet: Set<Int> = builder.excludeViewTypeSet
20+
private var isLeftToRight: Boolean = builder.isLeftToRight
2021

2122
private var currentViewHolder: RecyclerView.ViewHolder? = null
2223
private var absoluteDx = 0f
@@ -37,6 +38,7 @@ class HoldableSwipeHandler private constructor(builder: Builder) :
3738

3839
internal var firstItemDismissFlag = true
3940
internal val excludeViewTypeSet = mutableSetOf<Int>()
41+
internal var isLeftToRight = true
4042

4143
fun setSwipeButtonAction(swipeButtonAction: SwipeButtonAction) = this.apply {
4244
this.buttonAction = swipeButtonAction
@@ -73,6 +75,10 @@ class HoldableSwipeHandler private constructor(builder: Builder) :
7375
firstItemDismissFlag = value
7476
}
7577

78+
fun setDirectionAsLeftToRight(value: Boolean) = this.apply {
79+
isLeftToRight = value
80+
}
81+
7682
fun build(): HoldableSwipeHandler {
7783
if (buttonAction == null) {
7884
throw IllegalArgumentException("SwipeButtonAction should be implemented. Did you forget to call addSwipeButtonAction()?")
@@ -116,7 +122,7 @@ class HoldableSwipeHandler private constructor(builder: Builder) :
116122

117123
viewHolder.itemView.translationX = scopedX
118124

119-
swipedBackgroundHolder.drawHoldingBackground(canvas, viewHolder, scopedX.toInt())
125+
swipedBackgroundHolder.drawHoldingBackground(canvas, viewHolder, scopedX.toInt(), isLeftToRight)
120126
currentViewHolder = viewHolder
121127
}
122128

@@ -125,12 +131,13 @@ class HoldableSwipeHandler private constructor(builder: Builder) :
125131
* setViewHolderTag()를 설정한다.
126132
*/
127133
override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float {
128-
if (absoluteDx <= -swipedBackgroundHolder.holderWidth) {
129-
setViewHolderTag(viewHolder, true)
130-
} else { // 정확히 currentDx가 rightWidth만큼 당겨져야하는지, 그 중간이 될지는 추가 논의필요
131-
setViewHolderTag(viewHolder, false)
134+
val shouldHold = if (isLeftToRight) {
135+
absoluteDx <= -swipedBackgroundHolder.holderWidth
136+
} else {
137+
absoluteDx >= swipedBackgroundHolder.holderWidth
132138
}
133139

140+
setViewHolderTag(viewHolder, shouldHold)
134141
return 2f
135142
}
136143

@@ -166,15 +173,26 @@ class HoldableSwipeHandler private constructor(builder: Builder) :
166173
dX: Float,
167174
isHolding: Boolean
168175
): Float {
169-
val min: Float = -swipedBackgroundHolder.holderWidth.toFloat()
170-
val max = 0f
171-
172-
val x = if (isHolding) {
173-
dX - swipedBackgroundHolder.holderWidth
176+
if (isLeftToRight) {
177+
val min: Float = -swipedBackgroundHolder.holderWidth.toFloat()
178+
val max = 0f
179+
val x = if (isHolding) {
180+
dX - swipedBackgroundHolder.holderWidth
181+
} else {
182+
dX
183+
}
184+
return Math.min(Math.max(min, x), max)
174185
} else {
175-
dX
186+
val min = 0f
187+
val max = swipedBackgroundHolder.holderWidth.toFloat()
188+
189+
val x = if (isHolding) {
190+
dX + swipedBackgroundHolder.holderWidth
191+
} else {
192+
dX
193+
}
194+
return x.coerceIn(min, max) // todo : 검증 필요
176195
}
177-
return Math.min(Math.max(min, x), max)
178196
}
179197

180198
@SuppressLint("ClickableViewAccessibility")
@@ -237,7 +255,7 @@ class HoldableSwipeHandler private constructor(builder: Builder) :
237255
currentViewHolder?.let {
238256
if (getViewHolderTag(it)) {
239257
swipedBackgroundHolder.run {
240-
drawHoldingBackground(c, it, scopedX.toInt())
258+
drawHoldingBackground(c, it, scopedX.toInt(), isLeftToRight)
241259
}
242260
}
243261
}

HoldableSwipeHelper/src/main/java/com/yeonkyu/HoldableSwipeHelper/SwipedBackgroundHolder.kt

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,45 @@ class SwipedBackgroundHolder(context: Context) {
4040
return firstIcon.bounds.contains(x, y)
4141
}
4242

43-
fun drawHoldingBackground(canvas: Canvas, viewHolder: RecyclerView.ViewHolder, x: Int) {
43+
fun drawHoldingBackground(canvas: Canvas, viewHolder: RecyclerView.ViewHolder, x: Int, isLeftToRight: Boolean) {
4444
val itemView = viewHolder.itemView
4545

4646
/** holding 되는 background 그린다 */
47-
drawBackground(canvas, itemView, x)
47+
drawBackground(canvas, itemView, x, isLeftToRight)
4848

4949
/** holding 되는 background 에서 버튼의 위치를 계산하고 그린다 */
50-
drawFirstItem(canvas, itemView)
50+
drawFirstItem(canvas, itemView, isLeftToRight)
5151
}
5252

53-
private fun drawBackground(canvas: Canvas, itemView: View, x: Int) {
53+
private fun drawBackground(canvas: Canvas, itemView: View, x: Int, isLeftToRight: Boolean) {
5454
background.color = backgroundColor
55-
background.setBounds(itemView.right + x , itemView.top, itemView.right, itemView.bottom)
55+
if (isLeftToRight) {
56+
background.setBounds(itemView.right + x , itemView.top, itemView.right, itemView.bottom)
57+
} else {
58+
background.setBounds(0, itemView.top, x, itemView.bottom)
59+
}
5660
background.draw(canvas)
5761
}
5862

59-
private fun drawFirstItem(canvas: Canvas, itemView: View) {
63+
private fun drawFirstItem(canvas: Canvas, itemView: View, isLeftToRight: Boolean) {
6064
val itemHeight = itemView.bottom - itemView.top
6165

6266
/** holding 되는 background 에서 버튼의 위치를 계산한다 */
6367
val firstIconTop = itemView.top + (itemHeight - intrinsicHeight) / 2
64-
val firstIconLeft = itemView.right - firstItemSideMargin - intrinsicWidth
65-
val firstIconRight = itemView.right - firstItemSideMargin
6668
val firstIconBottom = firstIconTop + intrinsicHeight
6769

70+
val firstIconLeft = if (isLeftToRight) {
71+
itemView.right - firstItemSideMargin - intrinsicWidth
72+
} else {
73+
firstItemSideMargin
74+
}
75+
76+
val firstIconRight = if (isLeftToRight) {
77+
itemView.right - firstItemSideMargin
78+
} else {
79+
firstIconLeft + intrinsicWidth
80+
}
81+
6882
/** holding 되는 background 에서 버튼을 그린다. */
6983
firstIcon.setBounds(firstIconLeft, firstIconTop, firstIconRight, firstIconBottom)
7084
firstIcon.draw(canvas)

HoldableSwipeHelper/src/main/java/com/yeonkyu/HoldableSwipeHelper/deprecated/HoldableSwipeHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ open class HoldableSwipeHelper(context: Context, private val buttonAction: Swipe
8989

9090
viewHolder.itemView.translationX = scopedX
9191

92-
swipedBackgroundHolder.drawHoldingBackground(canvas, viewHolder, scopedX.toInt())
92+
swipedBackgroundHolder.drawHoldingBackground(canvas, viewHolder, scopedX.toInt(), true)
9393
currentViewHolder = viewHolder
9494
}
9595

@@ -213,7 +213,7 @@ open class HoldableSwipeHelper(context: Context, private val buttonAction: Swipe
213213
currentViewHolder?.let {
214214
if (getViewHolderTag(it)) {
215215
swipedBackgroundHolder.run {
216-
drawHoldingBackground(c, it, scopedX.toInt())
216+
drawHoldingBackground(c, it, scopedX.toInt(), true)
217217
}
218218
}
219219
}

app/src/main/java/com/yeonkyu/holdableswipehandler/ui/list_adapter_recyclerview/ListAdapterRecyclerViewFragment.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class ListAdapterRecyclerViewFragment : Fragment() {
5858
.setDismissOnClickFirstItem(true)
5959
.excludeFromHoldableViewHolder(200)
6060
.setBackgroundColor("#ff0000")
61+
.setDirectionAsLeftToRight(false)
6162
.build()
6263

6364
binding.recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))

0 commit comments

Comments
 (0)