Skip to content

Commit 0c1b649

Browse files
committed
+ api
1 parent f720381 commit 0c1b649

4 files changed

Lines changed: 167 additions & 12 deletions

File tree

Adapter/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ dependencies {
6060

6161
//https://mvnrepository.com/artifact/androidx.fragment
6262
//https://mvnrepository.com/artifact/androidx.fragment/fragment
63-
compileOnly "androidx.fragment:fragment-ktx:1.5.7"
63+
compileOnly "androidx.fragment:fragment-ktx:1.6.0"
6464
}

Adapter/src/main/java/com/angcyo/dsladapter/DslAdapter.kt

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.angcyo.dsladapter
22

3+
import android.annotation.SuppressLint
34
import android.view.LayoutInflater
45
import android.view.View
56
import android.view.ViewGroup
@@ -65,8 +66,11 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
6566
var onceFilterParams: FilterParams? = null
6667

6768
/**默认的[FilterParams]*/
69+
@SuppressLint("KotlinNullnessAnnotation")
6870
@NonNull
6971
var defaultFilterParams: FilterParams? = null
72+
@SuppressLint("KotlinNullnessAnnotation")
73+
@NonNull
7074
get() {
7175
return onceFilterParams ?: (field ?: _defaultFilterParams())
7276
}
@@ -124,6 +128,9 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
124128
return getValidFilterDataList().size
125129
}
126130

131+
/**当前的适配器数据是否为空*/
132+
fun isEmpty() = adapterItems.isEmpty()
133+
127134
override fun onBindViewHolder(
128135
holder: DslViewHolder,
129136
position: Int,
@@ -346,7 +353,11 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
346353
}
347354
dslAdapterStatusItem.itemDslAdapter = this
348355
dslAdapterStatusItem.itemState = status
349-
dslAdapterStatusItem.itemChanging = true
356+
if (dslAdapterStatusItem.itemChanging && dslAdapterStatusItem.itemChanged) {
357+
//可能正在更新中
358+
} else {
359+
dslAdapterStatusItem.itemChanging = true
360+
}
350361
}
351362

352363
/**自动设置状态
@@ -567,6 +578,81 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
567578
removeFooterItem(list)
568579
}
569580

581+
/**在指定的item[with], 后面插入新的item[newItem]
582+
* 调用 [updateItemDepend] 更新数据*/
583+
@UpdateFlag
584+
fun insertItemAfter(
585+
with: DslAdapterItem,
586+
newItem: DslAdapterItem,
587+
updateOther: Boolean = true
588+
): Boolean {
589+
var result = false
590+
result = result || insertItemAfter(dataItems, with, newItem, updateOther) != -1
591+
result = result || insertItemAfter(headerItems, with, newItem, updateOther) != -1
592+
result = result || insertItemAfter(footerItems, with, newItem, updateOther) != -1
593+
return result
594+
}
595+
596+
/**在指定的item[with], 前面插入新的item[newItem]
597+
*
598+
* 调用 [updateItemDepend] 更新数据*/
599+
@UpdateFlag
600+
fun insertItemBefore(
601+
with: DslAdapterItem,
602+
newItem: DslAdapterItem,
603+
updateOther: Boolean = true
604+
): Boolean {
605+
var result = false
606+
result = result || insertItemBefore(dataItems, with, newItem, updateOther) != -1
607+
result = result || insertItemBefore(headerItems, with, newItem, updateOther) != -1
608+
result = result || insertItemBefore(footerItems, with, newItem, updateOther) != -1
609+
return result
610+
}
611+
612+
/**[insertItemBefore]*/
613+
@UpdateFlag
614+
fun insertItemBefore(
615+
fromList: MutableList<DslAdapterItem>,
616+
with: DslAdapterItem,
617+
newItem: DslAdapterItem,
618+
updateOther: Boolean = true
619+
): Int {
620+
val index = fromList.addBeforeWith(with, newItem)
621+
if (index != -1) {
622+
//插入成功
623+
if (updateOther) {
624+
for (i in 0 until index) {
625+
//更新之后的item
626+
adapterItems.getOrNull(i)?.itemUpdateFlag = true
627+
}
628+
}
629+
_updateAdapterItems()
630+
}
631+
return index
632+
}
633+
634+
/**[insertItemAfter]*/
635+
@UpdateFlag
636+
fun insertItemAfter(
637+
fromList: MutableList<DslAdapterItem>,
638+
with: DslAdapterItem,
639+
newItem: DslAdapterItem,
640+
updateOther: Boolean = true
641+
): Int {
642+
val index = fromList.addAfterWith(with, newItem)
643+
if (index != -1) {
644+
//插入成功
645+
if (updateOther) {
646+
for (i in (index + 1) until fromList.size) {
647+
//更新之后的item
648+
adapterItems.getOrNull(i)?.itemUpdateFlag = true
649+
}
650+
}
651+
_updateAdapterItems()
652+
}
653+
return index
654+
}
655+
570656
/**移除数据*/
571657
@UpdateFlag
572658
fun removeItem(item: DslAdapterItem, updateOther: Boolean = true) {

Adapter/src/main/java/com/angcyo/dsladapter/DslViewHolder.kt

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.widget.ImageView
1313
import android.widget.TextView
1414
import androidx.annotation.IdRes
1515
import androidx.annotation.LayoutRes
16+
import androidx.annotation.MainThread
1617
import androidx.recyclerview.widget.RecyclerView
1718
import androidx.recyclerview.widget.RecyclerView.ViewHolder
1819
import 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
2831
open 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 {

Adapter/src/main/java/com/angcyo/dsladapter/LibEx.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,4 +904,27 @@ fun ViewGroup.removeAllDslItem(predicate: (Int, DslAdapterItem?) -> Boolean = {
904904
}
905905
}
906906

907-
//<editor-fold desc="DslAdapterItem操作">
907+
//<editor-fold desc="DslAdapterItem操作">
908+
909+
910+
/**在指定元素的后面插入新元素
911+
* @return -1:插入失败, index:对应的元素索引*/
912+
fun <T> MutableList<T>.addAfterWith(with: T, element: T): Int {
913+
val index = indexOf(with)
914+
if (index != -1) {
915+
add(index + 1, element)
916+
return index + 1
917+
}
918+
return -1
919+
}
920+
921+
/**在指定元素的前面插入新元素
922+
* @return -1:插入失败, index:对应的元素索引*/
923+
fun <T> MutableList<T>.addBeforeWith(with: T, element: T): Int {
924+
val index = indexOf(with)
925+
if (index != -1) {
926+
add(index, element)
927+
return index
928+
}
929+
return -1
930+
}

0 commit comments

Comments
 (0)