最火开源框架MVVMhabit
一. 准备工作
二. 快速上手
三. 数据绑定
四. 其他
1、layout:
创建list.xml文件、加入ListView标签、设置adapter(后面绑定需要)
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="viewModel" type="com.makuan.template.ui.list.ListViewModel" />
<import type="com.makuan.template.R" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
app:adapter="@{viewModel.list}"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
创建list_item.xml文件,item我们为了方便只加入一个TextVIew
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="com.makuan.template.ui.list.ItemBean" />
<variable name="CoinsDetail" type="com.makuan.template.ui.list.ItemBean" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/coins_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{CoinsDetail.title}" />
</RelativeLayout>
</layout>
2、ViewModel:创建ListViewModel.java、绑定ListView、添加测试数据
public class ListViewModel extends BaseViewModel {
//绑定ListVIew
public ObservableField<ListAdapter> list = new ObservableField<>();
public ListViewModel(@NonNull Application application) {
super(application);
//创建ItemBean泛型数组
ArrayList<ItemBean> itemBeans = new ArrayList<>();
itemBeans.add(new ItemBean("女朋友"));//添加
itemBeans.add(new ItemBean("是我的"));
list.set(new ListAdapter(getApplication(),itemBeans));//set List的数据
}
}
3、Bean:创建基本数据类型ItemBean.java,xml创建的时候只加了一个TextView所以这里只有一个字符串变量title,创建set get ItemBean
public class ItemBean {
public String title;
public ItemBean(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
4、Adapter:创建ListAdapter.java
public class ListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<ItemBean> coins_details;
public ListAdapter(Context mContext, ArrayList<ItemBean> coins_details) {
this.mContext = mContext;
this.coins_details = coins_details;
}
@Override
public int getCount() {
return coins_details.size();
}
@Override
public Object getItem(int position) {
return coins_details.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListItemBinding binding = null;
CoinsDetailViewHolder holder = null;
if (convertView == null) {
holder = new CoinsDetailViewHolder();
//获取item布局的binding
binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.list_item, parent, false);
//获取布局
convertView = binding.getRoot();
//缓存binding到holder
holder.setItemCoinsDetailBinding(binding);
//设置Tag
convertView.setTag(holder);
} else {
holder = (CoinsDetailViewHolder) convertView.getTag();
binding = (ListItemBinding) holder.getItemCoinsDetailBinding();
}
//通过binding设置当前的item对象,然后它就会自动给textview赋值
binding.setCoinsDetail(coins_details.get(position));
return convertView;
}
//viewholder类里只有一个binding对象和它的get,set方法
private class CoinsDetailViewHolder {
private ViewDataBinding itemCoinsDetailBinding;
public void setItemCoinsDetailBinding(ViewDataBinding itemCoinsDetailBinding) {
this.itemCoinsDetailBinding = itemCoinsDetailBinding;
}
public ViewDataBinding getItemCoinsDetailBinding() {
return itemCoinsDetailBinding;
}
}
}