Skip to content

Commit

Permalink
+ api
Browse files Browse the repository at this point in the history
  • Loading branch information
angcyo committed Jul 4, 2023
1 parent f720381 commit 0c1b649
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Adapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ dependencies {

//https://mvnrepository.com/artifact/androidx.fragment
//https://mvnrepository.com/artifact/androidx.fragment/fragment
compileOnly "androidx.fragment:fragment-ktx:1.5.7"
compileOnly "androidx.fragment:fragment-ktx:1.6.0"
}
88 changes: 87 additions & 1 deletion Adapter/src/main/java/com/angcyo/dsladapter/DslAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.angcyo.dsladapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -65,8 +66,11 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
var onceFilterParams: FilterParams? = null

/**默认的[FilterParams]*/
@SuppressLint("KotlinNullnessAnnotation")
@NonNull
var defaultFilterParams: FilterParams? = null
@SuppressLint("KotlinNullnessAnnotation")
@NonNull
get() {
return onceFilterParams ?: (field ?: _defaultFilterParams())
}
Expand Down Expand Up @@ -124,6 +128,9 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
return getValidFilterDataList().size
}

/**当前的适配器数据是否为空*/
fun isEmpty() = adapterItems.isEmpty()

override fun onBindViewHolder(
holder: DslViewHolder,
position: Int,
Expand Down Expand Up @@ -346,7 +353,11 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
}
dslAdapterStatusItem.itemDslAdapter = this
dslAdapterStatusItem.itemState = status
dslAdapterStatusItem.itemChanging = true
if (dslAdapterStatusItem.itemChanging && dslAdapterStatusItem.itemChanged) {
//可能正在更新中
} else {
dslAdapterStatusItem.itemChanging = true
}
}

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

/**在指定的item[with], 后面插入新的item[newItem]
* 调用 [updateItemDepend] 更新数据*/
@UpdateFlag
fun insertItemAfter(
with: DslAdapterItem,
newItem: DslAdapterItem,
updateOther: Boolean = true
): Boolean {
var result = false
result = result || insertItemAfter(dataItems, with, newItem, updateOther) != -1
result = result || insertItemAfter(headerItems, with, newItem, updateOther) != -1
result = result || insertItemAfter(footerItems, with, newItem, updateOther) != -1
return result
}

/**在指定的item[with], 前面插入新的item[newItem]
*
* 调用 [updateItemDepend] 更新数据*/
@UpdateFlag
fun insertItemBefore(
with: DslAdapterItem,
newItem: DslAdapterItem,
updateOther: Boolean = true
): Boolean {
var result = false
result = result || insertItemBefore(dataItems, with, newItem, updateOther) != -1
result = result || insertItemBefore(headerItems, with, newItem, updateOther) != -1
result = result || insertItemBefore(footerItems, with, newItem, updateOther) != -1
return result
}

/**[insertItemBefore]*/
@UpdateFlag
fun insertItemBefore(
fromList: MutableList<DslAdapterItem>,
with: DslAdapterItem,
newItem: DslAdapterItem,
updateOther: Boolean = true
): Int {
val index = fromList.addBeforeWith(with, newItem)
if (index != -1) {
//插入成功
if (updateOther) {
for (i in 0 until index) {
//更新之后的item
adapterItems.getOrNull(i)?.itemUpdateFlag = true
}
}
_updateAdapterItems()
}
return index
}

/**[insertItemAfter]*/
@UpdateFlag
fun insertItemAfter(
fromList: MutableList<DslAdapterItem>,
with: DslAdapterItem,
newItem: DslAdapterItem,
updateOther: Boolean = true
): Int {
val index = fromList.addAfterWith(with, newItem)
if (index != -1) {
//插入成功
if (updateOther) {
for (i in (index + 1) until fromList.size) {
//更新之后的item
adapterItems.getOrNull(i)?.itemUpdateFlag = true
}
}
_updateAdapterItems()
}
return index
}

