我希望在我的RecyclerView行中处理点击事件,目前通过将Presenter传递给ViewHolder来完成绑定。
Presenter接口:
interface IMainPresenter{
public void showDetail(Pojo pojo);
}
视图持有者:
class ViewHolder(itemView: View, val parent: ViewGroup?, private val binding: ListMainBinding) : RecyclerView.ViewHolder(itemView) {
fun bind(pojo: Pojo, mainPresenter: IMainPresenter) {
binding.pojo = pojo
binding.mainPresenter = mainPresenter
binding.executePendingBindings()
}
}
在我的布局中,我在onClick属性上调用了我的Presenter的一个方法。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View" />
<variable
name="mainPresenter"
type="com.noisyninja.androidlistpoc.views.main.IMainPresenter" />
<variable
name="pojo"
type="com.noisyninja.androidlistpoc.model.Pojo" />
</data>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:onClick="@{(view) -> mainPresenter.showDetail(pojo)}"
...
...
...
我认为传递Presenter不是一个好的设计,但有什么更好的方法来处理行点击事件并触发Presenter中的方法?