|
@@ -0,0 +1,213 @@
|
|
|
|
|
+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.chad.library.adapter.base.BaseQuickAdapter;
|
|
|
|
|
+import com.chad.library.adapter.base.BaseViewHolder;
|
|
|
|
|
+import com.sheep.gamegroup.absBase.AbsObserver;
|
|
|
|
|
+import com.sheep.gamegroup.absBase.ApiRefresh;
|
|
|
|
|
+import com.sheep.gamegroup.absBase.ILoadMore;
|
|
|
|
|
+import com.sheep.gamegroup.absBase.IRefresh;
|
|
|
|
|
+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.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+import butterknife.BindView;
|
|
|
|
|
+import rx.functions.Action1;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Created by realicing on 2018/11/28.
|
|
|
|
|
+ * realicing@sina.com
|
|
|
|
|
+ * 可设置每次请求多个接口
|
|
|
|
|
+ * android.support.v4.widget.SwipeRefreshLayout与RecyclerView来实现上拉刷新与下拉加载更多,子类提供网络接口相关
|
|
|
|
|
+ */
|
|
|
|
|
+public abstract class BaseListFragment6<T> extends BaseFragment implements IRefresh, ILoadMore {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int getLayoutId() {
|
|
|
|
|
+ return R.layout.common_title_net_srl_rv;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected Activity activity;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onViewCreated() {
|
|
|
|
|
+ activity = getActivity();
|
|
|
|
|
+ initView();
|
|
|
|
|
+ initListener();
|
|
|
|
|
+ switch (refreshDataType()) {
|
|
|
|
|
+ case REFRESH_ON_CREATE:
|
|
|
|
|
+ refreshData();
|
|
|
|
|
+ break;
|
|
|
|
|
+ case REFRESH_ON_RESUME:
|
|
|
|
|
+ break;
|
|
|
|
|
+ case REFRESH_ON_YOURSELF:
|
|
|
|
|
+ default:
|
|
|
|
|
+ notifyDataSetChanged();
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ private List<ApiRefresh<? extends Object>> apiRefreshList = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ 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.check_net_ll)
|
|
|
|
|
+ View check_net_ll;
|
|
|
|
|
+ @BindView(R.id.swipeRefreshLayout)
|
|
|
|
|
+ SwipeRefreshLayout swipeRefreshLayout;
|
|
|
|
|
+ @BindView(R.id.recyclerView)
|
|
|
|
|
+ RecyclerView recyclerView;
|
|
|
|
|
+
|
|
|
|
|
+ protected BaseQuickAdapter<T, BaseViewHolder> baseQuickAdapter;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public void initView() {
|
|
|
|
|
+ ViewUtil.setVisibility(title, false);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected void initLoadMoreListener() {
|
|
|
|
|
+ baseQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onLoadMoreRequested() {
|
|
|
|
|
+ loadMoreData();
|
|
|
|
|
+ }
|
|
|
|
|
+ }, recyclerView);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected RecyclerView.LayoutManager getLayoutManager() {
|
|
|
|
|
+ return new LinearLayoutManager(SheepApp.getInstance());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void loadMoreData() {
|
|
|
|
|
+ ListUtil.forEach(apiRefreshList, new Action1<ApiRefresh<? extends Object>>() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void call(ApiRefresh<?> apiRefresh) {
|
|
|
|
|
+ if(apiRefresh.loadMoreData()){
|
|
|
|
|
+ finishRefresh();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setNoMore(boolean noMore) {
|
|
|
|
|
+ if (noMore) {
|
|
|
|
|
+ baseQuickAdapter.loadMoreEnd(isLoadMoreGoneWhenNoMore());
|
|
|
|
|
+ }
|
|
|
|
|
+ finishRefresh();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected boolean isLoadMoreGoneWhenNoMore() {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onResume() {
|
|
|
|
|
+ super.onResume();
|
|
|
|
|
+ if (refreshDataType() == REFRESH_ON_RESUME) {
|
|
|
|
|
+ refreshData();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 在哪里初始化数据:0 onViewCreated后, 1 onResume后, -1 不初始化
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public int refreshDataType() {
|
|
|
|
|
+ return REFRESH_ON_CREATE;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void refreshData() {
|
|
|
|
|
+ checkMetLl();
|
|
|
|
|
+ clear();
|
|
|
|
|
+ ListUtil.forEach(apiRefreshList, new Action1<ApiRefresh<? extends Object>>() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void call(ApiRefresh<?> apiRefresh) {
|
|
|
|
|
+ apiRefresh.clear();
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ ViewUtil.notifyDataSetChanged(recyclerView);
|
|
|
|
|
+ ListUtil.forEach(apiRefreshList, new Action1<ApiRefresh<? extends Object>>() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void call(ApiRefresh<?> apiRefresh) {
|
|
|
|
|
+ apiRefresh.initData();
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ protected void clear(){
|
|
|
|
|
+ setNoMore(true);
|
|
|
|
|
+ initLoadMoreListener();
|
|
|
|
|
+ }
|
|
|
|
|
+ public void checkMetLl(){
|
|
|
|
|
+ if (check_net_ll != null) {
|
|
|
|
|
+ SysAppUtil.checkNet(new AbsObserver<Integer>() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onNext(Integer result) {
|
|
|
|
|
+ ViewUtil.setVisibility(check_net_ll, result != 0);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void notifyDataSetChanged() {
|
|
|
|
|
+ if (recyclerView == null)
|
|
|
|
|
+ recyclerView = findViewById(R.id.recyclerView);
|
|
|
|
|
+ ListUtil.forEach(apiRefreshList, new Action1<ApiRefresh<? extends Object>>() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void call(ApiRefresh<?> apiRefresh) {
|
|
|
|
|
+ apiRefresh.setLoadMore(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ updateEmptyView();
|
|
|
|
|
+ ViewUtil.notifyDataSetChanged(recyclerView);
|
|
|
|
|
+ finishRefresh();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected void finishRefresh() {
|
|
|
|
|
+ if (swipeRefreshLayout != null)
|
|
|
|
|
+ swipeRefreshLayout.setRefreshing(false);
|
|
|
|
|
+ baseQuickAdapter.loadMoreComplete();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected void updateEmptyView() {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onDestroyView() {
|
|
|
|
|
+ super.onDestroyView();
|
|
|
|
|
+ if (recyclerView != null) {
|
|
|
|
|
+ recyclerView = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected abstract BaseQuickAdapter<T, BaseViewHolder> getAdapter();
|
|
|
|
|
+
|
|
|
|
|
+}
|