>我参考vogella-tutorial进行数据绑定
>我要做的是:使用dataBinding检测每个Item的回收器视图行中的onClick的最佳方法是什么
activity_second.xml
rowlayout.xml
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter
MyAdapter.java
public class MyAdapter extends MyBaseAdapter {
List
最佳答案
不确定你是否已找到解决方案,但我设法很容易做到了.
原文链接:https://www.f2er.com/android/430149.html1)修改onCreateViewHolder方法,如下所示:
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
// create a new view
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater,false);
MainActivityPresenter presenter = new MainActivityPresenter(this,parent.getContext());
binding.setVariable(BR.presenter,presenter);
// set the view's size,paddings and layout parameters
return new MyViewHolder(binding);
}
2)使MyAdapter实现MainActivityContract.View,最后看起来如下:
公共类MyAdapter扩展了RecyclerView.Adapter< MyAdapter.MyViewHolder>实现MainActivityContract.View
3)在MyAdapter中实现必要的方法;例如:
@Override
public void showData(TemperatureData data) {
String clickedItemCelsius = data.getCelsius();
}
viewmodels.MainActivityPresenter"/>
5)最后在RelativeLayout下挂钩你的onClick事件:
希望能帮助到你!