列表說完了,接下來就是詳情頁了,詳情頁無非就是加載數(shù)據(jù)伍派,其實很簡單江耀,和列表實現(xiàn)相同的規(guī)范。
BaseDetailViewModel
原理和BaseListViewModel一樣诉植,比列表更簡單祥国,僅僅需要實現(xiàn)onLoadDatailHttpRequest函數(shù)即可。
public class TicketDetailViewModel extends BaseDetailViewModel{
public TicketDetailViewModel(){
onLoad();
}
@Override
public Call<HttpResult<Ticket>> onLoadDatailHttpRequest() {
return HttpServiceGenerator.createService().getTicketDetail("10002");
}
}
對應(yīng)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.TicketDetailViewModel" />
<import type="com.bigkoo.mvvmframeworkdemo.model.Ticket"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@{((Ticket)viewModel.detail).spotName}" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@{@string/productPrice(((Ticket)viewModel.detail).price??@string/zero)}" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@{((Ticket)viewModel.detail).detailInfo}" />
</LinearLayout>
</layout>
同樣也支持 設(shè)置狀態(tài)倍踪,include 對應(yīng)狀態(tài)的xml 進(jìn)去 Activity的xml即可。
BaseRefreshDetailViewModel
BaseRefreshDetailViewModel 則是 滿足下拉刷新需求的DetailViewModel索昂,在Activity 自行加入 SwipeRefreshLayout建车,給SwipeRefreshLayout添加屬性:
app:onRefreshListener="@{viewModel.onRefreshListener}"
app:refreshing="@={viewModel.refreshing}