|
|
@@ -58,10 +58,14 @@ public abstract class BaseListActivity<T> extends BaseActivity {
|
|
|
getType();
|
|
|
initView();
|
|
|
initListener();
|
|
|
+ if(!refreshDataAfterOnResume())
|
|
|
+ view_list.refresh();
|
|
|
}
|
|
|
|
|
|
protected int page = 1;//页数
|
|
|
protected int per_page = DataUtil.PER_PAGE;
|
|
|
+ protected BaseMessage lastMessage;//最后一个网络获取的结果
|
|
|
+ private boolean noMore = false;
|
|
|
public void initView() {
|
|
|
// title.setVisibility(View.GONE);
|
|
|
}
|
|
|
@@ -80,6 +84,7 @@ public abstract class BaseListActivity<T> extends BaseActivity {
|
|
|
initData();
|
|
|
}else {
|
|
|
view_list.setNoMore(true);
|
|
|
+ setNoMore(true);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
@@ -91,12 +96,18 @@ public abstract class BaseListActivity<T> extends BaseActivity {
|
|
|
|
|
|
|
|
|
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);
|
|
|
- //先尝试获取缓存数据
|
|
|
- List<T> newList = DataUtil.getInstance().getCacheList(urlKey, getTClass());
|
|
|
- loadList(newList);
|
|
|
- empty_view.setVisibility(View.INVISIBLE);
|
|
|
+ if(isFirstGetACache() && page == 1) {
|
|
|
+ //先尝试获取缓存数据
|
|
|
+ lastCacheList = DataUtil.getInstance().getCacheList(urlKey, getTClass());
|
|
|
+ loadList(lastCacheList);
|
|
|
+ }
|
|
|
SysAppUtil.checkNet(new Action1<Integer>() {
|
|
|
@Override
|
|
|
public void call(Integer result) {
|
|
|
@@ -115,8 +126,10 @@ public abstract class BaseListActivity<T> extends BaseActivity {
|
|
|
.subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
|
|
|
@Override
|
|
|
public void onNext(BaseMessage baseMessage) {
|
|
|
+ lastMessage = baseMessage;
|
|
|
boolean isNewData = DataUtil.getInstance().isNewData(urlKey);
|
|
|
- if(isNewData) {
|
|
|
+ if(isNewData || !isFirstGetACache()) {
|
|
|
+ list.removeAll(lastCacheList);
|
|
|
List<T> newList = baseMessage.getDatas(getTClass());
|
|
|
loadList(newList);
|
|
|
}else {
|
|
|
@@ -132,12 +145,17 @@ public abstract class BaseListActivity<T> extends BaseActivity {
|
|
|
}
|
|
|
|
|
|
public void refreshData(){
|
|
|
+ lastMessage = null;
|
|
|
+ setNoMore(false);
|
|
|
+ empty_view.setVisibility(View.INVISIBLE);
|
|
|
list.clear();
|
|
|
view_list.getAdapter().notifyDataSetChanged();
|
|
|
page = 1;
|
|
|
initData();
|
|
|
}
|
|
|
private void loadList(List<T> newList){
|
|
|
+ if(!isFirstGetACache() && page == 1)
|
|
|
+ list.clear();
|
|
|
ListUtil.addAll(list, newList);
|
|
|
notifyDataSetChanged();
|
|
|
}
|
|
|
@@ -152,6 +170,21 @@ public abstract class BaseListActivity<T> extends BaseActivity {
|
|
|
view_list.getAdapter().notifyDataSetChanged();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 每次都在onResume方法中进行更新数据
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean refreshDataAfterOnResume() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ if(refreshDataAfterOnResume()){
|
|
|
+ view_list.refresh();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
protected abstract RecyclerView.Adapter getAdapter();
|
|
|
|
|
|
protected abstract String getKey(int page, int per_page);
|
|
|
@@ -161,4 +194,7 @@ public abstract class BaseListActivity<T> extends BaseActivity {
|
|
|
protected abstract Class<T> getTClass();
|
|
|
|
|
|
protected abstract int getType();
|
|
|
+ public void setNoMore(boolean noMore) {
|
|
|
+ this.noMore = noMore;
|
|
|
+ }
|
|
|
}
|