|
|
@@ -40,6 +40,7 @@ public class FgtFind extends BaseCompatFragment {
|
|
|
setContentView(R.layout.fgt_find);
|
|
|
activity = getActivity();
|
|
|
ButterKnife.bind(this, mContentView);
|
|
|
+ initView();
|
|
|
initData();
|
|
|
}
|
|
|
|
|
|
@@ -49,44 +50,9 @@ public class FgtFind extends BaseCompatFragment {
|
|
|
ViewPager pager;
|
|
|
|
|
|
private GiftpackListAdapter mAdapter;
|
|
|
- private void initData(){
|
|
|
- SheepApp.getInstance().getNetComponent().getApiService().getFindTagList()
|
|
|
- .subscribeOn(Schedulers.io())
|
|
|
- .observeOn(AndroidSchedulers.mainThread())
|
|
|
- .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
|
|
|
- @Override
|
|
|
- public void onNext(BaseMessage baseMessage) {
|
|
|
- List<FindTag> newList = baseMessage.getDatas(FindTag.class);
|
|
|
- loadList(newList);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onError(BaseMessage baseMessage) {
|
|
|
- //无数据时尝试获取缓存数据
|
|
|
- List<FindTag> newList = DataUtil.getInstance().getCacheList(ApiKey.articles_tags, FindTag.class);
|
|
|
- if(loadList(newList))
|
|
|
- G.showToast(baseMessage);
|
|
|
- }
|
|
|
- //没有添加数据返回true
|
|
|
- private boolean loadList(List<FindTag> newList){
|
|
|
- if(!ListUtil.isEmpty(newList)){
|
|
|
- list.clear();
|
|
|
- ListUtil.addAll(list, newList);
|
|
|
- loadData();
|
|
|
- return false;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- private List<FindTag> list = ListUtil.emptyList();
|
|
|
- private void loadData() {
|
|
|
+ private void initView() {
|
|
|
mAdapter = new GiftpackListAdapter(getFragmentManager(), getContext());
|
|
|
mAdapter.add(FgtFindChild.newInstance(0), "全部");
|
|
|
- for (FindTag findLabel : list) {
|
|
|
- mAdapter.add(FgtFindChild.newInstance(findLabel.getId()), findLabel.getName());
|
|
|
- }
|
|
|
pager.setAdapter(mAdapter);
|
|
|
indicator.setupWithViewPager(pager);
|
|
|
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
|
|
@@ -106,9 +72,59 @@ public class FgtFind extends BaseCompatFragment {
|
|
|
|
|
|
}
|
|
|
});
|
|
|
- pager.setCurrentItem(position);
|
|
|
+ CommonUtil.getInstance().reflex(indicator, activity);
|
|
|
+ }
|
|
|
+ private void initData() {
|
|
|
+ //尝试获取缓存数据
|
|
|
+ List<FindTag> newList = DataUtil.getInstance().getCacheList(ApiKey.articles_tags, FindTag.class);
|
|
|
+ loadList(newList);
|
|
|
+ initNetData();
|
|
|
+ }
|
|
|
+ private int onResumeCount = 0;
|
|
|
+ @Override
|
|
|
+ public void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ if(list.isEmpty() && onResumeCount != 0)//非第一次时,并且没有数据时调用该接口
|
|
|
+ initNetData();
|
|
|
+ onResumeCount++;
|
|
|
+ }
|
|
|
|
|
|
+ private void initNetData(){
|
|
|
+ SheepApp.getInstance().getNetComponent().getApiService().getFindTagList()
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
|
|
|
+ @Override
|
|
|
+ public void onNext(BaseMessage baseMessage) {
|
|
|
+ if(list.isEmpty()) {//无数据时加载网络获取的新数据,否则不加载新数据
|
|
|
+ List<FindTag> newList = baseMessage.getDatas(FindTag.class);
|
|
|
+ loadList(newList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(BaseMessage baseMessage) {
|
|
|
+ G.showToast(baseMessage);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ //没有添加数据返回true
|
|
|
+ private void loadList(List<FindTag> newList){
|
|
|
+ if(!ListUtil.isEmpty(newList)){
|
|
|
+ list.clear();
|
|
|
+ ListUtil.addAll(list, newList);
|
|
|
+ loadMore();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<FindTag> list = ListUtil.emptyList();
|
|
|
+ private void loadMore(){
|
|
|
+ for (FindTag findLabel : list) {
|
|
|
+ mAdapter.add(FgtFindChild.newInstance(findLabel.getId()), findLabel.getName());
|
|
|
+ }
|
|
|
+ mAdapter.notifyDataSetChanged();
|
|
|
CommonUtil.getInstance().reflex(indicator, activity);
|
|
|
+ pager.setCurrentItem(position);
|
|
|
}
|
|
|
private int position = 0;//默认选中第一个
|
|
|
|