/**移除数据*/
@UpdateFlag
fun removeItem(item: DslAdapterItem, updateOther: Boolean = true) {
Expand Down
64 changes: 55 additions & 9 deletions Adapter/src/main/java/com/angcyo/dsladapter/DslViewHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.annotation.MainThread
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.angcyo.dsladapter.internal.ThrottleClickListener
Expand All @@ -25,6 +26,8 @@ import java.lang.ref.WeakReference
* @date 2019/08/09
* Copyright (c) 2019 ShenZhen O&M Cloud Co., Ltd. All rights reserved.
*/

@MainThread
open class DslViewHolder(
itemView: View,
initialCapacity: Int = DEFAULT_INITIAL_CAPACITY
Expand Down Expand Up @@ -252,19 +255,22 @@ open class DslViewHolder(
}

/**长按事件识别
* [loopLongPress] 是否需要循环发送长按事件, 否则只发送一次
* [EVENT_TYPE_CLICK]
* [EVENT_TYPE_LONG_PRESS]
* */
fun longTouch(
@IdRes id: Int,
loopLongPress: Boolean = false,
block: (view: View, event: MotionEvent, eventType: Int?) -> Boolean
) {
longTouch(v<View>(id), block)
longTouch(v<View>(id), loopLongPress, block)
}

@SuppressLint("ClickableViewAccessibility")
fun longTouch(
view: View?,
loopLongPress: Boolean = false,
block: (view: View, event: MotionEvent, eventType: Int?) -> Boolean
) {

Expand All @@ -281,10 +287,12 @@ open class DslViewHolder(
block(view, event, eventType)
event.recycle()

view.postDelayed(
longRunnable,
ViewConfiguration.getLongPressTimeout().toLong()
)
if (loopLongPress) {
view.postDelayed(
longRunnable,
ViewConfiguration.getLongPressTimeout().toLong()
)
}
}
}
}
Expand All @@ -305,9 +313,20 @@ open class DslViewHolder(
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
view.isPressed = false
if (eventType == null) {
block(view, event, EVENT_TYPE_CLICK)
eventType = EVENT_TYPE_CLICK
}
view.removeCallbacks(longRunnable)
}
}
if (eventType == EVENT_TYPE_CLICK) {
//发送点击事件
block(view, event, EVENT_TYPE_CLICK)
} else {
//其它事件转发, 用于自定义处理
block(view, event, null)
}
when (event.actionMasked) {
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
eventType = null
}
}
Expand Down Expand Up @@ -366,7 +385,7 @@ open class DslViewHolder(
}
}

fun postDelay(delayMillis: Long, runnable: () -> Unit) {
fun postDelay(delayMillis: Long = 0, runnable: () -> Unit) {
postDelay(object : Runnable {
override fun run() {
runnable.invoke()
Expand Down Expand Up @@ -404,7 +423,7 @@ open class DslViewHolder(
}

/**获取焦点*/
fun focused(view: View?) {
fun focused(view: View? = itemView) {
view?.isFocusable = true
view?.isFocusableInTouchMode = true
view?.requestFocus()
Expand Down Expand Up @@ -544,12 +563,39 @@ open class DslViewHolder(
return gone(v<View>(resId))
}

fun gone(@IdRes resId: Int, gone: Boolean) {
fun goneIndex(index: Int, gone: Boolean = true): DslViewHolder {
val view = itemView
if (view is ViewGroup) {
val child = view.getChildAt(index)
if (child != null) {
gone(child, gone)
}
}
return this
}

fun gone(view: View, gone: Boolean): DslViewHolder {
if (gone) {
gone(view)
} else {
visible(view)
}
return this
}

fun gone(@IdRes resId: Int, gone: Boolean): DslViewHolder {
if (gone) {
gone(v<View>(resId))
} else {
visible(resId)
}
return this
}

fun gone(@IdRes vararg resId: Int) {
for (id in resId) {
gone(id)
}
}

fun gone(view: View?): DslViewHolder {
Expand Down
25 changes: 24 additions & 1 deletion Adapter/src/main/java/com/angcyo/dsladapter/LibEx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -904,4 +904,27 @@ fun ViewGroup.removeAllDslItem(predicate: (Int, DslAdapterItem?) -> Boolean = {
}
}

//<editor-fold desc="DslAdapterItem操作">
//<editor-fold desc="DslAdapterItem操作">


/**在指定元素的后面插入新元素
* @return -1:插入失败, index:对应的元素索引*/
fun <T> MutableList<T>.addAfterWith(with: T, element: T): Int {
val index = indexOf(with)
if (index != -1) {
add(index + 1, element)
return index + 1
}
return -1
}

/**在指定元素的前面插入新元素
* @return -1:插入失败, index:对应的元素索引*/
fun <T> MutableList<T>.addBeforeWith(with: T, element: T): Int {
val index = indexOf(with)
if (index != -1) {
add(index, element)
return index
}
return -1
}

0 comments on commit 0c1b649

Please sign in to comment.