使用DataBinding来设置空状态(二)-重新加载

写在前面

这是使用DataBinding来设置空状态的第二篇,在上一篇中介绍了基本的绑定空状态的操作,而这一篇将在上一篇的基础上添加重新加载的功能,内容不多,但是还是蛮必要的。

看看效果

screenshot.gif

 

首先

修改我们的空状态布局,添加一个重试的button

注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频’并且免费分享源码解析视频

<?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>

如何利用这个button呢?对于重新加载这个功能来说,在这个视图里我们也仅仅需要考虑两点罢了

  1. 显示隐藏
  2. 重新加载事件

对于第一点,参照ContentLoadingProgressBar的方式,我们只需要根据error_code判断一下是否显示

而第二点,由于databinding可以在xml里绑定事件,所以我们可以添加一个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;
    }
    //使用回调处理错误及重新加载事件
    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

实际运用

由于需要处理回调的事件,基本每个页面都需要用到,所以建议在基类里做处理。

一般而言,基类通常会是抽象类。以笔者的习惯来说,基类里一般会有四个抽象方法。

  • protected abstract int getLayoutId();返回布局id

  • protected abstract void initView();初始化view

  • protected abstract void initEvent();处理事件

  • protected abstract void loadData(boolean isRefresh);

    加载数据,和重新加载操作一样,所以我们只需要重新调用loadData方法就好了

BaseActivity

/**
 * 页面描述:基类 ,协同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);//加载数据
    }

    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();
        }
    }
}

写在最后

对于空状态的展示经过这两篇文章,相信大家也有了自己的理解,处理空状态及其事件也明白该怎么做了,easy。

毕竟学到了才是属于自己的,授人以鱼不如授人以渔也正是这个道理。

至于使用databinding来处理空状态也是看了google的android-architecture受到的启发,推荐想了解架构的同学fork下来学习学习。

最后github地址:https://github.com/ditclear/StateBinding

注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频’并且免费分享源码解析视频

作者:ditclear
链接:https://www.jianshu.com/p/d89f25342906
來源:简书

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值