baseList多接口-资讯页面(完成)
This commit is contained in:
@ -43,7 +43,7 @@ public class AskSelectGameAdapter extends ListAdapter<AskGameSelectEntity> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void provideListData(List<AskGameSelectEntity> listData) {
|
||||
protected void setListData(List<AskGameSelectEntity> listData) {
|
||||
mEntityList.addAll(listData);
|
||||
for (int i = 0; i < mEntityList.size(); i++) {
|
||||
AskGameSelectEntity entity = mEntityList.get(i);
|
||||
|
||||
@ -16,9 +16,8 @@ import com.gh.gamecenter.ask.entity.AnswerEntity;
|
||||
import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.ListViewModel;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -34,7 +33,7 @@ import static com.gh.gamecenter.ask.AskFragment.COMMUNITIES_SELECT_REQUEST;
|
||||
* Created by khy on 2/12/17.
|
||||
*/
|
||||
|
||||
public class AskQuestionsHotFragment extends ListFragment<AnswerEntity, ListViewModel> {
|
||||
public class AskQuestionsHotFragment extends ListFragment<AnswerEntity, NormalListViewModel> {
|
||||
|
||||
@BindView(R.id.reuse_nodata_skip_tv_btn)
|
||||
View mSkipHint;
|
||||
@ -88,7 +87,6 @@ public class AskQuestionsHotFragment extends ListFragment<AnswerEntity, ListView
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -15,7 +15,6 @@ import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.ListViewModel;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
@ -91,7 +90,6 @@ public class AskQuestionsNewBodyFragment extends ListFragment<Questions, ListVie
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -87,7 +87,7 @@ public class AskQuestionsNewFragment extends BaseFragment {
|
||||
public void onChanged(@Nullable List<AskTagGroupsEntity> tagGroups) {
|
||||
mTagGroupsList = tagGroups;
|
||||
if (tagGroups != null && tagGroups.size() > 0) {
|
||||
initTab();
|
||||
// initTab();
|
||||
mViewpager.setVisibility(View.VISIBLE);
|
||||
mAppBar.setVisibility(View.VISIBLE);
|
||||
mNoConn.setVisibility(View.GONE);
|
||||
|
||||
@ -24,7 +24,6 @@ import com.gh.gamecenter.ask.entity.CommunitiesGameEntity;
|
||||
import com.gh.gamecenter.ask.entity.MeEntity;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.db.info.InstallInfo;
|
||||
import com.gh.gamecenter.entity.InstallGameEntity;
|
||||
@ -139,7 +138,6 @@ public class SelectGameFragment extends ListFragment {
|
||||
break;
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
} else if (mAdapter.isOver()) {
|
||||
CheckLoginUtils.checkLogin(getContext(), this::showSelectDialog);
|
||||
|
||||
@ -10,7 +10,6 @@ import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -50,7 +49,6 @@ public class ConcernQuestionsFragment extends ListFragment {
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -5,15 +5,14 @@ import android.view.View;
|
||||
|
||||
import com.gh.common.util.AskLogUtils;
|
||||
import com.gh.common.util.EntranceUtils;
|
||||
import com.gh.gamecenter.QuestionsDetailActivity;
|
||||
import com.gh.gamecenter.NormalActivity;
|
||||
import com.gh.gamecenter.QuestionsDetailActivity;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.ask.AnswerDetailFragment;
|
||||
import com.gh.gamecenter.ask.entity.AnswerEntity;
|
||||
import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
@ -55,7 +54,6 @@ public class MyAnswerFragment extends ListFragment {
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -15,7 +15,6 @@ import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.ask.questionsdetail.AnswerEditFragment;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
@ -74,7 +73,6 @@ public class MyDraftFragment extends ListFragment {
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -10,7 +10,6 @@ import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
@ -50,7 +49,6 @@ public class MyQuestionsFragment extends ListFragment {
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -15,7 +15,6 @@ import com.gh.gamecenter.ask.AnswerDetailFragment;
|
||||
import com.gh.gamecenter.ask.entity.AnswerEntity;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -84,7 +83,6 @@ public class AnswerFoldFragment extends ListFragment {
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -60,7 +60,7 @@ public class QuestionsDetailAdapter extends ListAdapter<AnswerEntity> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void provideListData(List<AnswerEntity> listData) {
|
||||
protected void setListData(List<AnswerEntity> listData) {
|
||||
if (listData != null && listData.size() != 0 && mQuestionsDetailEntity != null) {
|
||||
mEntityList.addAll(listData);
|
||||
notifyItemRangeInserted(mEntityList.size() - listData.size() + TOP_ITEM_COUNT, listData.size());
|
||||
@ -70,19 +70,18 @@ public class QuestionsDetailAdapter extends ListAdapter<AnswerEntity> {
|
||||
@Override
|
||||
public void loadChange(LoadStatus status) {
|
||||
switch (status) {
|
||||
case OVER:
|
||||
case LIST_OVER:
|
||||
mIsOver = true;
|
||||
break;
|
||||
case ERROR:
|
||||
case LIST_FAILED:
|
||||
mIsNetworkError = true;
|
||||
break;
|
||||
case RETRY:
|
||||
case LIST_LOADED:
|
||||
mIsNetworkError = false;
|
||||
break;
|
||||
case NORMAL:
|
||||
case LIST_LOADING:
|
||||
mIsNetworkError = false;
|
||||
mIsOver = false;
|
||||
return;
|
||||
case REFRESH:
|
||||
mEntityList.clear();
|
||||
break;
|
||||
|
||||
@ -136,7 +136,7 @@ public class QuestionsDetailFragment extends ListFragment {
|
||||
@Override
|
||||
public void onLoadEmpty() {
|
||||
onLoadDone();
|
||||
mAdapter.loadChange(LoadStatus.OVER);
|
||||
mAdapter.loadChange(LoadStatus.LIST_OVER);
|
||||
}
|
||||
|
||||
@OnClick({R.id.questionsdetail_answer, R.id.questionsdetail_invite})
|
||||
@ -176,7 +176,6 @@ public class QuestionsDetailFragment extends ListFragment {
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
} else if (mAdapter.isOver()) {
|
||||
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
|
||||
|
||||
@ -35,7 +35,7 @@ public class AskSearchAdapter extends ListAdapter<AskSearchEntity> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void provideListData(List<AskSearchEntity> listData) {
|
||||
protected void setListData(List<AskSearchEntity> listData) {
|
||||
if (listData == null) {
|
||||
mEntityList.clear();
|
||||
} else {
|
||||
|
||||
@ -12,8 +12,8 @@ import com.gh.common.util.AskLogUtils;
|
||||
import com.gh.common.util.CheckLoginUtils;
|
||||
import com.gh.common.util.EntranceUtils;
|
||||
import com.gh.common.view.VerticalItemDecoration;
|
||||
import com.gh.gamecenter.QuestionsDetailActivity;
|
||||
import com.gh.gamecenter.NormalActivity;
|
||||
import com.gh.gamecenter.QuestionsDetailActivity;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.ask.AnswerDetailFragment;
|
||||
import com.gh.gamecenter.ask.QuestionsEditFragment;
|
||||
@ -21,7 +21,6 @@ import com.gh.gamecenter.ask.entity.AskSearchEntity;
|
||||
import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
@ -119,7 +118,7 @@ public class AskSearchFragment extends ListFragment {
|
||||
mReuseNoData.setVisibility(View.GONE);
|
||||
mListLoading.setVisibility(View.GONE);
|
||||
mListRefresh.setRefreshing(false);
|
||||
mAdapter.provideListData(null); // 刷新页面
|
||||
mAdapter.setListData(null); // 刷新页面
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -128,7 +127,7 @@ public class AskSearchFragment extends ListFragment {
|
||||
mReuseNoData.setVisibility(View.VISIBLE);
|
||||
mListLoading.setVisibility(View.GONE);
|
||||
mListRefresh.setRefreshing(false);
|
||||
mAdapter.provideListData(null); // 刷新页面
|
||||
mAdapter.setListData(null); // 刷新页面
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -137,7 +136,6 @@ public class AskSearchFragment extends ListFragment {
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
break;
|
||||
@ -179,7 +177,7 @@ public class AskSearchFragment extends ListFragment {
|
||||
mReuseNoConn.setVisibility(View.GONE);
|
||||
mReuseNoData.setVisibility(View.GONE);
|
||||
mListLoading.setVisibility(View.GONE);
|
||||
mAdapter.provideListData(null);
|
||||
mAdapter.setListData(null);
|
||||
return;
|
||||
}
|
||||
postLog(true);
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package com.gh.gamecenter.baselist;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.v7.util.DiffUtil;
|
||||
|
||||
import com.lightgame.adapter.BaseRecyclerAdapter;
|
||||
|
||||
@ -16,7 +19,7 @@ public abstract class ListAdapter<DataType> extends BaseRecyclerAdapter {
|
||||
public static final int FOOTER_ITEM_COUNT = 1;
|
||||
public static final int TOP_ITEM_COUNT = 1;
|
||||
|
||||
protected final List<DataType> mEntityList = new ArrayList<>();
|
||||
protected List<DataType> mEntityList;
|
||||
|
||||
protected boolean mIsOver;
|
||||
|
||||
@ -26,11 +29,64 @@ public abstract class ListAdapter<DataType> extends BaseRecyclerAdapter {
|
||||
super(context);
|
||||
}
|
||||
|
||||
protected void provideListData(List<DataType> listData) {
|
||||
if (listData != null && listData.size() != 0) {
|
||||
mEntityList.addAll(listData);
|
||||
notifyItemRangeInserted(mEntityList.size() - listData.size(), listData.size());
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
protected void setListData(List<DataType> updateData) {
|
||||
if (updateData == null) {
|
||||
mEntityList = new ArrayList<>();
|
||||
notifyDataSetChanged();
|
||||
return;
|
||||
}
|
||||
// 避免手动刷新列表时出现异常动画
|
||||
if (mEntityList != null && mEntityList.size() > updateData.size()) {
|
||||
mEntityList = new ArrayList<>(updateData);
|
||||
notifyDataSetChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
new AsyncTask<Void, Void, DiffUtil.DiffResult>() {
|
||||
@Override
|
||||
protected DiffUtil.DiffResult doInBackground(Void... voids) {
|
||||
return DiffUtil.calculateDiff(new DiffUtil.Callback() {
|
||||
@Override
|
||||
public int getOldListSize() {
|
||||
return mEntityList == null ? 0 : mEntityList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNewListSize() {
|
||||
return updateData.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
DataType oldItem = mEntityList.get(oldItemPosition);
|
||||
DataType newItem = updateData.get(newItemPosition);
|
||||
return ListAdapter.this.areItemsTheSame(oldItem, newItem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
DataType oldItem = mEntityList.get(oldItemPosition);
|
||||
DataType newItem = updateData.get(newItemPosition);
|
||||
return ListAdapter.this.areContentsTheSame(oldItem, newItem);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(DiffUtil.DiffResult diffResult) {
|
||||
mEntityList = new ArrayList<>(updateData);
|
||||
diffResult.dispatchUpdatesTo(ListAdapter.this);
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
|
||||
protected boolean areItemsTheSame(DataType oldItem, DataType newItem) {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected boolean areContentsTheSame(DataType oldItem, DataType newItem) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isNetworkError() {
|
||||
@ -39,16 +95,16 @@ public abstract class ListAdapter<DataType> extends BaseRecyclerAdapter {
|
||||
|
||||
public void loadChange(LoadStatus status) {
|
||||
switch (status) {
|
||||
case OVER:
|
||||
case LIST_OVER:
|
||||
mIsOver = true;
|
||||
break;
|
||||
case ERROR:
|
||||
case LIST_FAILED:
|
||||
mIsNetworkError = true;
|
||||
break;
|
||||
case RETRY:
|
||||
case LIST_LOADED:
|
||||
mIsNetworkError = false;
|
||||
break;
|
||||
case NORMAL:
|
||||
case LIST_LOADING:
|
||||
mIsNetworkError = false;
|
||||
mIsOver = false;
|
||||
return;
|
||||
|
||||
@ -15,7 +15,6 @@ import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
||||
import com.gh.common.view.VerticalItemDecoration;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.normal.NormalFragment;
|
||||
import com.halo.assistant.HaloApp;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
@ -44,10 +43,10 @@ public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位
|
||||
@BindView(R.id.reuse_none_data)
|
||||
protected LinearLayout mReuseNoData;
|
||||
|
||||
protected LinearLayoutManager mLayoutManager;
|
||||
|
||||
protected VM mListViewModel;
|
||||
|
||||
protected LinearLayoutManager mLayoutManager;
|
||||
|
||||
protected abstract ListAdapter provideListAdapter();
|
||||
|
||||
protected RecyclerView.ItemDecoration getItemDecoration() {
|
||||
@ -70,8 +69,7 @@ public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Class<VM> viewModelClass = getViewModelClass();
|
||||
final VM.Factory factory = new VM.Factory(HaloApp.getInstance().getApplication(), this);
|
||||
mListViewModel = ViewModelProviders.of(this, factory).get(viewModelClass);
|
||||
mListViewModel = ViewModelProviders.of(this).get(viewModelClass);
|
||||
mListViewModel.getObsListData().observe(this, this);
|
||||
mListViewModel.getLoadStatusLiveData().observe(this, o -> {
|
||||
if (o instanceof LoadStatus) {
|
||||
@ -88,13 +86,10 @@ public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位
|
||||
onLoadDone();
|
||||
break;
|
||||
case LIST_OVER:
|
||||
provideListAdapter().loadChange(LoadStatus.OVER);
|
||||
break;
|
||||
case LIST_LOADED:
|
||||
provideListAdapter().loadChange(LoadStatus.NORMAL);
|
||||
break;
|
||||
case LIST_FAILED:
|
||||
provideListAdapter().loadChange(LoadStatus.ERROR);
|
||||
case LIST_LOADING:
|
||||
provideListAdapter().loadChange(loadStatus);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -140,7 +135,7 @@ public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位
|
||||
@Override
|
||||
public void onChanged(@Nullable List<T> ts) {
|
||||
assert ts != null;
|
||||
provideListAdapter().provideListData(ts);
|
||||
provideListAdapter().setListData(ts);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -149,14 +144,11 @@ public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位
|
||||
}
|
||||
|
||||
public void onLoadRefresh() {
|
||||
mReuseNoConn.setVisibility(View.GONE);
|
||||
mReuseNoData.setVisibility(View.GONE);
|
||||
mListLoading.setVisibility(View.VISIBLE);
|
||||
mListRv.setVisibility(View.GONE);
|
||||
mBaseHandler.postDelayed(() -> {
|
||||
provideListAdapter().loadChange(LoadStatus.REFRESH);
|
||||
mReuseNoConn.setVisibility(View.GONE);
|
||||
mReuseNoData.setVisibility(View.GONE);
|
||||
mListLoading.setVisibility(View.VISIBLE);
|
||||
mListRv.setVisibility(View.GONE);
|
||||
mListRefresh.setRefreshing(false);
|
||||
|
||||
mListViewModel.load(LoadType.REFRESH);
|
||||
}, 500);
|
||||
}
|
||||
|
||||
@ -2,9 +2,11 @@ package com.gh.gamecenter.baselist;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
import android.support.annotation.MainThread;
|
||||
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import retrofit2.HttpException;
|
||||
@ -16,11 +18,11 @@ import rx.schedulers.Schedulers;
|
||||
* Created by khy on 2/12/17.
|
||||
*/
|
||||
|
||||
public class ListRepository<T> {
|
||||
public class ListRepository<LD> {
|
||||
|
||||
private final static int REQUEST_FAILURE_SIZE = -100;
|
||||
|
||||
private MutableLiveData<List<T>> mResultLiveData = new MutableLiveData<>();
|
||||
private MutableLiveData<List<LD>> mResultLiveData = new MutableLiveData<>();
|
||||
private MutableLiveData<LoadStatus> mLoadStatusLiveData = new MutableLiveData<>();
|
||||
|
||||
private OnDataObservable mDataObservable;
|
||||
@ -38,12 +40,13 @@ public class ListRepository<T> {
|
||||
if (mCurLoadParams == null) initLoadParams();
|
||||
|
||||
LoadParams loadParams = mRetryParams != null ? mRetryParams : mCurLoadParams;
|
||||
Observable<List<T>> listObservable = mDataObservable.provideDataObservable(loadParams.getLoadOffset());
|
||||
Observable<List<LD>> listObservable = mDataObservable.provideDataObservable(loadParams.getLoadOffset());
|
||||
LoadStatus curStatus = mLoadStatusLiveData.getValue();
|
||||
|
||||
if (listObservable == null || curStatus != null && curStatus != LoadStatus.INIT_LOADED && curStatus != LoadStatus.LIST_LOADED) {
|
||||
return;
|
||||
}
|
||||
if (listObservable == null || curStatus != null &&
|
||||
curStatus != LoadStatus.INIT_LOADED &&
|
||||
curStatus != LoadStatus.LIST_LOADED &&
|
||||
curStatus != LoadStatus.INIT) return;
|
||||
|
||||
if (mCurLoadParams.getLoadOffset() == 0) {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.INIT_LOADING);
|
||||
@ -54,16 +57,17 @@ public class ListRepository<T> {
|
||||
listObservable
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<List<T>>() {
|
||||
.subscribe(new Response<List<LD>>() {
|
||||
@Override
|
||||
public void onResponse(List<T> response) {
|
||||
public void onResponse(List<LD> response) {
|
||||
loadStatusControl(response.size());
|
||||
|
||||
// todo post整个列表还是当前列表
|
||||
// List<T> previousData = mResultLiveData.getValue();
|
||||
// if (previousData == null) previousData = new ArrayList<>();
|
||||
// previousData.addAll(response);
|
||||
mResultLiveData.postValue(response);
|
||||
List<LD> previousData = mResultLiveData.getValue();
|
||||
if (previousData == null || curStatus == LoadStatus.INIT) {
|
||||
previousData = new ArrayList<>();
|
||||
}
|
||||
previousData.addAll(response);
|
||||
mResultLiveData.postValue(previousData);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -104,16 +108,20 @@ public class ListRepository<T> {
|
||||
|
||||
private void initLoadParams() {
|
||||
mCurLoadParams = new LoadParams(0);
|
||||
mLoadStatusLiveData.setValue(LoadStatus.INIT);
|
||||
}
|
||||
|
||||
@MainThread
|
||||
protected void load(LoadType loadType) {
|
||||
if (loadType == LoadType.REFRESH) {
|
||||
initLoadParams();
|
||||
} else if (loadType == LoadType.RETRY) {
|
||||
mLoadStatusLiveData.setValue(LoadStatus.INIT);
|
||||
}
|
||||
loadData();
|
||||
}
|
||||
|
||||
public LiveData<List<T>> getResultLiveData() {
|
||||
public LiveData<List<LD>> getResultLiveData() {
|
||||
return mResultLiveData;
|
||||
}
|
||||
|
||||
|
||||
@ -3,38 +3,31 @@ package com.gh.gamecenter.baselist;
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.AndroidViewModel;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.ViewModel;
|
||||
import android.arch.lifecycle.ViewModelProvider;
|
||||
import android.arch.lifecycle.MediatorLiveData;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
/**
|
||||
* Created by khy on 2/12/17.
|
||||
*/
|
||||
|
||||
public class ListViewModel<T> extends AndroidViewModel implements OnDataObservable {
|
||||
public abstract class ListViewModel<LD /*ListData*/, ID /*ItemData*/> extends AndroidViewModel implements OnDataObservable {
|
||||
|
||||
private ListRepository mRepository;
|
||||
|
||||
protected LiveData<List<T>> mListLiveData;
|
||||
protected final MediatorLiveData<List<ID>> mResultLiveData = new MediatorLiveData<>();
|
||||
|
||||
|
||||
public ListViewModel(Application application, ListRepository repository) {
|
||||
super(application);
|
||||
mRepository = repository;
|
||||
mListLiveData = mRepository.getResultLiveData();
|
||||
}
|
||||
protected final LiveData<List<LD>> mListLiveData;
|
||||
|
||||
public ListViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
mRepository = new ListRepository(this);
|
||||
mListLiveData = mRepository.getResultLiveData();
|
||||
}
|
||||
|
||||
public LiveData<List<T>> getObsListData() {
|
||||
return mListLiveData;
|
||||
public LiveData<List<ID>> getObsListData() {
|
||||
return mResultLiveData;
|
||||
}
|
||||
|
||||
public LiveData<LoadStatus> getLoadStatusLiveData() {
|
||||
@ -45,27 +38,4 @@ public class ListViewModel<T> extends AndroidViewModel implements OnDataObservab
|
||||
mRepository.load(loadType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<T>> provideDataObservable(int offset) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class Factory extends ViewModelProvider.NewInstanceFactory {
|
||||
@NonNull
|
||||
private final Application mApplication;
|
||||
|
||||
private final ListRepository mRepository;
|
||||
|
||||
public Factory(@NonNull Application application, OnDataObservable onDataObservable) {
|
||||
mApplication = application;
|
||||
mRepository = new ListRepository(onDataObservable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends ViewModel> T create(Class<T> modelClass) {
|
||||
//noinspection unchecked
|
||||
return (T) new ListViewModel(mApplication, mRepository);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -7,18 +7,10 @@ package com.gh.gamecenter.baselist;
|
||||
public enum LoadStatus {
|
||||
|
||||
// todo 删除
|
||||
ERROR,
|
||||
|
||||
OVER,
|
||||
|
||||
REFRESH,
|
||||
|
||||
RETRY,
|
||||
|
||||
NORMAL,
|
||||
|
||||
|
||||
//初始化加载状态
|
||||
INIT,
|
||||
INIT_LOADING,
|
||||
INIT_LOADED,
|
||||
INIT_FAILED,
|
||||
|
||||
@ -0,0 +1,29 @@
|
||||
package com.gh.gamecenter.baselist;
|
||||
|
||||
import android.app.Application;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.gh.gamecenter.ask.entity.AnswerEntity;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
/**
|
||||
* Created by khy on 20/03/18.
|
||||
*/
|
||||
|
||||
public class NormalListViewModel extends ListViewModel<AnswerEntity, AnswerEntity> {
|
||||
|
||||
public NormalListViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
mResultLiveData.addSource(mListLiveData, mResultLiveData::postValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getApplication()).getApi().getAskHot(UserManager.getInstance().getCommunityId(getApplication()), offset);
|
||||
}
|
||||
}
|
||||
@ -13,7 +13,6 @@ import com.gh.gamecenter.ask.entity.AnswerEntity;
|
||||
import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -46,7 +45,6 @@ public class AnswerFragment extends ListFragment {
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -2,10 +2,7 @@ package com.gh.gamecenter.info;
|
||||
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MediatorLiveData;
|
||||
import android.arch.lifecycle.Observer;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.gh.gamecenter.baselist.ListViewModel;
|
||||
import com.gh.gamecenter.entity.NewsEntity;
|
||||
@ -20,15 +17,13 @@ import rx.Observable;
|
||||
* Created by khy on 19/03/18.
|
||||
*/
|
||||
|
||||
public class InFoViewModel extends ListViewModel<NewsEntity> {
|
||||
|
||||
MediatorLiveData<List<NewsEntity>> mNewsListLiveData = new MediatorLiveData<>();
|
||||
public class InFoViewModel extends ListViewModel<NewsEntity, NewsEntity> {
|
||||
|
||||
NewsViewsRepository<ViewsEntity> mNewsViewsRepository = new NewsViewsRepository<>();
|
||||
|
||||
public InFoViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
mNewsListLiveData.addSource(mListLiveData, (Observer<List<NewsEntity>>) list -> {
|
||||
mResultLiveData.addSource(mListLiveData, list -> {
|
||||
if (list == null) return;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0, size = list.size(); i < size; i++) {
|
||||
@ -42,12 +37,10 @@ public class InFoViewModel extends ListViewModel<NewsEntity> {
|
||||
});
|
||||
|
||||
LiveData<List<ViewsEntity>> listLiveData = mNewsViewsRepository.asLiveData();
|
||||
|
||||
mNewsListLiveData.addSource(listLiveData, new Observer<List<ViewsEntity>>() {
|
||||
@Override
|
||||
public void onChanged(@Nullable List<ViewsEntity> viewsEntities) {
|
||||
// todo 此处有问题
|
||||
List<NewsEntity> value = mNewsListLiveData.getValue();
|
||||
mResultLiveData.addSource(listLiveData, viewsEntities -> {
|
||||
// todo 此处有问题
|
||||
List<NewsEntity> value = mListLiveData.getValue();
|
||||
if (value != null && viewsEntities != null)
|
||||
for (ViewsEntity viewsEntity : viewsEntities) {
|
||||
for (NewsEntity newsEntity : value) {
|
||||
if (viewsEntity.getId().equals(newsEntity.getId())) {
|
||||
@ -56,16 +49,10 @@ public class InFoViewModel extends ListViewModel<NewsEntity> {
|
||||
}
|
||||
}
|
||||
}
|
||||
mNewsListLiveData.postValue(value);
|
||||
}
|
||||
mResultLiveData.postValue(value);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<List<NewsEntity>> getObsListData() {
|
||||
return mNewsListLiveData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<NewsEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getApplication()).getApi().getZiXun(offset);
|
||||
|
||||
@ -59,7 +59,8 @@ class InfoAdapter extends ListAdapter<NewsEntity> {
|
||||
view = mLayoutInflater.inflate(R.layout.news_image3_item, parent, false);
|
||||
return new NewsImage3ViewHolder(view, mEntityList, mListListener);
|
||||
case ItemViewType.LOADING:
|
||||
return new FooterViewHolder(mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false));
|
||||
view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false);
|
||||
return new FooterViewHolder(view, null, mListListener);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -6,8 +6,10 @@ import com.gh.common.util.DataCollectionUtils;
|
||||
import com.gh.common.util.StringUtils;
|
||||
import com.gh.gamecenter.DataUtils;
|
||||
import com.gh.gamecenter.NewsDetailActivity;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.entity.NewsEntity;
|
||||
|
||||
import java.util.HashMap;
|
||||
@ -29,6 +31,10 @@ public class InfoFragment extends ListFragment<List<NewsEntity>, InFoViewModel>
|
||||
|
||||
@Override
|
||||
public void onListClick(View view, int position, Object data) {
|
||||
if (view.getId() == R.id.footerview_item) {
|
||||
if (mAdapter.isNetworkError()) mListViewModel.load(LoadType.RETRY);
|
||||
return;
|
||||
}
|
||||
List<NewsEntity> newsList = (List<NewsEntity>) data;
|
||||
NewsEntity newsEntity = newsList.get(position);
|
||||
Map<String, Object> kv = new HashMap<>();
|
||||
|
||||
@ -17,7 +17,6 @@ import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.ListViewModel;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.entity.MessageEntity;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
@ -59,7 +58,6 @@ public class MessageFragment extends ListFragment<MessageEntity,ListViewModel> {
|
||||
public void onListClick(View view, int position, Object data) {
|
||||
if (view.getId() == R.id.footerview_item) {
|
||||
if (mAdapter.isNetworkError()) {
|
||||
mAdapter.loadChange(LoadStatus.RETRY);
|
||||
mListViewModel.load(LoadType.RETRY);
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user