|
@@ -0,0 +1,314 @@
|
|
|
|
|
+package com.sheep.gamegroup.view.fragment;
|
|
|
|
|
+
|
|
|
|
|
+import android.app.Activity;
|
|
|
|
|
+import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
|
+import android.support.v7.widget.RecyclerView;
|
|
|
|
|
+import android.view.View;
|
|
|
|
|
+
|
|
|
|
|
+import com.jcodecraeer.xrecyclerview.XRecyclerView;
|
|
|
|
|
+import com.sheep.gamegroup.absBase.AbsObserver;
|
|
|
|
|
+import com.sheep.gamegroup.absBase.ILoadMore;
|
|
|
|
|
+import com.sheep.gamegroup.absBase.IRefresh;
|
|
|
|
|
+import com.sheep.gamegroup.model.api.ApiService;
|
|
|
|
|
+import com.sheep.gamegroup.model.entity.BaseMessage;
|
|
|
|
|
+import com.sheep.gamegroup.model.util.SheepSubscriber;
|
|
|
|
|
+import com.sheep.gamegroup.util.CommonUtil;
|
|
|
|
|
+import com.sheep.gamegroup.util.DataUtil;
|
|
|
|
|
+import com.sheep.gamegroup.util.ListUtil;
|
|
|
|
|
+import com.sheep.gamegroup.util.SysAppUtil;
|
|
|
|
|
+import com.sheep.gamegroup.util.ViewUtil;
|
|
|
|
|
+import com.sheep.jiuyan.samllsheep.R;
|
|
|
|
|
+import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
|
|
+import com.sheep.jiuyan.samllsheep.base.BaseFragment;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+import butterknife.BindView;
|
|
|
|
|
+import io.reactivex.Observable;
|
|
|
|
|
+import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
|
|
+import io.reactivex.schedulers.Schedulers;
|
|
|
|
|
+import rx.functions.Action1;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Created by realicing on 2018/11/22.
|
|
|
|
|
+ * realicing@sina.com
|
|
|
|
|
+ * 可设置每次请求多个接口
|
|
|
|
|
+ * 自己可以进行刷新与加载更多,使用com.jcodecraeer.xrecyclerview.XRecyclerView来实现,子类提供网络接口相关
|
|
|
|
|
+ */
|
|
|
|
|
+public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements IRefresh, ILoadMore {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int getLayoutId() {
|
|
|
|
|
+ return R.layout.net_empty_xrecycler;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected Activity activity;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onViewCreated() {
|
|
|
|
|
+ activity = getActivity();
|
|
|
|
|
+ initView();
|
|
|
|
|
+ initListener();
|
|
|
|
|
+ switch (refreshDataType()) {
|
|
|
|
|
+ case REFRESH_ON_CREATE:
|
|
|
|
|
+ ViewUtil.refreshXrv(view_list);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case REFRESH_ON_RESUME:
|
|
|
|
|
+ break;
|
|
|
|
|
+ case REFRESH_ON_YOURSELF:
|
|
|
|
|
+ default:
|
|
|
|
|
+ notifyDataSetChanged(0);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static final int REFRESH_ON_CREATE = 0;
|
|
|
|
|
+ public static final int REFRESH_ON_RESUME = 1;
|
|
|
|
|
+ public static final int REFRESH_ON_YOURSELF = -1;
|
|
|
|
|
+ @BindView(R.id.title)
|
|
|
|
|
+ protected View title;
|
|
|
|
|
+ @BindView(R.id.empty_view)
|
|
|
|
|
+ protected View empty_view;
|
|
|
|
|
+ @BindView(R.id.check_net_ll)
|
|
|
|
|
+ protected View check_net_ll;
|
|
|
|
|
+ @BindView(R.id.view_list)
|
|
|
|
|
+ protected XRecyclerView view_list;
|
|
|
|
|
+
|
|
|
|
|
+ protected BaseMessage lastMessage;//最后一个网络获取的结果
|
|
|
|
|
+ protected BaseMessage lastMessage2;//最后一个网络获取的结果
|
|
|
|
|
+ protected int page = 1;//页数
|
|
|
|
|
+ protected int per_page = DataUtil.PER_PAGE;
|
|
|
|
|
+ private boolean loadMore;
|
|
|
|
|
+
|
|
|
|
|
+ public void initView() {
|
|
|
|
|
+ title.setVisibility(View.GONE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected View bottomLine;
|
|
|
|
|
+
|
|
|
|
|
+ public void initListener() {
|
|
|
|
|
+ view_list.setLoadingListener(new XRecyclerView.LoadingListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onRefresh() {
|
|
|
|
|
+ refreshData();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onLoadMore() {
|
|
|
|
|
+ loadMoreData();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ bottomLine = ViewUtil.setBottomLine(view_list, getNoMoreCallBack());
|
|
|
|
|
+ view_list.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
|
|
|
|
|
+ view_list.setAdapter(getAdapter());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void loadMoreData() {
|
|
|
|
|
+ if (!loadMore) {
|
|
|
|
|
+ loadMore = true;
|
|
|
|
|
+ if (ListUtil.size(list) >= per_page * page) {
|
|
|
|
|
+ page += 1;
|
|
|
|
|
+ initData();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ view_list.setNoMore(true);
|
|
|
|
|
+ setNoMore(true);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ view_list.loadMoreComplete();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setNoMore(boolean noMore) {
|
|
|
|
|
+ this.noMore = noMore;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected List<T> list = ListUtil.emptyList();
|
|
|
|
|
+ protected List<T> lastCacheList = ListUtil.emptyList();
|
|
|
|
|
+ protected List<T2> list2 = ListUtil.emptyList();
|
|
|
|
|
+ protected List<T2> lastCacheList2 = ListUtil.emptyList();
|
|
|
|
|
+
|
|
|
|
|
+ //默认先获取缓存
|
|
|
|
|
+ protected boolean isFirstGetACache() {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void initData() {
|
|
|
|
|
+ final String urlKey = getKey(page, per_page);
|
|
|
|
|
+ if (isFirstGetACache()) {
|
|
|
|
|
+ //先尝试获取缓存数据
|
|
|
|
|
+ lastCacheList = DataUtil.getInstance().getCacheList(urlKey, getTClass());
|
|
|
|
|
+ loadList(lastCacheList);
|
|
|
|
|
+ }
|
|
|
|
|
+ final String urlKey2 = getKey2(page, per_page);
|
|
|
|
|
+ if (isFirstGetACache()) {
|
|
|
|
|
+ //先尝试获取缓存数据
|
|
|
|
|
+ lastCacheList2 = DataUtil.getInstance().getCacheList(urlKey2, getT2Class());
|
|
|
|
|
+ loadList2(lastCacheList2);
|
|
|
|
|
+ }
|
|
|
|
|
+ SysAppUtil.checkNet(new AbsObserver<Integer>() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onNext(Integer result) {
|
|
|
|
|
+ if (result != 0) {//无网络
|
|
|
|
|
+ if (check_net_ll != null)
|
|
|
|
|
+ check_net_ll.setVisibility(View.VISIBLE);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if (check_net_ll != null)
|
|
|
|
|
+ check_net_ll.setVisibility(View.GONE);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ getApi(SheepApp.getInstance().getNetComponent().getApiService())
|
|
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
+ .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onNext(BaseMessage baseMessage) {
|
|
|
|
|
+ lastMessage = baseMessage;
|
|
|
|
|
+ boolean isNewData = DataUtil.getInstance().isNewData(urlKey);
|
|
|
|
|
+ if (isNewData || !isFirstGetACache()) {
|
|
|
|
|
+ if (isFirstGetACache())
|
|
|
|
|
+ ListUtil.removeAll(list, lastCacheList);
|
|
|
|
|
+ List<T> newList = baseMessage.getDatas(getTClass());
|
|
|
|
|
+ loadList(newList);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ notifyDataSetChanged(1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onError(BaseMessage baseMessage) {
|
|
|
|
|
+ notifyDataSetChanged(1);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ getApi2(SheepApp.getInstance().getNetComponent().getApiService())
|
|
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
+ .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onNext(BaseMessage baseMessage) {
|
|
|
|
|
+ lastMessage2 = baseMessage;
|
|
|
|
|
+ boolean isNewData = DataUtil.getInstance().isNewData(urlKey2);
|
|
|
|
|
+ if (isNewData || !isFirstGetACache()) {
|
|
|
|
|
+ if (isFirstGetACache())
|
|
|
|
|
+ ListUtil.removeAll(list2, lastCacheList2);
|
|
|
|
|
+ List<T2> newList = baseMessage.getDatas(getT2Class());
|
|
|
|
|
+ loadList2(newList);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ notifyDataSetChanged(2);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onError(BaseMessage baseMessage) {
|
|
|
|
|
+ notifyDataSetChanged(2);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Action1<String> getNoMoreCallBack() {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onResume() {
|
|
|
|
|
+ super.onResume();
|
|
|
|
|
+ if (refreshDataType() == REFRESH_ON_RESUME) {
|
|
|
|
|
+ ViewUtil.refreshXrv(view_list);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 在哪里初始化数据:0 onViewCreated后, 1 onResume后, -1 不初始化
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public int refreshDataType() {
|
|
|
|
|
+ return REFRESH_ON_CREATE;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void clear() {
|
|
|
|
|
+ lastMessage = null;
|
|
|
|
|
+ lastMessage2 = null;
|
|
|
|
|
+ setNoMore(false);
|
|
|
|
|
+ empty_view.setVisibility(View.INVISIBLE);
|
|
|
|
|
+ list.clear();
|
|
|
|
|
+ list2.clear();
|
|
|
|
|
+ page = 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void refreshData() {
|
|
|
|
|
+ clear();
|
|
|
|
|
+ ViewUtil.notifyDataSetChanged(view_list);
|
|
|
|
|
+ initData();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected List<T> newList;
|
|
|
|
|
+
|
|
|
|
|
+ protected void loadList(List<T> addList) {
|
|
|
|
|
+ ListUtil.addAllItem(list, addList);
|
|
|
|
|
+ newList = addList;
|
|
|
|
|
+ notifyDataSetChanged(1);
|
|
|
|
|
+ }
|
|
|
|
|
+ protected List<T2> newList2;
|
|
|
|
|
+
|
|
|
|
|
+ protected void loadList2(List<T2> addList) {
|
|
|
|
|
+ ListUtil.addAllItem(list2, addList);
|
|
|
|
|
+ newList2 = addList;
|
|
|
|
|
+ notifyDataSetChanged(2);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void notifyDataSetChanged(int x) {
|
|
|
|
|
+ if (view_list == null)
|
|
|
|
|
+ view_list = findViewById(R.id.view_list);
|
|
|
|
|
+ if (empty_view == null)
|
|
|
|
|
+ empty_view = findViewById(R.id.empty_view);
|
|
|
|
|
+ loadMore = false;
|
|
|
|
|
+ updateEmptyView();
|
|
|
|
|
+ if (page == 1) {
|
|
|
|
|
+ view_list.refreshComplete();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ view_list.loadMoreComplete();
|
|
|
|
|
+ }
|
|
|
|
|
+ ViewUtil.notifyDataSetChanged(view_list);
|
|
|
|
|
+ notifyData(x);
|
|
|
|
|
+ view_list.getFootView().setVisibility(list.isEmpty() && list2.isEmpty() ? View.GONE : View.VISIBLE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected void updateEmptyView() {
|
|
|
|
|
+ CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onDestroyView() {
|
|
|
|
|
+ super.onDestroyView();
|
|
|
|
|
+ // any time,when you finish your activity or fragment,call this below
|
|
|
|
|
+ if (view_list != null) {
|
|
|
|
|
+ view_list.destroy(); // this will totally release XR's memory
|
|
|
|
|
+ view_list = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private boolean noMore = false;
|
|
|
|
|
+
|
|
|
|
|
+ public boolean isNoMore() {
|
|
|
|
|
+ return noMore;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected abstract RecyclerView.Adapter getAdapter();
|
|
|
|
|
+
|
|
|
|
|
+ protected abstract String getKey(int page, int per_page);
|
|
|
|
|
+ protected abstract String getKey2(int page, int per_page);
|
|
|
|
|
+
|
|
|
|
|
+ protected abstract Observable<BaseMessage> getApi(ApiService apiService);
|
|
|
|
|
+ protected abstract Observable<BaseMessage> getApi2(ApiService apiService);
|
|
|
|
|
+
|
|
|
|
|
+ protected abstract Class<T> getTClass();
|
|
|
|
|
+ protected abstract Class<T2> getT2Class();
|
|
|
|
|
+
|
|
|
|
|
+ //刷新数据的回调,子类可以实现自己的需求
|
|
|
|
|
+ public void notifyData(int x){
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+}
|