寫(xiě)在前面
這是使用DataBinding來(lái)設(shè)置空狀態(tài)的第二篇认轨,在上一篇中介紹了基本的綁定空狀態(tài)的操作,而這一篇將在上一篇的基礎(chǔ)上添加重新加載的功能,內(nèi)容不多翅睛,但是還是蠻必要的捷泞。
看看效果
![screenshot.gif](https://github.com/ditclear/StateBinding/blob/master/screenshot.gif?raw=true)
首先
修改我們的空狀態(tài)布局足删,添加一個(gè)重試的button
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<import type="android.view.View"/>
<variable
name="stateModel"
type="com.ditclear.app.state.StateModel"/>
</data>
<RelativeLayout
android:id="@+id/rv_empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:clickable="true"
android:focusableInTouchMode="true"
android:visibility="@{stateModel.empty?View.VISIBLE:View.GONE}">
<android.support.v4.widget.ContentLoadingProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="@{stateModel.progress?View.VISIBLE:View.GONE}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center"
android:orientation="vertical"
android:visibility="@{stateModel.progress?View.INVISIBLE:View.VISIBLE}">
<ImageView
android:id="@+id/none_data"
android:layout_width="345dp"
android:layout_height="180dp"
android:scaleType="fitCenter"
android:src="@{stateModel.emptyIconRes}"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_below="@+id/none_data"
android:layout_centerHorizontal="true"
android:text="@{stateModel.currentStateLabel}"
android:textSize="16sp"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/colorAccent"
android:text="重新加載"
android:textColor="@color/background"/>
</RelativeLayout>
</layout>
如何利用這個(gè)button
呢?對(duì)于重新加載這個(gè)功能來(lái)說(shuō)锁右,在這個(gè)視圖里我們也僅僅需要考慮兩點(diǎn)罷了
- 顯示隱藏
- 重新加載事件
對(duì)于第一點(diǎn)失受,參照ContentLoadingProgressBar
的方式讶泰,我們只需要根據(jù)error_code判斷一下是否顯示
而第二點(diǎn),由于databinding可以在xml里綁定事件拂到,所以我們可以添加一個(gè)onclick事件給button
// 顯示重新加載
public boolean isNetError() {
return this.emptyState == EmptyState.NET_ERROR;
}
//重新加載
public void reload() {
if (mCallBack != null) {
mCallBack.onReload();
}
}
public void attach(CallBack callBack) {
mCallBack = callBack;
}
public void detach() {
this.mCallBack = null;
}
//使用回調(diào)處理錯(cuò)誤及重新加載事件
public interface CallBack {
//失敗
public void onFailure(Throwable e);
//重新加載
public void onReload();
}
xml布局
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/colorAccent"
android:onClick="@{()->stateModel.reload()}"
android:text="重新加載"
android:textColor="@color/background"
android:visibility="@{stateModel.netError?View.VISIBLE:View.GONE}"/>
如此峻厚,我們的功能就大體完成了,easy
實(shí)際運(yùn)用
由于需要處理回調(diào)的事件谆焊,基本每個(gè)頁(yè)面都需要用到惠桃,所以建議在基類(lèi)里做處理。
一般而言辖试,基類(lèi)通常會(huì)是抽象類(lèi)辜王。以筆者的習(xí)慣來(lái)說(shuō),基類(lèi)里一般會(huì)有四個(gè)抽象方法罐孝。
protected abstract int getLayoutId();返回布局id
protected abstract void initView();初始化view
protected abstract void initEvent();處理事件
-
protected abstract void loadData(boolean isRefresh);
加載數(shù)據(jù)呐馆,和重新加載操作一樣,所以我們只需要重新調(diào)用loadData方法就好了
BaseActivity
/**
* 頁(yè)面描述:基類(lèi) 莲兢,協(xié)同stateModel處理error及重新加載操作
*
* Created by ditclear on 2017/7/1.
*/
public abstract class BaseActivity<VB extends ViewDataBinding> extends AppCompatActivity implements StateModel.CallBack {
protected StateModel mStateModel;
protected VB mBinding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, getLayoutId());
mStateModel = new StateModel();
mStateModel.attach(this);
initView();//初始化view
initEvent();//處理事件
loadData(true);//加載數(shù)據(jù)
}
protected abstract void initView();
protected abstract void initEvent();
protected abstract void loadData(boolean isRefresh);
@LayoutRes
//返回布局id
protected abstract int getLayoutId();
@Override
public void onFailure(Throwable e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onReload() {
loadData(true);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mStateModel != null) {
mStateModel.detach();
}
}
}
寫(xiě)在最后
對(duì)于空狀態(tài)的展示經(jīng)過(guò)這兩篇文章汹来,相信大家也有了自己的理解,處理空狀態(tài)及其事件也明白該怎么做了改艇,easy收班。
畢竟學(xué)到了才是屬于自己的,授人以魚(yú)不如授人以漁也正是這個(gè)道理谒兄。
至于使用databinding來(lái)處理空狀態(tài)也是看了google的android-architecture受到的啟發(fā)摔桦,推薦想了解架構(gòu)的同學(xué)fork下來(lái)學(xué)習(xí)學(xué)習(xí)。
最后github地址:https://github.com/ditclear/StateBinding