课堂笔记
- 把 categoryId 是否是当前页面 Id 放在逻辑层判断
- 首先在 ICategoryPagerCallback 定义获取 categoryId 的方法
//获取分类Id
int getCategoryId();
- HomePagerFragment 实现该方法,返回 materialId
@Override
public int getCategoryId() {
return mMaterialId;
}
- 在逻辑层获取 categoryId 并对各个状态进行判断
- 请求成功
private void handleHomePagerContentResult(HomePagerContent pagerContent, int categoryId) {
for (ICategoryPagerCallback callback : callbacks) {
if (callback.getCategoryId() == categoryId) {
if (pagerContent == null || pagerContent.getData().size() == 0) {
callback.onEmpty();
} else {
callback.onContentLoaded(pagerContent.getData());
}
}
}
}
private void handleNetworkError(int categoryId) {
for (ICategoryPagerCallback callback : callbacks) {
if (callback.getCategoryId() == categoryId) {
callback.onError();
}
}
}
for (ICategoryPagerCallback callback : callbacks) {
if (callback.getCategoryId() == categoryId) {
callback.onLoading();
}
}
- ICategoryPagerCallback 接口中方法就不再需要参数 categoryId 了,这样,onLoading,onEmpty,onError 也可以抽取到 IBaseCallback 里了
public interface ICategoryPagerCallback extends IBaseCallback {
//加载分类内容
void onContentLoaded(List<HomePagerContent.DataBean > contents);
//加载更多网络错误
void onLoadMoreError();
//没有更多内容了
void onLoadMoreEmpty();
//获取分类Id
int getCategoryId();
//加载到了更多内容
void onLoadMoreLoaded(List<HomePagerContent.DataBean > contents);
//加载轮播图
void onLooperListLoaded(List<HomePagerContent.DataBean > contents);
}
@Override
public void onContentLoaded(List<HomePagerContent.DataBean> contents) {
setUpState(State.SUCCESS);
}
@Override
public void onLoading() {
setUpState(State.LOADING);
}
@Override
public void onError() {
setUpState(State.ERROR);
}
@Override
public void onEmpty() {
setUpState(State.EMPTY);
}
@Override
public void onLoadMoreError() {
}
@Override
public void onLoadMoreEmpty() {
}