美文网首页
MVVM-Loading

MVVM-Loading

作者: tea也么 | 来源:发表于2019-11-19 15:25 被阅读0次

简介

引用https://github.com/luckybilly/Gloading
结合LoadedController,在BaseFragment中实现了控制多任务的界面加载等待

使用

  • 继承BaseFragment
  • 设置任务数TASK_COUNT
  • 开启加载 startLoading(TASK_COUNT);
  • 任务完成确认 loadedOneTask(source, true);
  • 设置重试按钮 protected void onReload(){}
public class HomeFragment extends BaseFragment<HomeViewModel, FragmentHomeBinding> {
    private static final int TASK_COUNT = 3;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 开启加载界面,并设置加载任务数目
        startLoading(TASK_COUNT);
        ...
    }

    private void bindUI() {
        mWeather.response().observe(getViewLifecycleOwner(), curWeather -> {
            if (curWeather != null) {
                mBinding.weather.setWeather(curWeather);
                // 每当任务执行完一个任务,执行该方法
                loadedOneTask(mWeather, true);
            } else {
                loadedOneTask(mWeather, false);
            }
        });

        mCalendar.response().observe(getViewLifecycleOwner(), calendar -> {
            if (calendar != null) {
                mBinding.calendar.setCalendar(calendar);
                loadedOneTask(mCalendar, true);
            } else {
                loadedOneTask(mCalendar, false);
            }
        });

        mBus.response().observe(getViewLifecycleOwner(), bus -> {
            if (bus != null) {
                mBinding.bus.bus.setText(getBusTime(bus));
                loadedOneTask(mBus, true);
            } else {
                loadedOneTask(mBus, false);
            }
        });
        ...
    }

   // 数据加载失败界面的重试按钮点击回调事件
    @CheckNet
    @Override
    protected void onReload() {
        startLoading(TASK_COUNT);
        mWeather.onReload(null);
        mCalendar.request().setValue(getCurTime());
        mBus.request().setValue(isBus0Time() ? BusSource.bus_125_0_url : BusSource.bus_125_1_url);
    }
}

相关文章

  • MVVM-Loading

    简介 引用https://github.com/luckybilly/Gloading结合LoadedContro...

网友评论

      本文标题:MVVM-Loading

      本文链接:https://www.haomeiwen.com/subject/snnwictx.html