|
|
@@ -1,19 +1,20 @@
|
|
|
package com.sheep.gamegroup.view.fragment;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
-import android.support.v4.widget.SwipeRefreshLayout;
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
import android.view.View;
|
|
|
|
|
|
-import com.chad.library.adapter.base.BaseQuickAdapter;
|
|
|
-import com.chad.library.adapter.base.BaseViewHolder;
|
|
|
+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;
|
|
|
@@ -28,17 +29,17 @@ 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/28.
|
|
|
+ * Created by realicing on 2018/9/3.
|
|
|
* realicing@sina.com
|
|
|
- * 可设置每次请求多个接口
|
|
|
- * android.support.v4.widget.SwipeRefreshLayout与RecyclerView来实现上拉刷新与下拉加载更多,子类提供网络接口相关
|
|
|
+ * 自己可以进行刷新与加载更多,使用com.jcodecraeer.xrecyclerview.XRecyclerView来实现,子类提供网络接口相关
|
|
|
*/
|
|
|
public abstract class BaseListFragment5<T> extends BaseFragment implements IRefresh, ILoadMore {
|
|
|
@Override
|
|
|
public int getLayoutId() {
|
|
|
- return R.layout.common_title_net_srl_rv;
|
|
|
+ return R.layout.net_empty_xrecycler;
|
|
|
}
|
|
|
|
|
|
protected Activity activity;
|
|
|
@@ -50,7 +51,7 @@ public abstract class BaseListFragment5<T> extends BaseFragment implements IRefr
|
|
|
initListener();
|
|
|
switch (refreshDataType()) {
|
|
|
case REFRESH_ON_CREATE:
|
|
|
- refreshData();
|
|
|
+// ViewUtil.refreshXrv(view_list);
|
|
|
break;
|
|
|
case REFRESH_ON_RESUME:
|
|
|
break;
|
|
|
@@ -64,16 +65,12 @@ public abstract class BaseListFragment5<T> extends BaseFragment implements IRefr
|
|
|
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)
|
|
|
- View title;
|
|
|
+ @BindView(R.id.empty_view)
|
|
|
+ protected View empty_view;
|
|
|
@BindView(R.id.check_net_ll)
|
|
|
- View check_net_ll;
|
|
|
- @BindView(R.id.swipeRefreshLayout)
|
|
|
- SwipeRefreshLayout swipeRefreshLayout;
|
|
|
- @BindView(R.id.recyclerView)
|
|
|
- RecyclerView recyclerView;
|
|
|
-
|
|
|
- protected BaseQuickAdapter<T, BaseViewHolder> baseQuickAdapter;
|
|
|
+ protected View check_net_ll;
|
|
|
+ @BindView(R.id.view_list)
|
|
|
+ protected XRecyclerView view_list;
|
|
|
|
|
|
protected BaseMessage lastMessage;//最后一个网络获取的结果
|
|
|
protected int page = 1;//页数
|
|
|
@@ -81,31 +78,27 @@ public abstract class BaseListFragment5<T> extends BaseFragment implements IRefr
|
|
|
private boolean loadMore;
|
|
|
|
|
|
public void initView() {
|
|
|
- ViewUtil.setVisibility(title, false);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+ protected View bottomLine;
|
|
|
+
|
|
|
public void initListener() {
|
|
|
- if (swipeRefreshLayout != null) {
|
|
|
- swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
- @Override
|
|
|
- public void onRefresh() {
|
|
|
- refreshData();
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- recyclerView.setLayoutManager(getLayoutManager());
|
|
|
- baseQuickAdapter = getAdapter();
|
|
|
- baseQuickAdapter.bindToRecyclerView(recyclerView);
|
|
|
- initLoadMoreListener();
|
|
|
- }
|
|
|
+ view_list.setLoadingListener(new XRecyclerView.LoadingListener() {
|
|
|
+ @Override
|
|
|
+ public void onRefresh() {
|
|
|
+ refreshData();
|
|
|
+ }
|
|
|
|
|
|
- protected void initLoadMoreListener() {
|
|
|
- baseQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
|
|
|
@Override
|
|
|
- public void onLoadMoreRequested() {
|
|
|
+ public void onLoadMore() {
|
|
|
loadMoreData();
|
|
|
+
|
|
|
}
|
|
|
- }, recyclerView);
|
|
|
+ });
|
|
|
+ bottomLine = ViewUtil.setBottomLine(view_list, getNoMoreCallBack());
|
|
|
+ view_list.setLayoutManager(getLayoutManager());
|
|
|
+ view_list.setAdapter(getAdapter());
|
|
|
}
|
|
|
|
|
|
protected RecyclerView.LayoutManager getLayoutManager() {
|
|
|
@@ -119,50 +112,54 @@ public abstract class BaseListFragment5<T> extends BaseFragment implements IRefr
|
|
|
page += 1;
|
|
|
initData();
|
|
|
} else {
|
|
|
- setNoMore(false);
|
|
|
+ view_list.setNoMore(true);
|
|
|
+ setNoMore(true);
|
|
|
}
|
|
|
} else {
|
|
|
- finishRefresh();
|
|
|
+ view_list.loadMoreComplete();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
//还有数据没有获取
|
|
|
protected boolean hasMore() {
|
|
|
- if(lastMessage != null && lastMessage.getTotal() > 0){
|
|
|
+ if (lastMessage != null && lastMessage.getTotal() > 0) {
|
|
|
return lastMessage.getTotal() > ListUtil.size(list);
|
|
|
}
|
|
|
return ListUtil.size(list) >= per_page * page;
|
|
|
}
|
|
|
|
|
|
- protected void setNoMore(boolean noMore) {
|
|
|
- if (noMore) {
|
|
|
- baseQuickAdapter.loadMoreEnd(isLoadMoreGoneWhenNoMore());
|
|
|
- }
|
|
|
- finishRefresh();
|
|
|
- }
|
|
|
|
|
|
- protected boolean isLoadMoreGoneWhenNoMore() {
|
|
|
- return false;
|
|
|
+ public void setNoMore(boolean noMore) {
|
|
|
+ this.noMore = noMore;
|
|
|
}
|
|
|
|
|
|
protected List<T> list = ListUtil.emptyList();
|
|
|
protected List<T> lastCacheList = ListUtil.emptyList();
|
|
|
|
|
|
+ //默认先获取缓存
|
|
|
+ protected boolean isFirstGetACache() {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
public void initData() {
|
|
|
final String urlKey = getKey(page, per_page);
|
|
|
- final boolean isFirstGetACache = urlKey != null;
|
|
|
- if (isFirstGetACache) {
|
|
|
+ if (isFirstGetACache()) {
|
|
|
//先尝试获取缓存数据
|
|
|
lastCacheList = DataUtil.getInstance().getCacheList(urlKey, getTClass());
|
|
|
loadList(lastCacheList);
|
|
|
}
|
|
|
- if (check_net_ll != null) {
|
|
|
- SysAppUtil.checkNet(new AbsObserver<Integer>() {
|
|
|
- @Override
|
|
|
- public void onNext(Integer result) {
|
|
|
- ViewUtil.setVisibility(check_net_ll, result != 0);
|
|
|
+ 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())
|
|
|
@@ -171,8 +168,8 @@ public abstract class BaseListFragment5<T> extends BaseFragment implements IRefr
|
|
|
public void onNext(BaseMessage baseMessage) {
|
|
|
lastMessage = baseMessage;
|
|
|
boolean isNewData = DataUtil.getInstance().isNewData(urlKey);
|
|
|
- if (isNewData || !isFirstGetACache) {
|
|
|
- if (isFirstGetACache)
|
|
|
+ if (isNewData || !isFirstGetACache()) {
|
|
|
+ if (isFirstGetACache())
|
|
|
ListUtil.removeAll(list, lastCacheList);
|
|
|
List<T> newList = baseMessage.getDatas(getTClass());
|
|
|
loadList(newList);
|
|
|
@@ -188,11 +185,24 @@ public abstract class BaseListFragment5<T> extends BaseFragment implements IRefr
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ public Action1<String> getNoMoreCallBack() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public void onResume() {
|
|
|
super.onResume();
|
|
|
if (refreshDataType() == REFRESH_ON_RESUME) {
|
|
|
- refreshData();
|
|
|
+// ViewUtil.refreshXrv(view_list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initDataOnVisibleToUser() {
|
|
|
+ Log.e("BaseListFragment5", this.getTag());
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ ViewUtil.refreshXrv(view_list);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -207,15 +217,15 @@ public abstract class BaseListFragment5<T> extends BaseFragment implements IRefr
|
|
|
|
|
|
public void clear() {
|
|
|
lastMessage = null;
|
|
|
- setNoMore(true);
|
|
|
+ setNoMore(false);
|
|
|
+ empty_view.setVisibility(View.INVISIBLE);
|
|
|
list.clear();
|
|
|
page = 1;
|
|
|
- initLoadMoreListener();
|
|
|
}
|
|
|
|
|
|
public void refreshData() {
|
|
|
clear();
|
|
|
- ViewUtil.notifyDataSetChanged(recyclerView);
|
|
|
+ view_list.getAdapter().notifyDataSetChanged();
|
|
|
initData();
|
|
|
}
|
|
|
|
|
|
@@ -228,28 +238,44 @@ public abstract class BaseListFragment5<T> extends BaseFragment implements IRefr
|
|
|
}
|
|
|
|
|
|
public void notifyDataSetChanged() {
|
|
|
- if (recyclerView == null)
|
|
|
- recyclerView = findViewById(R.id.recyclerView);
|
|
|
+ if (view_list == null)
|
|
|
+ view_list = findViewById(R.id.view_list);
|
|
|
+ if (empty_view == null)
|
|
|
+ empty_view = findViewById(R.id.empty_view);
|
|
|
loadMore = false;
|
|
|
- ViewUtil.notifyDataSetChanged(recyclerView);
|
|
|
- finishRefresh();
|
|
|
+ updateEmptyView();
|
|
|
+ if (page == 1) {
|
|
|
+ view_list.refreshComplete();
|
|
|
+ } else {
|
|
|
+ view_list.loadMoreComplete();
|
|
|
+ }
|
|
|
+ view_list.getAdapter().notifyDataSetChanged();
|
|
|
+ notifyData();
|
|
|
+ view_list.getFootView().setVisibility(view_list.getAdapter().getItemCount() == 0 ? View.GONE : View.VISIBLE);
|
|
|
}
|
|
|
|
|
|
- protected void finishRefresh() {
|
|
|
- if (swipeRefreshLayout != null)
|
|
|
- swipeRefreshLayout.setRefreshing(false);
|
|
|
- baseQuickAdapter.loadMoreComplete();
|
|
|
+ protected void updateEmptyView() {
|
|
|
+ CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onDestroyView() {
|
|
|
super.onDestroyView();
|
|
|
- if (recyclerView != null) {
|
|
|
- recyclerView = null;
|
|
|
+ // 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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- protected abstract BaseQuickAdapter<T, BaseViewHolder> getAdapter();
|
|
|
+
|
|
|
+ private boolean noMore = false;
|
|
|
+
|
|
|
+ public boolean isNoMore() {
|
|
|
+ return noMore;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected abstract RecyclerView.Adapter getAdapter();
|
|
|
|
|
|
protected abstract String getKey(int page, int per_page);
|
|
|
|
|
|
@@ -257,4 +283,17 @@ public abstract class BaseListFragment5<T> extends BaseFragment implements IRefr
|
|
|
|
|
|
protected abstract Class<T> getTClass();
|
|
|
|
|
|
+ //刷新数据的回调,子类可以实现自己的需求
|
|
|
+ public void notifyData() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doNextAction(Object action) {
|
|
|
+ if (action instanceof String) {
|
|
|
+ if (TextUtils.equals((String) action, IRefresh.class.getSimpleName())) {
|
|
|
+ refreshData();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|