前言
刚加入MVVM阵营不久,踩了不少坑,今天说说关于DataBinding的坑
对于DataBinding如何实现 onClick,推荐大家看看这篇文章
DataBinding onClick 的几种点击方式
如果有跟我一样上面方法都试遍了,但就是没能实现效果的,可以往下看
正题
1.我的方法定义位置(CommentsViewModel.java)
public class CommentsViewModel extends BaseVM<CommentsActivity> implements DataInterface,INormalView {
private CommentsModel model;
private CommentsAdapter adapter;
private int page=1;
public void setAdapter(CommentsAdapter adapter) {
this.adapter = adapter;
}
public CommentsViewModel() {
model=new CommentsModel(this);
}
//点击按钮要实现的功能
public void onBtnClick(View v){
String str= getComments(model.dataList);
Log.i("str",str);
}
private String getComments(List<CommentsInfo> dataList) {
StringBuilder sb=new StringBuilder();
for (CommentsInfo bean :dataList) {
sb.append(bean.getName()).append(":").append(bean.getDescription().equals("")?"no":bean.getDescription()).append("\n");
}
return sb.toString();
}
...
}
网上看了很多文章,大部分都是只把他自己认为关键的一部分贴出来,我觉得这种很恶心。我会把所有牵扯到的类全部放进来,做到前后连贯。
2.我的方法调用(使用)位置(activity_comments.xml)
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="viewModel"
type="com.example.mingho.databindingdemo.activity.comments_activity.CommentsViewModel"/>
</data>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/commentsRecycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{viewModel::onBtnClick}"
android:text="提交"/>
</LinearLayout>
</layout>
3.我的Activity()
public class CommentsActivity extends AppCompatActivity implements IBaseView {
ActivityCommentsBinding commentsBinding;
CommentsAdapter adapter;
CommentsViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
commentsBinding=DataBindingUtil.setContentView(this,R.layout.activity_comments);
initView();
}
private void initView() {
adapter=new CommentsAdapter(this);
commentsBinding.commentsRecycler.setLayoutManager(new LinearLayoutManager(this));
commentsBinding.commentsRecycler.setAdapter(adapter);
if (viewModel==null){
viewModel=new CommentsViewModel();
}
viewModel.attachView(this);
viewModel.setAdapter(adapter);
viewModel.refresh();
}
...
}
有没有细心的道友发现是少了什么,导致点击完全没有反应?好了,不卖关子了。看这里:
private void initView() {
adapter=new CommentsAdapter(this);
commentsBinding.commentsRecycler.setLayoutManager(new LinearLayoutManager(this));
commentsBinding.commentsRecycler.setAdapter(adapter);
if (viewModel==null){
viewModel=new CommentsViewModel();
}
commentsBinding.setViewModel(viewModel);//添加的代码
viewModel.attachView(this);
viewModel.setAdapter(adapter);
viewModel.refresh();
}
就是因为这个东西,害我折腾半天,在我上网搜索并尝试了近百个例子后,终于找到解决方法。
基类和接口定义我就不贴出来了,借鉴大佬的东西(Demo直通车)
我把涉及到的Model,实体类,Adapter放在这儿:
- CommentsModel.java
public class CommentsModel {
public List<CommentsInfo> dataList=new ArrayList<>();
public DataInterface dataInterface;
public CommentsModel(DataInterface dataInterface) {
this.dataInterface = dataInterface;
}
public void getData() {
CommentsInfo info=new CommentsInfo("郭德纲");
CommentsInfo info2=new CommentsInfo("于谦");
CommentsInfo info3=new CommentsInfo("郭麒麟");
CommentsInfo info4=new CommentsInfo("岳云鹏");
CommentsInfo info5=new CommentsInfo("孙越");
dataList.add(info);
dataList.add(info2);
dataList.add(info3);
dataList.add(info4);
dataList.add(info5);
if (dataList.size()>0){
dataInterface.success(dataList);
}else {
dataInterface.error("加载数据失败");
}
}
}
- CommentsInfo.java
public class CommentsInfo {
private String name="John";
private String description="";
public CommentsInfo() {
}
public CommentsInfo(String name, String description) {
this.name = name;
this.description = description;
}
public CommentsInfo(String name) {
this.name = name;
}
/***
*getter and setter
**/
}
- CommentsAdapter.java
public class CommentsAdapter extends BaseRecyclerAdapter<CommentsInfo,BaseRecyclerViewHolder> {
public CommentsAdapter(Context context) {
super(context);
}
@Override
public void onBindVH(BaseRecyclerViewHolder holder, int i) {
ViewDataBinding dataBinding=holder.getDataBinding();
dataBinding.setVariable(BR.comments,dataList.get(i));
dataBinding.executePendingBindings();
}
@Override
public BaseRecyclerViewHolder onCreateVH(ViewGroup viewGroup, int i) {
ViewDataBinding dataBinding=DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.item_comments,viewGroup,false);
return new BaseRecyclerViewHolder(dataBinding);
}
}
结语
-
其实呢,我写文章主要是便于自己理解和记忆。如果很久不用,某一天 需要用到它,可能仍然需要上网搜索近百篇文章,这时它就能解决燃眉之急;
-
因为主要是给自己看的,所以写的很随性,语言组织或者代码逻辑若有待优化的地方,希望前辈们不吝啬的指出,晚辈积极改进;
-
我只是一名小菜鸟,不能说是指导别人怎么怎么做,最多只能算和我同级别的道友们技术交流。
网友评论