MVVM-Loading

简介

引用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);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容