diff --git a/app/src/main/java/me/yifeiyuan/flapdev/GitHubDemoFragment.kt b/app/src/main/java/me/yifeiyuan/flapdev/GitHubDemoFragment.kt index 2f827550..6d1d3b38 100644 --- a/app/src/main/java/me/yifeiyuan/flapdev/GitHubDemoFragment.kt +++ b/app/src/main/java/me/yifeiyuan/flapdev/GitHubDemoFragment.kt @@ -2,7 +2,7 @@ package me.yifeiyuan.flapdev import android.view.View import me.yifeiyuan.flap.decoration.LinearSpaceItemDecoration -import me.yifeiyuan.flap.delegate.adapterDelegate +import me.yifeiyuan.flap.dsl.adapterDelegate import me.yifeiyuan.flap.ext.bindTextView import me.yifeiyuan.flapdev.components.SimpleTextModel import me.yifeiyuan.flapdev.testcases.BaseTestcaseFragment diff --git a/app/src/main/java/me/yifeiyuan/flapdev/testcases/AdapterDelegateDSLTestcase.kt b/app/src/main/java/me/yifeiyuan/flapdev/testcases/AdapterDelegateDSLTestcase.kt index 9b631347..2f97d720 100644 --- a/app/src/main/java/me/yifeiyuan/flapdev/testcases/AdapterDelegateDSLTestcase.kt +++ b/app/src/main/java/me/yifeiyuan/flapdev/testcases/AdapterDelegateDSLTestcase.kt @@ -4,8 +4,7 @@ import android.util.Log import android.view.View import android.widget.ImageView import me.yifeiyuan.flap.FlapAdapter -import me.yifeiyuan.flap.delegate.LayoutAdapterDelegate -import me.yifeiyuan.flap.delegate.adapterDelegate +import me.yifeiyuan.flap.dsl.adapterDelegate import me.yifeiyuan.flap.ext.bindButton import me.yifeiyuan.flap.ext.bindTextView import me.yifeiyuan.flap.ext.bindView @@ -127,13 +126,27 @@ class AdapterDelegateDSLTestcase : BaseTestcaseFragment() { } } - val simpleImageDelegate = LayoutAdapterDelegate(SimpleImageModel::class.java, - R.layout.flap_item_simple_image) { model -> - bindView(R.id.logo) { - setOnClickListener { - toast("simpleImageDelegate clicked") +// val simpleImageDelegate = LayoutAdapterDelegate(SimpleImageModel::class.java, +// R.layout.flap_item_simple_image) { model -> +// bindView(R.id.logo) { +// setOnClickListener { +// toast("simpleImageDelegate clicked") +// } +// } +// } + + val simpleImageDelegate = adapterDelegate(R.layout.flap_item_simple_image) { + + onBind { model, position, payloads, adapter -> + bindView(R.id.logo) { + setOnClickListener { + toast("simpleImageDelegate clicked") + } } } + onResume { + Log.d(TAG, "simpleImageDelegate onResume() called $this") + } } adapter.registerAdapterDelegates(simpleTextDelegate, simpleImageDelegate, testAllDelegate) diff --git a/flap-compiler/src/main/java/me/yifeiyuan/flap/compiler/FlapProcessor.java b/flap-compiler/src/main/java/me/yifeiyuan/flap/compiler/FlapProcessor.java index 64e86cc0..daac19a9 100644 --- a/flap-compiler/src/main/java/me/yifeiyuan/flap/compiler/FlapProcessor.java +++ b/flap-compiler/src/main/java/me/yifeiyuan/flap/compiler/FlapProcessor.java @@ -186,6 +186,7 @@ private TypeSpec createAdapterDelegateTypeSpec(final RoundEnvironment roundEnvir .addModifiers(Modifier.PUBLIC) // .addParameter(itemModelClass, "model") .addParameter(CLASS_OBJECT, "model") + .addParameter(Integer.TYPE, "position") .addStatement("return 0") .returns(Long.TYPE); diff --git a/flap/src/main/java/me/yifeiyuan/flap/ComponentConfig.kt b/flap/src/main/java/me/yifeiyuan/flap/ComponentConfig.kt index c690bac2..887799f8 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/ComponentConfig.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/ComponentConfig.kt @@ -5,20 +5,42 @@ import me.yifeiyuan.flap.ext.SwipeDragHelper /** * 组件的配置项抽象,可以控制部分功能的开关 * + * @see SwipeDragHelper + * @see me.yifeiyuan.flap.ext.ItemClicksHelper + * * Created by 程序亦非猿 on 2022/8/31. * + * @since 3.0.8 */ interface ComponentConfig { + /** + * 支持滑动的方向 + */ fun getSwipeFlags(): Int = SwipeDragHelper.FLAG_UN_SET + /** + * 支持拖动的方向 + */ fun getDragFlags(): Int = SwipeDragHelper.FLAG_UN_SET - fun isSwipeEnable(): Boolean = true + /** + * 是否可以滑动删除 + */ + fun isSwipeEnabled(): Boolean = true - fun isDragEnable(): Boolean = true + /** + * 是否可以拖动 + */ + fun isDragEnabled(): Boolean = true + /** + * 是否可以点击 + */ fun isClickable(): Boolean = true + /** + * 是否可以长按点击 + */ fun isLongClickable(): Boolean = true } \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt index 92ba84a6..547e1957 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt @@ -307,7 +307,7 @@ open class FlapAdapter : RecyclerView.Adapter>(), IRegistry, IAdapt override fun getItemId(position: Int): Long { val itemData = getItemData(position) val delegate = getDelegateByViewType(getItemViewType(position)) - return delegate.getItemId(itemData) + return delegate.getItemId(itemData, position) } /** diff --git a/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegate.kt b/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegate.kt index 792438a9..39a7795b 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegate.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegate.kt @@ -66,7 +66,7 @@ interface AdapterDelegate> { /** * @see RecyclerView.Adapter.getItemId */ - fun getItemId(model: Any): Long = RecyclerView.NO_ID + fun getItemId(model: Any, position: Int): Long = RecyclerView.NO_ID /** * @see RecyclerView.Adapter.getItemViewType diff --git a/flap/src/main/java/me/yifeiyuan/flap/delegate/FallbackAdapterDelegate.kt b/flap/src/main/java/me/yifeiyuan/flap/delegate/FallbackAdapterDelegate.kt index 31925a05..1f27da8c 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/delegate/FallbackAdapterDelegate.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/delegate/FallbackAdapterDelegate.kt @@ -49,11 +49,19 @@ internal class FallbackComponent(v: View) : Component(v) { } } - override fun isDragEnable(): Boolean { + override fun isClickable(): Boolean { return false } - override fun isSwipeEnable(): Boolean { + override fun isLongClickable(): Boolean { + return false + } + + override fun isDragEnabled(): Boolean { + return false + } + + override fun isSwipeEnabled(): Boolean { return false } } diff --git a/flap/src/main/java/me/yifeiyuan/flap/delegate/LayoutAdapterDelegate.kt b/flap/src/main/java/me/yifeiyuan/flap/delegate/LayoutAdapterDelegate.kt deleted file mode 100644 index bdb3b138..00000000 --- a/flap/src/main/java/me/yifeiyuan/flap/delegate/LayoutAdapterDelegate.kt +++ /dev/null @@ -1,199 +0,0 @@ -@file:Suppress("UNCHECKED_CAST") - -package me.yifeiyuan.flap.delegate - -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.lifecycle.LifecycleOwner -import androidx.recyclerview.widget.RecyclerView -import me.yifeiyuan.flap.Component -import me.yifeiyuan.flap.FlapAdapter -import me.yifeiyuan.flap.R - -/** - * - * LayoutAdapterDelegate 是为了降低创建 Component 和 自定义 AdapterDelegate 带来的使用成本而创建的。 - * - * 使用 LayoutAdapterDelegate 的好处: - * 1. 不需要写 Component - * 2. 不需要写 AdapterDelegate - * 3. 有 DSL 支持,更方便 - * - * 使用 LayoutAdapterDelegate 必须要保证的情况: - * 1. Model 与 LayoutAdapterDelegate 是一对一的关系 - * 2. layoutId 可以当做 itemViewType 直接用 - * - * Created by 程序亦非猿 on 2021/10/27. - * @since 3.0.0 - */ -class LayoutAdapterDelegate> : AdapterDelegate { - - private var config: LayoutAdapterDelegateConfig = LayoutAdapterDelegateConfig() - - /** - * 最简单的构造,只关心简单的 onBind - */ - constructor(modelClass: Class, layoutId: Int, binder: Component.(model: T) -> Unit) { - config.modelClass = modelClass - config.layoutId = layoutId - config.onBind = binder - } - - internal constructor(delegateConfig: LayoutAdapterDelegateConfig) { - config = delegateConfig - } - - override fun delegate(model: Any): Boolean { - return model.javaClass == config.modelClass - } - - override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): C { - val view = inflater.inflate(viewType, parent, false) - return LayoutComponent(view) as C - } - - override fun onBindViewHolder(component: Component<*>, data: Any, position: Int, payloads: List, adapter: FlapAdapter) { - - if (config.onBind2 != null) { - config.onBind2?.invoke(component as Component, data as T, position, payloads, adapter) - } else if (config.onBind != null) { - config.onBind?.invoke(component as Component, data as T) - } - - if (config.onClickListener != null) { - component.itemView.setOnClickListener { - config.onClickListener?.invoke(component as Component, data as T, position, adapter) - } - } else { - component.itemView.setOnClickListener(null) - } - - if (config.onLongClickListener != null) { - component.itemView.setOnLongClickListener { - config.onLongClickListener!!.invoke(component as Component, data as T, position, adapter) - } - } else { - component.itemView.setOnLongClickListener(null) - } - - component.itemView.setTag(R.id.layout_adapter_delegate, this) - } - - override fun getItemViewType(model: Any): Int { - return config.layoutId - } - - override fun getItemId(model: Any): Long { - return config.itemId - } - - override fun onViewAttachedToWindow(adapter: FlapAdapter, component: Component<*>) { - super.onViewAttachedToWindow(adapter, component) - config.onViewAttachedToWindow?.invoke(component as Component) - } - - override fun onViewDetachedFromWindow(adapter: FlapAdapter, component: Component<*>) { - super.onViewDetachedFromWindow(adapter, component) - config.onViewDetachedFromWindow?.invoke(component as Component) - } - - override fun onFailedToRecycleView(adapter: FlapAdapter, component: Component<*>): Boolean { - return config.onFailedToRecycleView?.invoke(component as Component, adapter) - ?: super.onFailedToRecycleView(adapter, component) - } - - override fun onViewRecycled(adapter: FlapAdapter, component: Component<*>) { - super.onViewRecycled(adapter, component) - config.onViewRecycled?.invoke(component as Component, adapter) - } - - internal fun componentOnResume(component: Component<*>, owner: LifecycleOwner) { - config.onResume?.invoke(component as Component) - } - - internal fun componentOnPause(component: Component<*>, owner: LifecycleOwner) { - config.onPause?.invoke(component as Component) - } - - internal fun componentOnStop(component: Component<*>, owner: LifecycleOwner) { - config.onStop?.invoke(component as Component) - } - - internal fun componentOnDestroy(component: Component<*>, owner: LifecycleOwner) { - config.onDestroy?.invoke(component as Component) - } -} - -class LayoutComponent(view: View) : Component(view) { - - override fun onBind(model: T) { - } - - override fun onResume(owner: LifecycleOwner) { - super.onResume(owner) - getLayoutAdapterDelegateByTag()?.componentOnResume(this, owner) - } - - override fun onPause(owner: LifecycleOwner) { - super.onPause(owner) - getLayoutAdapterDelegateByTag()?.componentOnPause(this, owner) - } - - override fun onStop(owner: LifecycleOwner) { - super.onStop(owner) - getLayoutAdapterDelegateByTag()?.componentOnStop(this, owner) - } - - override fun onDestroy(owner: LifecycleOwner) { - super.onDestroy(owner) - getLayoutAdapterDelegateByTag()?.componentOnDestroy(this, owner) - } - - private fun getLayoutAdapterDelegateByTag(): LayoutAdapterDelegate<*, *>? { - return itemView.getTag(R.id.layout_adapter_delegate) as? LayoutAdapterDelegate<*, *> - } -} - -class LayoutAdapterDelegateConfig { - - var modelClass: Class? = null - - /** - * 资源文件 layout id - */ - var layoutId: Int = 0 - - var itemId: Long = RecyclerView.NO_ID - - /** - * 更多参数的 onBind - */ - var onBind2: (Component.(model: T, position: Int, payloads: List, adapter: FlapAdapter) -> Unit)? = null - - /** - * 简单参数的 onBind - */ - var onBind: (Component.(model: T) -> Unit)? = null - - var onViewAttachedToWindow: (Component.() -> Unit)? = null - var onViewDetachedFromWindow: (Component.() -> Unit)? = null - - var onResume: (Component.() -> Unit)? = null - var onPause: (Component.() -> Unit)? = null - var onStop: (Component.() -> Unit)? = null - var onDestroy: (Component.() -> Unit)? = null - - var onViewRecycled: (Component.(adapter: FlapAdapter) -> Unit)? = null - var onFailedToRecycleView: (Component.(adapter: FlapAdapter) -> Boolean)? = null - - /** - * 单击事件 - */ - var onClickListener: (Component.(model: T, position: Int, adapter: FlapAdapter) -> Unit)? = null - - /** - * 长按事件 - */ - var onLongClickListener: (Component.(model: T, position: Int, adapter: FlapAdapter) -> Boolean)? = null -} \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/delegate/LayoutAdapterDelegateDSL.kt b/flap/src/main/java/me/yifeiyuan/flap/delegate/LayoutAdapterDelegateDSL.kt deleted file mode 100644 index 102fb4c5..00000000 --- a/flap/src/main/java/me/yifeiyuan/flap/delegate/LayoutAdapterDelegateDSL.kt +++ /dev/null @@ -1,134 +0,0 @@ -@file:Suppress("unused") - -package me.yifeiyuan.flap.delegate - -import androidx.recyclerview.widget.RecyclerView -import me.yifeiyuan.flap.Component -import me.yifeiyuan.flap.FlapAdapter - -/** - * - * LayoutAdapterDelegate DSL 支持 - * - * Created by 程序亦非猿 on 2022/8/4. - * @since 3.0.0 - */ - -/** - * - * @param modelClass 要代理的模型类 - * @param layoutId 布局id - * @since 3.0.0 - */ -class LayoutAdapterDelegateBuilder(private var modelClass: Class, var layoutId: Int = 0) { - - /** - * 简单参数的 onBind - */ - private var onBind: (Component.(model: T) -> Unit)? = null - - /** - * 更多参数的 onBind - */ - private var onBind2: (Component.(model: T, position: Int, payloads: List, adapter: FlapAdapter) -> Unit)? = null - - private var onViewAttachedToWindow: (Component.() -> Unit)? = null - - private var onViewDetachedFromWindow: (Component.() -> Unit)? = null - - private var onViewRecycled: (Component.(adapter: FlapAdapter) -> Unit)? = null - private var onFailedToRecycleView: (Component.(adapter: FlapAdapter) -> Boolean)? = null - - private var onResume: (Component.() -> Unit)? = null - private var onPause: (Component.() -> Unit)? = null - private var onStop: (Component.() -> Unit)? = null - private var onDestroy: (Component.() -> Unit)? = null - - /** - * 单击事件 - */ - private var onClickListener: (Component.(model: T, position: Int, adapter: FlapAdapter) -> Unit)? = null - - /** - * 长按事件 - */ - private var onLongClickListener: ((component: Component, model: T, position: Int, adapter: FlapAdapter) -> Boolean)? = null - - private var itemId: Long = RecyclerView.NO_ID - - fun itemId(itemId: Long) { - this.itemId = itemId - } - - fun onBind(onBind: (Component.(model: T) -> Unit)) { - this.onBind = onBind - } - - fun onBind(onBind: (Component.(model: T, position: Int, payloads: List, adapter: FlapAdapter) -> Unit)) { - onBind2 = onBind - } - - fun onClick(onclick: (Component.(model: T, position: Int, adapter: FlapAdapter) -> Unit)) { - onClickListener = onclick - } - - fun onLongClick(onLongClick: (Component.(model: T, position: Int, adapter: FlapAdapter) -> Boolean)) { - onLongClickListener = onLongClick - } - - fun onViewAttachedToWindow(onAttach: (Component.() -> Unit)) { - onViewAttachedToWindow = onAttach - } - - fun onViewDetachedFromWindow(onDetach: (Component.() -> Unit)) { - onViewDetachedFromWindow = onDetach - } - - fun onViewRecycled(block: (Component.(adapter: FlapAdapter) -> Unit)) { - onViewRecycled = block - } - - fun onFailedToRecycleView(block: (Component.(adapter: FlapAdapter) -> Boolean)) { - onFailedToRecycleView = block - } - - fun onResume(block: (Component.() -> Unit)) { - onResume = block - } - - fun onPause(block: (Component.() -> Unit)) { - onPause = block - } - - fun onStop(block: (Component.() -> Unit)) { - onStop = block - } - - fun onDestroy(block: (Component.() -> Unit)) { - onDestroy = block - } - - fun build(): LayoutAdapterDelegate> { - val config = LayoutAdapterDelegateConfig() - config.modelClass = modelClass - config.onClickListener = onClickListener - config.onLongClickListener = onLongClickListener - config.onBind = onBind - config.onBind2 = onBind2 - config.layoutId = layoutId - config.itemId = itemId - config.onViewAttachedToWindow = onViewAttachedToWindow - config.onViewDetachedFromWindow = onViewDetachedFromWindow - config.onViewRecycled = onViewRecycled - config.onFailedToRecycleView = onFailedToRecycleView - config.onResume = onResume - config.onPause = onPause - config.onStop = onStop - config.onDestroy = onDestroy - return LayoutAdapterDelegate(config) - } -} - -inline fun adapterDelegate(layoutId: Int, builder: LayoutAdapterDelegateBuilder.() -> Unit): LayoutAdapterDelegate> { - return LayoutAdapterDelegateBuilder(T::class.java, layoutId).apply(builder).build() -} \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt b/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt new file mode 100644 index 00000000..856635a6 --- /dev/null +++ b/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt @@ -0,0 +1,212 @@ +package me.yifeiyuan.flap.dsl + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.annotation.LayoutRes +import androidx.lifecycle.LifecycleOwner +import androidx.recyclerview.widget.RecyclerView +import me.yifeiyuan.flap.Component +import me.yifeiyuan.flap.FlapAdapter +import me.yifeiyuan.flap.delegate.AdapterDelegate + + +/** + * 支持 AdapterDelegate DSL 功能 + * + * @since 3.0.9 + */ +inline fun adapterDelegate(@LayoutRes layoutId: Int, noinline isDelegateFor: ((model: Any) -> Boolean) = { m -> m.javaClass == T::class.java }, noinline componentInitializer: DslComponent.() -> Unit): DslAdapterDelegate { + return DslAdapterDelegate(T::class.java, layoutId, isDelegateFor = isDelegateFor, block = componentInitializer) +} + +/** + * Created by 程序亦非猿 on 2022/9/1. + * + * @since 3.0.9 + */ +class DslAdapterDelegate( + private var modelClass: Class?, + @LayoutRes private var layoutId: Int, + private var itemId: Long = RecyclerView.NO_ID, + private var isDelegateFor: ((model: Any) -> Boolean) = { m -> m.javaClass == modelClass }, + private var block: DslComponent.() -> Unit, +) : AdapterDelegate> { + + override fun delegate(model: Any): Boolean { + return isDelegateFor.invoke(model) + } + + override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): DslComponent { + val view = inflater.inflate(viewType, parent, false) + val component = DslComponent(view) + block.invoke(component) + return component + } + + override fun onBindViewHolder(component: Component<*>, data: Any, position: Int, payloads: List, adapter: FlapAdapter) { + super.onBindViewHolder(component, data, position, payloads, adapter) + } + + override fun getItemViewType(model: Any): Int { + return layoutId + } + + override fun getItemId(model: Any, position: Int): Long { + return itemId + } +} + +/** + * Created by 程序亦非猿 on 2022/9/1. + * + * @since 3.0.9 + */ +class DslComponent(view: View) : Component(view) { + + /** + * 更多参数的 onBind + */ + private var onBind2: ((model: T, position: Int, payloads: List, adapter: FlapAdapter) -> Unit)? = null + + /** + * 简单参数的 onBind + */ + private var onBind: ((model: T) -> Unit)? = null + + private var onViewAttachedToWindow: (() -> Unit)? = null + private var onViewDetachedFromWindow: (() -> Unit)? = null + + private var onResume: (() -> Unit)? = null + private var onPause: (() -> Unit)? = null + private var onStop: (() -> Unit)? = null + private var onDestroy: (() -> Unit)? = null + + private var onViewRecycled: (() -> Unit)? = null + private var onFailedToRecycleView: (() -> Boolean)? = null + + /** + * 单击事件 + */ + private var onClickListener: ((model: T, position: Int, adapter: FlapAdapter) -> Unit)? = null + + /** + * 长按事件 + */ + private var onLongClickListener: ((model: T, position: Int, adapter: FlapAdapter) -> Boolean)? = null + + override fun onBind(model: T, position: Int, payloads: List, adapter: FlapAdapter, delegate: AdapterDelegate<*, *>) { + + if (onBind2 != null) { + onBind2?.invoke(model, position, payloads, adapter) + } else if (onBind != null) { + onBind?.invoke(model) + } + + if (onClickListener != null && isClickable()) { + itemView.setOnClickListener { + onClickListener?.invoke(model, position, adapter) + } + } else { + itemView.setOnClickListener(null) + } + + if (onLongClickListener != null && isLongClickable()) { + itemView.setOnLongClickListener { + onLongClickListener!!.invoke(model, position, adapter) + } + } else { + itemView.setOnLongClickListener(null) + } + } + + override fun onBind(model: T) { + } + + override fun onViewAttachedToWindow(flapAdapter: FlapAdapter) { + super.onViewAttachedToWindow(flapAdapter) + onViewAttachedToWindow?.invoke() + } + + override fun onViewDetachedFromWindow(flapAdapter: FlapAdapter) { + super.onViewDetachedFromWindow(flapAdapter) + onViewDetachedFromWindow?.invoke() + } + + override fun onViewRecycled(flapAdapter: FlapAdapter) { + super.onViewRecycled(flapAdapter) + onViewRecycled?.invoke() + } + + override fun onPause(owner: LifecycleOwner) { + super.onPause(owner) + onPause?.invoke() + } + + override fun onResume(owner: LifecycleOwner) { + super.onResume(owner) + onResume?.invoke() + } + + override fun onStop(owner: LifecycleOwner) { + super.onStop(owner) + onStop?.invoke() + } + + override fun onDestroy(owner: LifecycleOwner) { + super.onDestroy(owner) + onDestroy?.invoke() + } + + override fun onFailedToRecycleView(flapAdapter: FlapAdapter): Boolean { + return onFailedToRecycleView?.invoke() ?: super.onFailedToRecycleView(flapAdapter) + } + + fun onBind(onBind: ((model: T) -> Unit)) { + this.onBind = onBind + } + + fun onBind(onBind: ((model: T, position: Int, payloads: List, adapter: FlapAdapter) -> Unit)) { + onBind2 = onBind + } + + fun onClick(onclick: ((model: T, position: Int, adapter: FlapAdapter) -> Unit)) { + onClickListener = onclick + } + + fun onLongClick(onLongClick: ((model: T, position: Int, adapter: FlapAdapter) -> Boolean)) { + onLongClickListener = onLongClick + } + + fun onViewAttachedToWindow(onAttach: (() -> Unit)) { + onViewAttachedToWindow = onAttach + } + + fun onViewDetachedFromWindow(onDetach: (() -> Unit)) { + onViewDetachedFromWindow = onDetach + } + + fun onViewRecycled(block: (() -> Unit)) { + onViewRecycled = block + } + + fun onFailedToRecycleView(block: (() -> Boolean)) { + onFailedToRecycleView = block + } + + fun onResume(block: (() -> Unit)) { + onResume = block + } + + fun onPause(block: (() -> Unit)) { + onPause = block + } + + fun onStop(block: (() -> Unit)) { + onStop = block + } + + fun onDestroy(block: (() -> Unit)) { + onDestroy = block + } +} \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/ext/HeaderFooterAdapter.kt b/flap/src/main/java/me/yifeiyuan/flap/ext/HeaderFooterAdapter.kt index 26cbd1da..5bbabbc3 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/ext/HeaderFooterAdapter.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/ext/HeaderFooterAdapter.kt @@ -11,11 +11,11 @@ const val ITEM_VIEW_TYPE_HEADER = 2123321000 const val ITEM_VIEW_TYPE_FOOTER = 2123321001 class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view), ComponentConfig { - override fun isSwipeEnable(): Boolean { + override fun isSwipeEnabled(): Boolean { return false } - override fun isDragEnable(): Boolean { + override fun isDragEnabled(): Boolean { return false } @@ -30,11 +30,11 @@ class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view), ComponentCon class FooterViewHolder(view: View) : RecyclerView.ViewHolder(view), ComponentConfig { - override fun isSwipeEnable(): Boolean { + override fun isSwipeEnabled(): Boolean { return false } - override fun isDragEnable(): Boolean { + override fun isDragEnabled(): Boolean { return false } diff --git a/flap/src/main/java/me/yifeiyuan/flap/ext/SwipeDragHelper.kt b/flap/src/main/java/me/yifeiyuan/flap/ext/SwipeDragHelper.kt index 75ffe66a..48ecd1cd 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/ext/SwipeDragHelper.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/ext/SwipeDragHelper.kt @@ -76,7 +76,7 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int { var finalDragFlags = if (dragFlags != FLAG_UN_SET) dragFlags else getDefaultDragFlags(recyclerView, viewHolder) if (viewHolder is ComponentConfig) { - if (viewHolder.isDragEnable()) { + if (viewHolder.isDragEnabled()) { finalDragFlags = if (viewHolder.getDragFlags() == FLAG_UN_SET) finalDragFlags else 0 } else { finalDragFlags = FLAG_DISABLE @@ -86,7 +86,7 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback var finalSwipeFlags = if (swipeFlags != FLAG_UN_SET) swipeFlags else getDefaultSwipeFlags(recyclerView, viewHolder) if (viewHolder is ComponentConfig) { - if (viewHolder.isSwipeEnable()) { + if (viewHolder.isSwipeEnabled()) { finalSwipeFlags = if (viewHolder.getSwipeFlags() == FLAG_UN_SET) finalSwipeFlags else 0 } else { finalSwipeFlags = FLAG_DISABLE diff --git a/flap/src/main/res/values/ids.xml b/flap/src/main/res/values/ids.xml index 20ee0175..0d2c4cc4 100644 --- a/flap/src/main/res/values/ids.xml +++ b/flap/src/main/res/values/ids.xml @@ -1,5 +1,4 @@ - \ No newline at end of file diff --git a/othermodule/src/main/java/me/yifeiyuan/ktx/foundation/othermodule/JavaModuleComponentDelegate.java b/othermodule/src/main/java/me/yifeiyuan/ktx/foundation/othermodule/JavaModuleComponentDelegate.java index 29af248a..5db25199 100644 --- a/othermodule/src/main/java/me/yifeiyuan/ktx/foundation/othermodule/JavaModuleComponentDelegate.java +++ b/othermodule/src/main/java/me/yifeiyuan/ktx/foundation/othermodule/JavaModuleComponentDelegate.java @@ -30,7 +30,7 @@ public JavaModuleComponent onCreateViewHolder(@NotNull LayoutInflater inflater, } @Override - public long getItemId(@NotNull Object model) { + public long getItemId(@NotNull Object model, int position) { return 0; }