如何解决将视图模型中的方法作为自定义 android 组件中的表达式传递
我有一个自定义组件,它将状态更改侦听器作为输入来触发 OnClick 方法中的 onChecked 和 onUnchecked 事件。现在我想将此组件迁移到 DATA BINDING 并尝试将 onClick 方法分离为两个方法,如下所示:
<declare-styleable name="CustomView">
<attr name="onChecked" format="string" />
<attr name="onUnchecked" format="string" />
<declare-styleable name="ToggleImageView">
然后在课堂上
class CustomView: AppCompatImageView,View.OnClickListener {
....
private lateinit var mCallbacks: DeclaredOnStateChangedListener
constructor(context: Context) : super(context)
constructor (context: Context,attrs: AttributeSet?) : super(context,attrs) {
val checkedMethodName = typedArray.getString(R.styleable.CustomView_onChecked).orEmpty()
val unCheckedMethodName = typedArray.getString(R.styleable.CustomView_onUnchecked).orEmpty()
mCallbacks =
DeclaredOnStateChangedListener(this,checkedMethodName,unCheckedMethodName)
}
override fun onClick(v: View?) {
if (mState == CHECKED) {
....
v?.let { mCallbacks.onUnchecked(it) }
} else {
.....
v?.let { mCallbacks.onChecked(it) }
}
}
}
DeclaredOnStateChangedListener 是这样实现的:
private interface OnStateChangedListener {
fun onChecked(v: View)
fun onUnchecked(v: View) {}
}
class DeclaredOnStateChangedListener(
private val mHostView: View,private val checkedMethodName: String,private val uncheckedMethodName: String
) :
OnStateChangedListener {
private var mResolvedMethod: Method? = null
private var mResolvedContext: Context? = null
private fun resolveMethod(contextInput: Context?,mMethodName: String) {
var context = contextInput
while (context != null) {
try {
if (!context.isRestricted) {
val method = context.javaClass.getMethod(
mMethodName,View::class.java
)
mResolvedMethod = method
mResolvedContext = context
return
}
} catch (e: NoSuchMethodException) {
// Failed to find method,keep searching up the hierarchy.
}
context = if (context is ContextWrapper) {
context.baseContext
} else {
// Can't search up the hierarchy,null out and fail.
null
}
}
val id = mHostView.id
val idText =
if (id == View.NO_ID) "" else " with id '" + mHostView.context.resources.getResourceEntryName(
id
) + "'"
throw IllegalStateException(
"Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.javaClass + idText
)
}
private fun doExecuteMathod(mMethodName: String,v: View) {
if (mResolvedMethod == null) {
resolveMethod(mHostView.context,mMethodName)
}
try {
mResolvedMethod!!.invoke(mResolvedContext,v)
} catch (e: IllegalAccessException) {
throw IllegalStateException(
"Could not execute non-public method for app:${mMethodName}",e
)
} catch (e: InvocationTargetException) {
throw IllegalStateException(
"Could not execute method for app:${mMethodName}",e
)
}
}
override fun onChecked(v: View) {
if (checkedMethodName.isNotEmpty())
doExecuteMathod(checkedMethodName,v)
}
override fun onUnchecked(v: View) {
if (uncheckedMethodName.isNotEmpty())
doExecuteMathod(uncheckedMethodName,v)
}
}
我的问题,当我想像这样使用它时:我遇到绑定问题并且项目未构建
<CustomView
....
app:onChecked="@{()->viewModel.doNavigate()}"
app:onUnchecked="@{()-> viewModel.removeFromFavorite()}"
.... />
知道如何在 Android 视图中像 onClick 属性那样做吗??
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。