最火開源框架MVVMhabit
一. 準(zhǔn)備工作
二. 快速上手
三. 數(shù)據(jù)綁定
四. 其他
1、layout:
創(chuàng)建list.xml文件、加入ListView標(biāo)簽、設(shè)置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>
創(chuàng)建list_item.xml文件唆樊,item我們?yōu)榱朔奖阒患尤胍粋€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:創(chuàng)建ListViewModel.java瓤帚、綁定ListView臭猜、添加測試數(shù)據(jù)
public class ListViewModel extends BaseViewModel {
//綁定ListVIew
public ObservableField<ListAdapter> list = new ObservableField<>();
public ListViewModel(@NonNull Application application) {
super(application);
//創(chuàng)建ItemBean泛型數(shù)組
ArrayList<ItemBean> itemBeans = new ArrayList<>();
itemBeans.add(new ItemBean("女朋友"));//添加
itemBeans.add(new ItemBean("是我的"));
list.set(new ListAdapter(getApplication(),itemBeans));//set List的數(shù)據(jù)
}
}
3挪钓、Bean:創(chuàng)建基本數(shù)據(jù)類型ItemBean.java批销,xml創(chuàng)建的時候只加了一個TextView所以這里只有一個字符串變量title洒闸,創(chuàng)建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:創(chuàng)建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);
//設(shè)置Tag
convertView.setTag(holder);
} else {
holder = (CoinsDetailViewHolder) convertView.getTag();
binding = (ListItemBinding) holder.getItemCoinsDetailBinding();
}
//通過binding設(shè)置當(dāng)前的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;
}
}
}