@@ -13,6 +13,7 @@ import android.widget.ImageView
1313import android.widget.TextView
1414import androidx.annotation.IdRes
1515import androidx.annotation.LayoutRes
16+ import androidx.annotation.MainThread
1617import androidx.recyclerview.widget.RecyclerView
1718import androidx.recyclerview.widget.RecyclerView.ViewHolder
1819import com.angcyo.dsladapter.internal.ThrottleClickListener
@@ -25,6 +26,8 @@ import java.lang.ref.WeakReference
2526 * @date 2019/08/09
2627 * Copyright (c) 2019 ShenZhen O&M Cloud Co., Ltd. All rights reserved.
2728 */
29+
30+ @MainThread
2831open class DslViewHolder (
2932 itemView : View ,
3033 initialCapacity : Int = DEFAULT_INITIAL_CAPACITY
@@ -252,19 +255,22 @@ open class DslViewHolder(
252255 }
253256
254257 /* *长按事件识别
258+ * [loopLongPress] 是否需要循环发送长按事件, 否则只发送一次
255259 * [EVENT_TYPE_CLICK]
256260 * [EVENT_TYPE_LONG_PRESS]
257261 * */
258262 fun longTouch (
259263 @IdRes id : Int ,
264+ loopLongPress : Boolean = false,
260265 block : (view: View , event: MotionEvent , eventType: Int? ) -> Boolean
261266 ) {
262- longTouch(v<View >(id), block)
267+ longTouch(v<View >(id), loopLongPress, block)
263268 }
264269
265270 @SuppressLint(" ClickableViewAccessibility" )
266271 fun longTouch (
267272 view : View ? ,
273+ loopLongPress : Boolean = false,
268274 block : (view: View , event: MotionEvent , eventType: Int? ) -> Boolean
269275 ) {
270276
@@ -281,10 +287,12 @@ open class DslViewHolder(
281287 block(view, event, eventType)
282288 event.recycle()
283289
284- view.postDelayed(
285- longRunnable,
286- ViewConfiguration .getLongPressTimeout().toLong()
287- )
290+ if (loopLongPress) {
291+ view.postDelayed(
292+ longRunnable,
293+ ViewConfiguration .getLongPressTimeout().toLong()
294+ )
295+ }
288296 }
289297 }
290298 }
@@ -305,9 +313,20 @@ open class DslViewHolder(
305313 MotionEvent .ACTION_UP , MotionEvent .ACTION_CANCEL -> {
306314 view.isPressed = false
307315 if (eventType == null ) {
308- block(view, event, EVENT_TYPE_CLICK )
316+ eventType = EVENT_TYPE_CLICK
309317 }
310318 view.removeCallbacks(longRunnable)
319+ }
320+ }
321+ if (eventType == EVENT_TYPE_CLICK ) {
322+ // 发送点击事件
323+ block(view, event, EVENT_TYPE_CLICK )
324+ } else {
325+ // 其它事件转发, 用于自定义处理
326+ block(view, event, null )
327+ }
328+ when (event.actionMasked) {
329+ MotionEvent .ACTION_UP , MotionEvent .ACTION_CANCEL -> {
311330 eventType = null
312331 }
313332 }
@@ -366,7 +385,7 @@ open class DslViewHolder(
366385 }
367386 }
368387
369- fun postDelay (delayMillis : Long , runnable : () -> Unit ) {
388+ fun postDelay (delayMillis : Long = 0 , runnable : () -> Unit ) {
370389 postDelay(object : Runnable {
371390 override fun run () {
372391 runnable.invoke()
@@ -404,7 +423,7 @@ open class DslViewHolder(
404423 }
405424
406425 /* *获取焦点*/
407- fun focused (view : View ? ) {
426+ fun focused (view : View ? = itemView ) {
408427 view?.isFocusable = true
409428 view?.isFocusableInTouchMode = true
410429 view?.requestFocus()
@@ -544,12 +563,39 @@ open class DslViewHolder(
544563 return gone(v<View >(resId))
545564 }
546565
547- fun gone (@IdRes resId : Int , gone : Boolean ) {
566+ fun goneIndex (index : Int , gone : Boolean = true): DslViewHolder {
567+ val view = itemView
568+ if (view is ViewGroup ) {
569+ val child = view.getChildAt(index)
570+ if (child != null ) {
571+ gone(child, gone)
572+ }
573+ }
574+ return this
575+ }
576+
577+ fun gone (view : View , gone : Boolean ): DslViewHolder {
578+ if (gone) {
579+ gone(view)
580+ } else {
581+ visible(view)
582+ }
583+ return this
584+ }
585+
586+ fun gone (@IdRes resId : Int , gone : Boolean ): DslViewHolder {
548587 if (gone) {
549588 gone(v<View >(resId))
550589 } else {
551590 visible(resId)
552591 }
592+ return this
593+ }
594+
595+ fun gone (@IdRes vararg resId : Int ) {
596+ for (id in resId) {
597+ gone(id)
598+ }
553599 }
554600
555601 fun gone (view : View ? ): DslViewHolder {
0 commit comments