@@ -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 }
0 commit comments