|
|
@@ -1,18 +1,19 @@
|
|
|
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.view.View;
|
|
|
|
|
|
-import com.jcodecraeer.xrecyclerview.XRecyclerView;
|
|
|
+import com.chad.library.adapter.base.BaseQuickAdapter;
|
|
|
+import com.chad.library.adapter.base.BaseViewHolder;
|
|
|
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;
|
|
|
@@ -27,18 +28,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/22.
|
|
|
+ * Created by realicing on 2018/11/28.
|
|
|
* realicing@sina.com
|
|
|
* 可设置每次请求多个接口
|
|
|
- * 自己可以进行刷新与加载更多,使用com.jcodecraeer.xrecyclerview.XRecyclerView来实现,子类提供网络接口相关
|
|
|
+ * android.support.v4.widget.SwipeRefreshLayout与RecyclerView来实现上拉刷新与下拉加载更多,子类提供网络接口相关
|
|
|
*/
|
|
|
-public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements IRefresh, ILoadMore {
|
|
|
+public abstract class BaseListFragment5x<I, T, T2> extends BaseFragment implements IRefresh, ILoadMore {
|
|
|
@Override
|
|
|
public int getLayoutId() {
|
|
|
- return R.layout.net_empty_xrecycler;
|
|
|
+ return R.layout.common_title_net_srl_rv;
|
|
|
}
|
|
|
|
|
|
protected Activity activity;
|
|
|
@@ -50,7 +50,7 @@ public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements I
|
|
|
initListener();
|
|
|
switch (refreshDataType()) {
|
|
|
case REFRESH_ON_CREATE:
|
|
|
- ViewUtil.refreshXrv(view_list);
|
|
|
+ refreshData();
|
|
|
break;
|
|
|
case REFRESH_ON_RESUME:
|
|
|
break;
|
|
|
@@ -65,13 +65,15 @@ public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements I
|
|
|
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;
|
|
|
+ View title;
|
|
|
@BindView(R.id.check_net_ll)
|
|
|
- protected View check_net_ll;
|
|
|
- @BindView(R.id.view_list)
|
|
|
- protected XRecyclerView view_list;
|
|
|
+ View check_net_ll;
|
|
|
+ @BindView(R.id.swipeRefreshLayout)
|
|
|
+ SwipeRefreshLayout swipeRefreshLayout;
|
|
|
+ @BindView(R.id.recyclerView)
|
|
|
+ RecyclerView recyclerView;
|
|
|
+
|
|
|
+ protected BaseQuickAdapter<I, BaseViewHolder> baseQuickAdapter;
|
|
|
|
|
|
protected BaseMessage lastMessage;//最后一个网络获取的结果
|
|
|
protected BaseMessage lastMessage2;//最后一个网络获取的结果
|
|
|
@@ -80,27 +82,35 @@ public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements I
|
|
|
private boolean loadMore;
|
|
|
|
|
|
public void initView() {
|
|
|
- title.setVisibility(View.GONE);
|
|
|
+ ViewUtil.setVisibility(title, false);
|
|
|
}
|
|
|
|
|
|
- protected View bottomLine;
|
|
|
-
|
|
|
public void initListener() {
|
|
|
- view_list.setLoadingListener(new XRecyclerView.LoadingListener() {
|
|
|
- @Override
|
|
|
- public void onRefresh() {
|
|
|
- refreshData();
|
|
|
- }
|
|
|
+ if (swipeRefreshLayout != null) {
|
|
|
+ swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
+ @Override
|
|
|
+ public void onRefresh() {
|
|
|
+ refreshData();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ recyclerView.setLayoutManager(getLayoutManager());
|
|
|
+ baseQuickAdapter = getAdapter();
|
|
|
+ baseQuickAdapter.bindToRecyclerView(recyclerView);
|
|
|
+ initLoadMoreListener();
|
|
|
+ }
|
|
|
|
|
|
+ protected void initLoadMoreListener() {
|
|
|
+ baseQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
|
|
|
@Override
|
|
|
- public void onLoadMore() {
|
|
|
+ public void onLoadMoreRequested() {
|
|
|
loadMoreData();
|
|
|
-
|
|
|
}
|
|
|
- });
|
|
|
- bottomLine = ViewUtil.setBottomLine(view_list, getNoMoreCallBack());
|
|
|
- view_list.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
|
|
|
- view_list.setAdapter(getAdapter());
|
|
|
+ }, recyclerView);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected RecyclerView.LayoutManager getLayoutManager() {
|
|
|
+ return new LinearLayoutManager(SheepApp.getInstance());
|
|
|
}
|
|
|
|
|
|
public void loadMoreData() {
|
|
|
@@ -110,16 +120,22 @@ public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements I
|
|
|
page += 1;
|
|
|
initData();
|
|
|
} else {
|
|
|
- view_list.setNoMore(true);
|
|
|
- setNoMore(true);
|
|
|
+ setNoMore(false);
|
|
|
}
|
|
|
} else {
|
|
|
- view_list.loadMoreComplete();
|
|
|
+ finishRefresh();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void setNoMore(boolean noMore) {
|
|
|
+ if (noMore) {
|
|
|
+ baseQuickAdapter.loadMoreEnd(isLoadMoreGoneWhenNoMore());
|
|
|
}
|
|
|
+ finishRefresh();
|
|
|
}
|
|
|
|
|
|
- public void setNoMore(boolean noMore) {
|
|
|
- this.noMore = noMore;
|
|
|
+ protected boolean isLoadMoreGoneWhenNoMore() {
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
protected List<T> list = ListUtil.emptyList();
|
|
|
@@ -145,18 +161,14 @@ public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements I
|
|
|
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);
|
|
|
+ if (check_net_ll != null) {
|
|
|
+ SysAppUtil.checkNet(new AbsObserver<Integer>() {
|
|
|
+ @Override
|
|
|
+ public void onNext(Integer result) {
|
|
|
+ ViewUtil.setVisibility(check_net_ll, result != 0);
|
|
|
}
|
|
|
- }
|
|
|
- });
|
|
|
+ });
|
|
|
+ }
|
|
|
getApi(SheepApp.getInstance().getNetComponent().getApiService())
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
@@ -205,16 +217,11 @@ public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements I
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- public Action1<String> getNoMoreCallBack() {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
@Override
|
|
|
public void onResume() {
|
|
|
super.onResume();
|
|
|
if (refreshDataType() == REFRESH_ON_RESUME) {
|
|
|
- ViewUtil.refreshXrv(view_list);
|
|
|
+ refreshData();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -230,16 +237,16 @@ public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements I
|
|
|
public void clear() {
|
|
|
lastMessage = null;
|
|
|
lastMessage2 = null;
|
|
|
- setNoMore(false);
|
|
|
- empty_view.setVisibility(View.INVISIBLE);
|
|
|
+ setNoMore(true);
|
|
|
list.clear();
|
|
|
list2.clear();
|
|
|
page = 1;
|
|
|
+ initLoadMoreListener();
|
|
|
}
|
|
|
|
|
|
public void refreshData() {
|
|
|
clear();
|
|
|
- ViewUtil.notifyDataSetChanged(view_list);
|
|
|
+ ViewUtil.notifyDataSetChanged(recyclerView);
|
|
|
initData();
|
|
|
}
|
|
|
|
|
|
@@ -250,6 +257,7 @@ public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements I
|
|
|
newList = addList;
|
|
|
notifyDataSetChanged(1);
|
|
|
}
|
|
|
+
|
|
|
protected List<T2> newList2;
|
|
|
|
|
|
protected void loadList2(List<T2> addList) {
|
|
|
@@ -259,56 +267,43 @@ public abstract class BaseListFragment2x<T,T2> extends BaseFragment implements I
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
+ if (recyclerView == null)
|
|
|
+ recyclerView = findViewById(R.id.recyclerView);
|
|
|
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);
|
|
|
+ ViewUtil.notifyDataSetChanged(recyclerView);
|
|
|
+ finishRefresh();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void finishRefresh() {
|
|
|
+ if (swipeRefreshLayout != null)
|
|
|
+ swipeRefreshLayout.setRefreshing(false);
|
|
|
+ baseQuickAdapter.loadMoreComplete();
|
|
|
}
|
|
|
|
|
|
protected void updateEmptyView() {
|
|
|
- CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty() && list2.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;
|
|
|
+ if (recyclerView != null) {
|
|
|
+ recyclerView = null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- private boolean noMore = false;
|
|
|
-
|
|
|
- public boolean isNoMore() {
|
|
|
- return noMore;
|
|
|
- }
|
|
|
-
|
|
|
- protected abstract RecyclerView.Adapter getAdapter();
|
|
|
+ protected abstract BaseQuickAdapter<I, BaseViewHolder> 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){
|
|
|
|
|
|
- }
|
|
|
+ protected abstract Class<T2> getT2Class();
|
|
|
}
|