這一章是列表ViewModel的具體使用小結(jié)。
Model
比如我們要請(qǐng)求一個(gè)門(mén)票列表,地址為http://www.bigkoo.com/api/list/ticket
參數(shù)page和pageSize分別代表當(dāng)前頁(yè)和一頁(yè)多少個(gè)item,返回格式
{"code":0,"msg":"獲取詳情成功","content":[{"productId":"1200001","spotName":"門(mén)票名稱1"},{"productId":"1200002","spotName":"門(mén)票名稱2"}]}
那么則建立一個(gè)叫Ticket的Model,屬性有productId,spotName
public class Ticket {
/**
* productId : 923010086
* spotName : 藍(lán)水河漂流
*/
private String productId;
private String spotName;
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getSpotName() {
return spotName;
}
public void setSpotName(String spotName) {
this.spotName = spotName;
}
HttpService里面也加入對(duì)應(yīng)接口
@GET("api/list/ticket")Call<HttpResult<List<Ticket>>> getTicketList(@Query("page") int page, @Query("pageSize") int pageSize);
建立列表ViewModel
TicketListViewModel 就是具體實(shí)現(xiàn)的ViewModel,通過(guò)onLoadListHttpRequest指定請(qǐng)求的接口和返回的類(lèi)型Ticket
public class TicketListViewModel extends BaseRefreshRecyclerViewModel{
public TicketListViewModel(){
super(R.layout.item_ticketlist);
onListRefresh();
}
@Override
public Call<HttpResult<List<Ticket>>> onLoadListHttpRequest() {
return HttpServiceGenerator.createService().getTicketList(getPage(),getPageSize());
}
@Override
public void onItemClick(View v, int position, View itemView, Object item) {
}
}
這個(gè)列表item對(duì)應(yīng)的 item_ticketlist.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="com.bigkoo.mvvmframeworkdemo.model.Ticket" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="@{viewModel.spotName}"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
</layout>
這個(gè)列表Activity對(duì)應(yīng)的xml
<?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"
android:fitsSystemWindows="true">
<data>
<variable
name="viewModel"
type="com.bigkoo.mvvmframeworkdemo.viewmodel.TicketListViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/include_recyclerview_refresh"
app:viewModel="@{viewModel}"/>
</FrameLayout>
</layout>
然后就是Databinding綁定xml到Activity的知識(shí)了,具體請(qǐng)看源碼例子。
列表的幾個(gè)狀態(tài)可以拓展上面Activity的xml,改為
<?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"
android:fitsSystemWindows="true">
<data>
<variable
name="viewModel"
type="com.bigkoo.mvvmframeworkdemo.viewmodel.TicketListViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/include_recyclerview_refresh"
app:viewModel="@{viewModel}"/>
<include layout="@layout/include_status_error"
app:viewModel="@{viewModel}"/>
<include layout="@layout/include_status_networkerror"
app:viewModel="@{viewModel}"/>
</FrameLayout>
</layout>
這里舉一反三,服務(wù)器異常錯(cuò)誤對(duì)應(yīng)的xml,做成include供其他頁(yè)面公用,其他狀態(tài)的xml類(lèi)似。還記得嗎?ViewModel中的callback的時(shí)候已經(jīng)封裝了這些狀態(tài)的改變,所以現(xiàn)在我們不用在ViewModel中加任何代碼就能實(shí)現(xiàn)這些狀態(tài)界面的顯示和隱藏控制。
<?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.bigkoo.mvvmframework.viewmodel.BaseViewModel" />
<variable
name="emptyTips"
type="String" />
<import type="android.view.View" />
</data>
<LinearLayout
android:clickable="true"
app:setReload="@{viewModel}"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="@{viewModel.statusError ? View.VISIBLE : View.GONE}">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="20dp"
android:text="服務(wù)器異常" />
</LinearLayout>
</layout>
一個(gè)帶下拉刷新,滑動(dòng)到最后一行加載更多,不同狀態(tài)顯示不同界面的列表就這么輕松的實(shí)現(xiàn)了。