|
|
@@ -1,33 +1,23 @@
|
|
|
package com.sheep.gamegroup.view.fragment;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
-import android.os.Bundle;
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
-import android.util.Log;
|
|
|
-import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
-import android.view.ViewGroup;
|
|
|
|
|
|
-import com.scwang.smartrefresh.layout.SmartRefreshLayout;
|
|
|
-import com.scwang.smartrefresh.layout.api.RefreshLayout;
|
|
|
-import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
|
|
|
-import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
|
|
|
+import com.jcodecraeer.xrecyclerview.XRecyclerView;
|
|
|
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.RefreshUtil;
|
|
|
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 org.greenrobot.eventbus.EventBus;
|
|
|
-import org.greenrobot.eventbus.Subscribe;
|
|
|
-
|
|
|
import java.util.List;
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
@@ -37,139 +27,89 @@ import rx.functions.Action1;
|
|
|
import rx.schedulers.Schedulers;
|
|
|
|
|
|
/**
|
|
|
- * Created by ljy on 2018/9/13.
|
|
|
+ * Created by realicing on 2018/9/3.
|
|
|
+ * realicing@sina.com
|
|
|
+ * 自己可以进行刷新与加载更多,使用com.jcodecraeer.xrecyclerview.XRecyclerView来实现,子类提供网络接口相关
|
|
|
*/
|
|
|
-
|
|
|
public abstract class BaseListFragment2<T> extends BaseFragment {
|
|
|
@Override
|
|
|
public int getLayoutId() {
|
|
|
- return R.layout.fgt_playgame_son_refresh_layout;
|
|
|
+ return R.layout.net_empty_xrecycler;
|
|
|
}
|
|
|
|
|
|
protected Activity activity;
|
|
|
+
|
|
|
@Override
|
|
|
public void onViewCreated() {
|
|
|
activity = getActivity();
|
|
|
initView();
|
|
|
initListener();
|
|
|
switch (refreshDataType()) {
|
|
|
- case 0:
|
|
|
-// view_list.refresh();
|
|
|
- case 1:
|
|
|
+ case REFRESH_ON_CREATE:
|
|
|
+ view_list.refresh();
|
|
|
+ case REFRESH_ON_RESUME:
|
|
|
break;
|
|
|
+ case REFRESH_ON_YOURSELF:
|
|
|
default:
|
|
|
notifyDataSetChanged();
|
|
|
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.refreshLayout)
|
|
|
- protected SmartRefreshLayout refreshLayout;
|
|
|
@BindView(R.id.view_list)
|
|
|
- protected RecyclerView view_list;
|
|
|
+ protected XRecyclerView view_list;
|
|
|
|
|
|
protected BaseMessage lastMessage;//最后一个网络获取的结果
|
|
|
protected int page = 1;//页数
|
|
|
protected int per_page = DataUtil.PER_PAGE;
|
|
|
private boolean loadMore;
|
|
|
|
|
|
- @Subscribe
|
|
|
public void initView() {
|
|
|
title.setVisibility(View.GONE);
|
|
|
- try {
|
|
|
- EventBus.getDefault().register(this);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- RefreshUtil.newInstance().publicParameterRefresh(refreshLayout, activity);
|
|
|
-// refreshLayout.setEnableLoadMoreWhenContentNotFull(true);//内容不满屏幕的时候也开启加载更多
|
|
|
-// refreshLayout.setFooterTriggerRate(1);
|
|
|
}
|
|
|
+
|
|
|
protected View bottomLine;
|
|
|
+
|
|
|
public void initListener() {
|
|
|
- refreshLayout.setOnRefreshListener(new OnRefreshListener() {
|
|
|
+ view_list.setLoadingListener(new XRecyclerView.LoadingListener() {
|
|
|
@Override
|
|
|
- public void onRefresh(RefreshLayout refreshlayout) {
|
|
|
- refreshlayout.finishRefresh(100/*,false*/);//传入false表示刷新失败
|
|
|
+ public void onRefresh() {
|
|
|
refreshData();
|
|
|
}
|
|
|
- });
|
|
|
- refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
|
|
|
- @Override
|
|
|
- public void onLoadMore(final RefreshLayout refreshlayout) {
|
|
|
- if(ListUtil.size(list) >= per_page*page){
|
|
|
- page += 1;
|
|
|
- initData();
|
|
|
- }else {
|
|
|
- refreshlayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
|
|
|
- setNoMore(true);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- });
|
|
|
- view_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
- boolean isSlidingToLast;
|
|
|
- @Override
|
|
|
- public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
|
- super.onScrollStateChanged(recyclerView, newState);
|
|
|
-
|
|
|
- RecyclerView.LayoutManager layoutManager = view_list.getLayoutManager();
|
|
|
-
|
|
|
- if(layoutManager instanceof LinearLayoutManager){
|
|
|
- LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
|
|
|
- //获取最后一个view 位置
|
|
|
- int lastItemPosition = linearLayoutManager.findLastVisibleItemPosition();
|
|
|
-
|
|
|
- View childView = recyclerView.getChildAt(list.size() - 1);
|
|
|
- //底部bottom
|
|
|
-// int bottom = childView.getBottom();
|
|
|
- //正常来说RecycleView的顶部坐标应该是0,但是严格来考虑,当RecycleView设置了paddingTop时,所有子view的绘制将以paddingTop的位置为起始位置,所以实际的顶部应该是paddingTop的高度的数值.
|
|
|
- int topEdge=recyclerView.getPaddingBottom();
|
|
|
-
|
|
|
- //获取最后一个完全显示的item的position
|
|
|
- int lastVisibleItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();
|
|
|
- int totalCount = linearLayoutManager.getItemCount();
|
|
|
- Log.e("lastvisi_item_-----", lastVisibleItem+","+totalCount);
|
|
|
-
|
|
|
- if(newState == RecyclerView.SCROLL_STATE_IDLE ){//滑动到了底部
|
|
|
- if(ListUtil.size(list) >= per_page*page){
|
|
|
-
|
|
|
- refreshLayout.autoLoadMore();
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
@Override
|
|
|
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
|
- super.onScrolled(recyclerView, dx, dy);
|
|
|
- Log.e("dx-----", dx+","+dy);
|
|
|
- if (dy > 0) {
|
|
|
- //大于0表示正在向下滚动
|
|
|
- isSlidingToLast = true;
|
|
|
- } else {
|
|
|
- //小于等于0表示停止或向上滚动
|
|
|
- isSlidingToLast = false;
|
|
|
- }
|
|
|
+ public void onLoadMore() {
|
|
|
+ loadMoreData();
|
|
|
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
-// bottomLine = ViewUtil.setBottomLine(refreshLayout, getNoMoreCallBack());
|
|
|
-
|
|
|
- LinearLayoutManager hotManager = new LinearLayoutManager(SheepApp.getInstance(), LinearLayoutManager.VERTICAL, false);
|
|
|
- view_list.setHasFixedSize(true);
|
|
|
- view_list.setNestedScrollingEnabled(false);
|
|
|
- view_list.setLayoutManager(hotManager);
|
|
|
+ bottomLine = ViewUtil.setBottomLine(view_list, getNoMoreCallBack());
|
|
|
+ view_list.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
|
|
|
view_list.setAdapter(getAdapter());
|
|
|
+ }
|
|
|
|
|
|
+ protected 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) {
|
|
|
@@ -177,18 +117,16 @@ public abstract class BaseListFragment2<T> extends BaseFragment {
|
|
|
}
|
|
|
|
|
|
protected List<T> list = ListUtil.emptyList();
|
|
|
- protected List<T> listCacheOneList = ListUtil.emptyList();
|
|
|
protected List<T> lastCacheList = ListUtil.emptyList();
|
|
|
+
|
|
|
//默认先获取缓存
|
|
|
- protected boolean isFirstGetACache(){
|
|
|
+ protected boolean isFirstGetACache() {
|
|
|
return true;
|
|
|
}
|
|
|
- public void setPerpage(int per_page){
|
|
|
- this.per_page = per_page;
|
|
|
- }
|
|
|
- public synchronized void initData() {
|
|
|
+
|
|
|
+ public void initData() {
|
|
|
final String urlKey = getKey(page, per_page);
|
|
|
- if(isFirstGetACache()) {
|
|
|
+ if (isFirstGetACache()) {
|
|
|
//先尝试获取缓存数据
|
|
|
lastCacheList = DataUtil.getInstance().getCacheList(urlKey, getTClass());
|
|
|
loadList(lastCacheList);
|
|
|
@@ -196,24 +134,15 @@ public abstract class BaseListFragment2<T> extends BaseFragment {
|
|
|
SysAppUtil.checkNet(new Action1<Integer>() {
|
|
|
@Override
|
|
|
public void call(Integer result) {
|
|
|
- if(result != 0){//无网络
|
|
|
- if(check_net_ll != null)
|
|
|
+ if (result != 0) {//无网络
|
|
|
+ if (check_net_ll != null)
|
|
|
check_net_ll.setVisibility(View.VISIBLE);
|
|
|
} else {
|
|
|
- if(check_net_ll != null)
|
|
|
+ if (check_net_ll != null)
|
|
|
check_net_ll.setVisibility(View.GONE);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
- getData(urlKey);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取数据
|
|
|
- * @return
|
|
|
- */
|
|
|
- public void getData(final String urlKey){
|
|
|
-
|
|
|
getApi(SheepApp.getInstance().getNetComponent().getApiService())
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
@@ -222,11 +151,12 @@ public abstract class BaseListFragment2<T> extends BaseFragment {
|
|
|
public void onNext(BaseMessage baseMessage) {
|
|
|
lastMessage = baseMessage;
|
|
|
boolean isNewData = DataUtil.getInstance().isNewData(urlKey);
|
|
|
- if(isNewData || !isFirstGetACache()) {
|
|
|
- ListUtil.removeAll(list, lastCacheList);
|
|
|
+ if (isNewData || !isFirstGetACache()) {
|
|
|
+ if (isFirstGetACache())
|
|
|
+ ListUtil.removeAll(list, lastCacheList);
|
|
|
List<T> newList = baseMessage.getDatas(getTClass());
|
|
|
loadList(newList);
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
notifyDataSetChanged();
|
|
|
}
|
|
|
}
|
|
|
@@ -237,7 +167,8 @@ public abstract class BaseListFragment2<T> extends BaseFragment {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
- public Action1<String> getNoMoreCallBack(){
|
|
|
+
|
|
|
+ public Action1<String> getNoMoreCallBack() {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@@ -245,92 +176,90 @@ public abstract class BaseListFragment2<T> extends BaseFragment {
|
|
|
@Override
|
|
|
public void onResume() {
|
|
|
super.onResume();
|
|
|
- if(refreshDataType() == 1){
|
|
|
- refreshData();
|
|
|
+ if (refreshDataType() == REFRESH_ON_RESUME) {
|
|
|
+ view_list.refresh();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在哪里初始化数据:0 onViewCreated后, 1 onResume后, -1 不初始化
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
public int refreshDataType() {
|
|
|
- return 0;
|
|
|
+ return REFRESH_ON_CREATE;
|
|
|
}
|
|
|
|
|
|
- public void refreshData(){
|
|
|
+ public void clear() {
|
|
|
lastMessage = null;
|
|
|
setNoMore(false);
|
|
|
empty_view.setVisibility(View.INVISIBLE);
|
|
|
list.clear();
|
|
|
- view_list.getAdapter().notifyDataSetChanged();
|
|
|
page = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void refreshData() {
|
|
|
+ clear();
|
|
|
+ view_list.getAdapter().notifyDataSetChanged();
|
|
|
initData();
|
|
|
}
|
|
|
- private void loadList(List<T> newList){
|
|
|
- if(!isFirstGetACache() && page == 1)
|
|
|
- list.clear();
|
|
|
- ListUtil.addAll(list, newList);
|
|
|
+
|
|
|
+ protected List<T> newList;
|
|
|
+
|
|
|
+ protected void loadList(List<T> addList) {
|
|
|
+ ListUtil.addAllItem(list, addList);
|
|
|
+ newList = addList;
|
|
|
notifyDataSetChanged();
|
|
|
}
|
|
|
- public void notifyDataSetChanged(){
|
|
|
- if(refreshLayout == null || view_list == null || empty_view == null){
|
|
|
- return;
|
|
|
- }
|
|
|
- loadMore = false;
|
|
|
- if(list.isEmpty()){
|
|
|
- refreshLayout.setVisibility(View.GONE);
|
|
|
- }else {
|
|
|
|
|
|
- refreshLayout.setVisibility(View.VISIBLE);
|
|
|
- }
|
|
|
- CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty());
|
|
|
- if(page == 1){
|
|
|
- refreshLayout.finishRefresh(100/*,false*/);//传入false表示加载失败
|
|
|
+ public void notifyDataSetChanged() {
|
|
|
+ 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 {
|
|
|
- refreshLayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
|
|
|
+ view_list.loadMoreComplete();
|
|
|
}
|
|
|
view_list.getAdapter().notifyDataSetChanged();
|
|
|
notifyData();
|
|
|
+ view_list.getFootView().setVisibility(view_list.getAdapter().getItemCount() == 0 ? View.GONE : View.VISIBLE);
|
|
|
}
|
|
|
- @Override
|
|
|
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
- // TODO: inflate a fragment view
|
|
|
- View rootView = super.onCreateView(inflater, container, savedInstanceState);
|
|
|
- return rootView;
|
|
|
+
|
|
|
+ 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
|
|
|
+ if (view_list != null) {
|
|
|
+ view_list.destroy(); // this will totally release XR's memory
|
|
|
view_list = null;
|
|
|
}
|
|
|
- EventBus.getDefault().unregister(this);
|
|
|
- }
|
|
|
- public int getDataCount() {
|
|
|
- return list.size();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public void setNoFresh(){
|
|
|
-// if(view_list != null)
|
|
|
-// view_list.setPullRefreshEnabled(false);
|
|
|
- }
|
|
|
|
|
|
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 Observable<BaseMessage> getApi(ApiService apiService);
|
|
|
+
|
|
|
protected abstract Class<T> getTClass();
|
|
|
|
|
|
- public abstract void notifyData();
|
|
|
+ //刷新数据的回调,子类可以实现自己的需求
|
|
|
+ public void notifyData(){
|
|
|
+
|
|
|
+ }
|
|
|
}
|