parent
6852689604
commit
1225af46a4
@ -1,25 +1,31 @@ |
|||||||
package io.legado.app.base.adapter |
package io.legado.app.base.adapter |
||||||
|
|
||||||
import android.content.Context |
import android.content.Context |
||||||
|
import androidx.viewbinding.ViewBinding |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by Invincible on 2017/11/24. |
* Created by Invincible on 2017/11/24. |
||||||
* |
* |
||||||
* item代理, |
* item代理, |
||||||
*/ |
*/ |
||||||
abstract class ItemViewDelegate<ITEM>(protected val context: Context, val layoutId: Int) { |
abstract class ItemViewDelegate<ITEM, VB : ViewBinding>(protected val context: Context) { |
||||||
|
|
||||||
/** |
/** |
||||||
* 如果使用了事件回调,回调里不要直接使用item,会出现不更新的问题, |
* 如果使用了事件回调,回调里不要直接使用item,会出现不更新的问题, |
||||||
* 使用getItem(holder.layoutPosition)来获取item, |
* 使用getItem(holder.layoutPosition)来获取item, |
||||||
* 或者使用registerListener(holder: ItemViewHolder, position: Int) |
* 或者使用registerListener(holder: ItemViewHolder, position: Int) |
||||||
*/ |
*/ |
||||||
abstract fun convert(holder: ItemViewHolder, item: ITEM, payloads: MutableList<Any>) |
abstract fun convert( |
||||||
|
holder: ItemViewHolder, |
||||||
|
binding: VB, |
||||||
|
item: ITEM, |
||||||
|
payloads: MutableList<Any> |
||||||
|
) |
||||||
|
|
||||||
|
|
||||||
/** |
/** |
||||||
* 注册事件 |
* 注册事件 |
||||||
*/ |
*/ |
||||||
abstract fun registerListener(holder: ItemViewHolder) |
abstract fun registerListener(holder: ItemViewHolder, binding: VB) |
||||||
|
|
||||||
} |
} |
@ -1,9 +1,10 @@ |
|||||||
package io.legado.app.base.adapter |
package io.legado.app.base.adapter |
||||||
|
|
||||||
import android.view.View |
|
||||||
import androidx.recyclerview.widget.RecyclerView |
import androidx.recyclerview.widget.RecyclerView |
||||||
|
import androidx.viewbinding.ViewBinding |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by Invincible on 2017/11/28. |
* Created by Invincible on 2017/11/28. |
||||||
*/ |
*/ |
||||||
class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) |
@Suppress("MemberVisibilityCanBePrivate") |
||||||
|
class ItemViewHolder(val binding: ViewBinding) : RecyclerView.ViewHolder(binding.root) |
@ -1,58 +0,0 @@ |
|||||||
@file:Suppress("RedundantVisibilityModifier", "unused") |
|
||||||
|
|
||||||
package io.legado.app.utils.viewbindingdelegate |
|
||||||
|
|
||||||
import android.view.View |
|
||||||
import androidx.annotation.IdRes |
|
||||||
import androidx.fragment.app.DialogFragment |
|
||||||
import androidx.lifecycle.LifecycleOwner |
|
||||||
import androidx.viewbinding.ViewBinding |
|
||||||
|
|
||||||
private class DialogFragmentViewBindingProperty<F : DialogFragment, T : ViewBinding>( |
|
||||||
viewBinder: (F) -> T |
|
||||||
) : ViewBindingProperty<F, T>(viewBinder) { |
|
||||||
|
|
||||||
override fun getLifecycleOwner(thisRef: F): LifecycleOwner { |
|
||||||
return if (thisRef.view == null) thisRef.viewLifecycleOwner else thisRef |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Create new [ViewBinding] associated with the [DialogFragment] |
|
||||||
*/ |
|
||||||
@JvmName("viewBindingDialogFragment") |
|
||||||
public fun <F : DialogFragment, T : ViewBinding> DialogFragment.dialogViewBinding( |
|
||||||
viewBinder: (F) -> T |
|
||||||
): ViewBindingProperty<F, T> { |
|
||||||
return DialogFragmentViewBindingProperty(viewBinder) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Create new [ViewBinding] associated with the [DialogFragment] |
|
||||||
* |
|
||||||
* @param vbFactory Function that create new instance of [ViewBinding]. `MyViewBinding::bind` can be used |
|
||||||
*/ |
|
||||||
@JvmName("viewBindingDialogFragment") |
|
||||||
public inline fun <F : DialogFragment, T : ViewBinding> DialogFragment.dialogViewBinding( |
|
||||||
crossinline vbFactory: (View) -> T, |
|
||||||
crossinline viewProvider: (F) -> View |
|
||||||
): ViewBindingProperty<F, T> { |
|
||||||
return dialogViewBinding { fragment -> vbFactory(viewProvider(fragment)) } |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Create new [ViewBinding] associated with the [DialogFragment][this] |
|
||||||
* |
|
||||||
* @param vbFactory Function that create new instance of [ViewBinding]. `MyViewBinding::bind` can be used |
|
||||||
* @param viewBindingRootId Id of the root view from your custom view |
|
||||||
*/ |
|
||||||
@Suppress("unused") |
|
||||||
@JvmName("viewBindingDialogFragment") |
|
||||||
public inline fun <T : ViewBinding> DialogFragment.dialogViewBinding( |
|
||||||
crossinline vbFactory: (View) -> T, |
|
||||||
@IdRes viewBindingRootId: Int |
|
||||||
): ViewBindingProperty<DialogFragment, T> { |
|
||||||
return viewBinding(vbFactory) { fragment: DialogFragment -> |
|
||||||
fragment.dialog!!.window!!.decorView.findViewById(viewBindingRootId) |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue