baseList 兼容多接口列表
This commit is contained in:
@ -9,13 +9,14 @@ import android.view.View;
|
||||
import com.gh.common.util.AskLogUtils;
|
||||
import com.gh.common.util.CheckLoginUtils;
|
||||
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.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.manager.UserManager;
|
||||
@ -33,7 +34,7 @@ import static com.gh.gamecenter.ask.AskFragment.COMMUNITIES_SELECT_REQUEST;
|
||||
* Created by khy on 2/12/17.
|
||||
*/
|
||||
|
||||
public class AskQuestionsHotFragment extends ListFragment {
|
||||
public class AskQuestionsHotFragment extends ListFragment<AnswerEntity, ListViewModel> {
|
||||
|
||||
@BindView(R.id.reuse_nodata_skip_tv_btn)
|
||||
View mSkipHint;
|
||||
@ -65,8 +66,8 @@ public class AskQuestionsHotFragment extends ListFragment {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getAskHot(UserManager.getInstance().getCommunityId(getContext()), getListOffset());
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getAskHot(UserManager.getInstance().getCommunityId(getContext()), offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -14,6 +14,7 @@ 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.ListViewModel;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
@ -30,7 +31,7 @@ import rx.Observable;
|
||||
* Created by khy on 5/12/17.
|
||||
*/
|
||||
|
||||
public class AskQuestionsNewBodyFragment extends ListFragment {
|
||||
public class AskQuestionsNewBodyFragment extends ListFragment<Questions, ListViewModel> {
|
||||
|
||||
private AskQuestionsNewBodyAdapter mAdapter;
|
||||
private CheckLoginUtils.OnLoggenInListener mOnLoggenInListener;
|
||||
@ -81,9 +82,8 @@ public class AskQuestionsNewBodyFragment extends ListFragment {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Observable<List<Questions>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi()
|
||||
.getAskQuestions(mCommunityId, mType, getListOffset());
|
||||
public Observable<List<Questions>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getAskQuestions(mCommunityId, mType, offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -114,8 +114,8 @@ public class SelectGameFragment extends ListFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<AskGameSelectEntity>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getAskGameSelect(getListOffset());
|
||||
public Observable<List<AskGameSelectEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getAskGameSelect(offset);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -41,8 +41,8 @@ public class ConcernQuestionsFragment extends ListFragment {
|
||||
|
||||
|
||||
@Override
|
||||
public Observable<List<Questions>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getConcernQuestions(getListOffset());
|
||||
public Observable<List<Questions>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getConcernQuestions(offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -44,8 +44,8 @@ public class MyAnswerFragment extends ListFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getMyAnswers(UserManager.getInstance().getUserId(), getListOffset());
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getMyAnswers(UserManager.getInstance().getUserId(), offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -64,8 +64,8 @@ public class MyDraftFragment extends ListFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getMyAnswerDrafts(UserManager.getInstance().getUserId(), getListOffset());
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getMyAnswerDrafts(UserManager.getInstance().getUserId(), offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -41,8 +41,8 @@ public class MyQuestionsFragment extends ListFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<Questions>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getMyQuestions(UserManager.getInstance().getUserId(), getListOffset());
|
||||
public Observable<List<Questions>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getMyQuestions(UserManager.getInstance().getUserId(), offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.gh.gamecenter.ask.questionsdetail;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
@ -66,9 +67,10 @@ public class AnswerFoldFragment extends ListFragment {
|
||||
return new VerticalItemDecoration(getContext(), 8, false);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getQuestionsAnswer(mQuestionsId, true, mListViewModel.getListOffset());
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getQuestionsAnswer(mQuestionsId, true, offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -165,8 +165,8 @@ public class QuestionsDetailFragment extends ListFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getQuestionsAnswer(mQuestionsId, false, mListViewModel.getListOffset());
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getQuestionsAnswer(mQuestionsId, false, offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -63,8 +63,8 @@ public class QuestionsInviteFragment extends ListFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<InviteEntity>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getInviteExperts(mQuestionsDetailEntity.getId(), getListOffset());
|
||||
public Observable<List<InviteEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getInviteExperts(mQuestionsDetailEntity.getId(), offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -84,9 +84,9 @@ public class AskSearchFragment extends ListFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<AskSearchEntity>> provideDataObservable() {
|
||||
public Observable<List<AskSearchEntity>> provideDataObservable(int offset) {
|
||||
if (TextUtils.isEmpty(mSearchKey)) return null;
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getAskSearch(UserManager.getInstance().getCommunityId(getContext()), mSearchKey, getListOffset());
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getAskSearch(UserManager.getInstance().getCommunityId(getContext()), mSearchKey, offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -15,7 +15,10 @@ 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;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
@ -24,10 +27,10 @@ import butterknife.BindView;
|
||||
* Created by khy on 2/12/17.
|
||||
*/
|
||||
|
||||
public abstract class ListFragment<T> extends NormalFragment implements
|
||||
public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位置对应getViewModelClass */> extends NormalFragment implements
|
||||
Observer<List<T>>,
|
||||
OnListLoadListener,
|
||||
SwipeRefreshLayout.OnRefreshListener {
|
||||
SwipeRefreshLayout.OnRefreshListener,
|
||||
OnDataObservable {
|
||||
|
||||
@BindView(R.id.list_rv)
|
||||
protected RecyclerView mListRv;
|
||||
@ -42,7 +45,7 @@ public abstract class ListFragment<T> extends NormalFragment implements
|
||||
|
||||
protected LinearLayoutManager mLayoutManager;
|
||||
|
||||
protected ListViewModel mListViewModel;
|
||||
protected VM mListViewModel;
|
||||
|
||||
protected abstract ListAdapter provideListAdapter();
|
||||
|
||||
@ -65,9 +68,42 @@ public abstract class ListFragment<T> extends NormalFragment implements
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
final ListViewModel.Factory factory = new ListViewModel.Factory(getActivity().getApplication(), this);
|
||||
mListViewModel = ViewModelProviders.of(this, factory).get(ListViewModel.class);
|
||||
Class<VM> viewModelClass = getViewModelClass();
|
||||
if (ListViewModel.class.getName().equals(viewModelClass.getName())) {
|
||||
final ListViewModel.Factory factory = new ListViewModel.Factory(HaloApp.getInstance().getApplication(), this);
|
||||
mListViewModel = ViewModelProviders.of(this, factory).get(viewModelClass);
|
||||
} else {
|
||||
mListViewModel = ViewModelProviders.of(this).get(viewModelClass);
|
||||
}
|
||||
|
||||
mListViewModel.getObsListData().observe(this, this);
|
||||
mListViewModel.getLoadStatusLiveData().observe(this, o -> {
|
||||
if (o instanceof LoadStatus) {
|
||||
LoadStatus loadStatus = (LoadStatus) o;
|
||||
switch (loadStatus) {
|
||||
case INIT_EMPTY:
|
||||
onLoadEmpty();
|
||||
break;
|
||||
case INIT_FAILED:
|
||||
onLoadError();
|
||||
break;
|
||||
case INIT_OVER:
|
||||
case INIT_LOADED:
|
||||
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);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (isAutomaticLoad()) mListViewModel.load(LoadType.NORMAL);
|
||||
|
||||
mListRv.addItemDecoration(getItemDecoration());
|
||||
@ -91,24 +127,29 @@ public abstract class ListFragment<T> extends NormalFragment implements
|
||||
mListViewModel.load(LoadType.NORMAL);
|
||||
}
|
||||
});
|
||||
mReuseNoConn.setOnClickListener(view1 -> {
|
||||
mReuseNoConn.setVisibility(View.GONE);
|
||||
mReuseNoData.setVisibility(View.GONE);
|
||||
mListLoading.setVisibility(View.VISIBLE);
|
||||
mListRv.setVisibility(View.GONE);
|
||||
mListRefresh.setRefreshing(false);
|
||||
|
||||
mListViewModel.load(LoadType.REFRESH);
|
||||
});
|
||||
mReuseNoConn.setOnClickListener(view1 -> onLoadRefresh());
|
||||
}
|
||||
|
||||
protected int getListOffset() {
|
||||
return mListViewModel == null ? 0 : mListViewModel.getListOffset();
|
||||
private Class<VM> getViewModelClass() {
|
||||
Type t = getClass().getGenericSuperclass();
|
||||
Type[] params = ((ParameterizedType) t).getActualTypeArguments();
|
||||
return (Class<VM>) params[1]; // 该类泛型类型中第二个是<VM>
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
onLoadRefresh();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChanged(@Nullable List<T> ts) {
|
||||
assert ts != null;
|
||||
provideListAdapter().provideListData(ts);
|
||||
}
|
||||
|
||||
public void onLoadRefresh() {
|
||||
mBaseHandler.postDelayed(() -> {
|
||||
provideListAdapter().loadChange(LoadStatus.REFRESH);
|
||||
mReuseNoConn.setVisibility(View.GONE);
|
||||
mReuseNoData.setVisibility(View.GONE);
|
||||
mListLoading.setVisibility(View.VISIBLE);
|
||||
@ -119,14 +160,6 @@ public abstract class ListFragment<T> extends NormalFragment implements
|
||||
}, 500);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChanged(@Nullable List<T> ts) {
|
||||
assert ts != null;
|
||||
provideListAdapter().provideListData(ts);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onLoadDone() {
|
||||
mReuseNoConn.setVisibility(View.GONE);
|
||||
mReuseNoData.setVisibility(View.GONE);
|
||||
@ -135,7 +168,6 @@ public abstract class ListFragment<T> extends NormalFragment implements
|
||||
mListRefresh.setRefreshing(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadError() {
|
||||
mReuseNoConn.setVisibility(View.VISIBLE);
|
||||
mReuseNoData.setVisibility(View.GONE);
|
||||
@ -144,7 +176,6 @@ public abstract class ListFragment<T> extends NormalFragment implements
|
||||
mListRefresh.setRefreshing(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadEmpty() {
|
||||
mReuseNoConn.setVisibility(View.GONE);
|
||||
mReuseNoData.setVisibility(View.VISIBLE);
|
||||
@ -152,24 +183,4 @@ public abstract class ListFragment<T> extends NormalFragment implements
|
||||
mListRv.setVisibility(View.GONE);
|
||||
mListRefresh.setRefreshing(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadNormal() {
|
||||
provideListAdapter().loadChange(LoadStatus.NORMAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadMoreError() {
|
||||
provideListAdapter().loadChange(LoadStatus.ERROR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadOver() {
|
||||
provideListAdapter().loadChange(LoadStatus.OVER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadRefresh() {
|
||||
provideListAdapter().loadChange(LoadStatus.REFRESH);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,13 +4,9 @@ import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
import com.lightgame.config.CommonDebug;
|
||||
import com.lightgame.utils.Utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.HttpException;
|
||||
import rx.Observable;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
@ -22,30 +18,38 @@ import rx.schedulers.Schedulers;
|
||||
|
||||
public class ListRepository<T> {
|
||||
|
||||
public final static int PAGE_SIZE = 20;
|
||||
private final static int REQUEST_FAILURE_SIZE = -100;
|
||||
|
||||
private MutableLiveData<List<T>> mListLiveData;
|
||||
private MutableLiveData<List<T>> mResultLiveData = new MutableLiveData<>();
|
||||
private MutableLiveData<LoadStatus> mLoadStatusLiveData = new MutableLiveData<>();
|
||||
|
||||
private OnListLoadListener mLoadListener;
|
||||
private OnDataObservable mDataObservable;
|
||||
|
||||
private boolean mIsLoading;
|
||||
private boolean mIsOver;
|
||||
private boolean mIsNetworkError;
|
||||
|
||||
private int mListOffset;
|
||||
private LoadParams mRetryParams;
|
||||
private LoadParams mCurLoadParams;
|
||||
|
||||
|
||||
public ListRepository(OnListLoadListener loadListener) {
|
||||
mListOffset = 0;
|
||||
mLoadListener = loadListener;
|
||||
mListLiveData = new MutableLiveData<>();
|
||||
public ListRepository(OnDataObservable onDataObservable) {
|
||||
mDataObservable = onDataObservable;
|
||||
initLoadParams();
|
||||
}
|
||||
|
||||
private void loadData() {
|
||||
Observable<List<T>> listObservable = mLoadListener.provideDataObservable();
|
||||
if (mCurLoadParams == null) initLoadParams();
|
||||
|
||||
if (mIsLoading || mIsOver || mIsNetworkError || listObservable == null) return;
|
||||
mIsLoading = true;
|
||||
LoadParams loadParams = mRetryParams != null ? mRetryParams : mCurLoadParams;
|
||||
Observable<List<T>> listObservable = mDataObservable.provideDataObservable(loadParams.getLoadOffset());
|
||||
LoadStatus curStatus = mLoadStatusLiveData.getValue();
|
||||
|
||||
if (curStatus != null && curStatus != LoadStatus.INIT_LOADED && curStatus != LoadStatus.LIST_LOADED) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mCurLoadParams.getLoadOffset() == 0) {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.INIT_LOADING);
|
||||
} else {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.LIST_LOADING);
|
||||
}
|
||||
|
||||
listObservable
|
||||
.subscribeOn(Schedulers.io())
|
||||
@ -53,79 +57,67 @@ public class ListRepository<T> {
|
||||
.subscribe(new Response<List<T>>() {
|
||||
@Override
|
||||
public void onResponse(List<T> response) {
|
||||
super.onResponse(response);
|
||||
int size = response.size();
|
||||
if (size == 0 && mListOffset == 0) {
|
||||
mLoadListener.onLoadEmpty();
|
||||
} else {
|
||||
if (size == 0 || mListOffset == 0 && size < PAGE_SIZE) {
|
||||
mIsOver = true;
|
||||
mLoadListener.onLoadOver();
|
||||
} else {
|
||||
mLoadListener.onLoadNormal();
|
||||
}
|
||||
mListOffset += size;
|
||||
mLoadListener.onLoadDone();
|
||||
cacheAndNotifyListData(response);
|
||||
}
|
||||
mIsLoading = false;
|
||||
mIsNetworkError = false;
|
||||
loadStatusControl(response.size());
|
||||
|
||||
// todo post整个列表还是当前列表
|
||||
// List<T> previousData = mResultLiveData.getValue();
|
||||
// if (previousData == null) previousData = new ArrayList<>();
|
||||
// previousData.addAll(response);
|
||||
mResultLiveData.postValue(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
mIsLoading = false;
|
||||
mIsNetworkError = true;
|
||||
if (mListOffset > 0) {
|
||||
mLoadListener.onLoadMoreError();
|
||||
} else {
|
||||
mLoadListener.onLoadError();
|
||||
}
|
||||
|
||||
if (CommonDebug.IS_DEBUG && e != null) {
|
||||
try {
|
||||
ResponseBody responseBody = e.response().errorBody();
|
||||
String string = responseBody.string();
|
||||
Utils.log("ListRepository->onFailure::" + e.code() + "==" + string);
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
loadStatusControl(REQUEST_FAILURE_SIZE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void resetOffset() {
|
||||
mListOffset = 0;
|
||||
mIsOver = false;
|
||||
mIsNetworkError = false;
|
||||
private void loadStatusControl(int size) {
|
||||
if (mCurLoadParams.getLoadOffset() == 0) { // 初始化列表
|
||||
if (size == 0) {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.INIT_EMPTY);
|
||||
} else if (size == REQUEST_FAILURE_SIZE) {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.INIT_FAILED);
|
||||
} else if (size < mCurLoadParams.getLoadSize()) {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.INIT_OVER);
|
||||
} else {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.INIT_LOADED);
|
||||
}
|
||||
} else {
|
||||
if (size == REQUEST_FAILURE_SIZE) {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.LIST_FAILED);
|
||||
} else if (size < mCurLoadParams.getLoadSize()) {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.LIST_OVER);
|
||||
} else {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.LIST_LOADED);
|
||||
}
|
||||
}
|
||||
|
||||
if (size == REQUEST_FAILURE_SIZE) {
|
||||
mRetryParams = mCurLoadParams;
|
||||
} else {
|
||||
mRetryParams = null;
|
||||
mCurLoadParams.setLoadOffset(mCurLoadParams.getLoadOffset() + size);
|
||||
}
|
||||
}
|
||||
|
||||
private void initLoadParams() {
|
||||
mCurLoadParams = new LoadParams(0);
|
||||
}
|
||||
|
||||
protected void load(LoadType loadType) {
|
||||
if (loadType == null) loadType = LoadType.NORMAL;
|
||||
switch (loadType) {
|
||||
case REFRESH:
|
||||
mLoadListener.onLoadRefresh();
|
||||
resetOffset();
|
||||
break;
|
||||
case RETRY:
|
||||
mIsNetworkError = false;
|
||||
break;
|
||||
if (loadType == LoadType.REFRESH) {
|
||||
initLoadParams();
|
||||
}
|
||||
loadData();
|
||||
}
|
||||
|
||||
protected int getListOffset() {
|
||||
return mListOffset;
|
||||
public LiveData<List<T>> getResultLiveData() {
|
||||
return mResultLiveData;
|
||||
}
|
||||
|
||||
|
||||
private void cacheAndNotifyListData(List<T> listData) {
|
||||
mListLiveData.postValue(listData);
|
||||
}
|
||||
|
||||
public LiveData<List<T>> getListLiveData() {
|
||||
return mListLiveData;
|
||||
public LiveData<LoadStatus> getLoadStatusLiveData() {
|
||||
return mLoadStatusLiveData;
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,11 +9,13 @@ 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 {
|
||||
public class ListViewModel<T> extends AndroidViewModel implements OnDataObservable {
|
||||
|
||||
private ListRepository mRepository;
|
||||
|
||||
@ -23,7 +25,13 @@ public class ListViewModel<T> extends AndroidViewModel {
|
||||
public ListViewModel(Application application, ListRepository repository) {
|
||||
super(application);
|
||||
mRepository = repository;
|
||||
mLiveListData = mRepository.getListLiveData();
|
||||
mLiveListData = mRepository.getResultLiveData();
|
||||
|
||||
}
|
||||
|
||||
public ListViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
mRepository = new ListRepository(this);
|
||||
|
||||
}
|
||||
|
||||
@ -31,13 +39,17 @@ public class ListViewModel<T> extends AndroidViewModel {
|
||||
return mLiveListData;
|
||||
}
|
||||
|
||||
public LiveData<LoadStatus> getLoadStatusLiveData() {
|
||||
return mRepository.getLoadStatusLiveData();
|
||||
}
|
||||
|
||||
public void load(LoadType loadType) {
|
||||
mRepository.load(loadType);
|
||||
}
|
||||
|
||||
public int getListOffset() {
|
||||
return mRepository.getListOffset();
|
||||
@Override
|
||||
public Observable<List<T>> provideDataObservable(int offset) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class Factory extends ViewModelProvider.NewInstanceFactory {
|
||||
@ -46,9 +58,9 @@ public class ListViewModel<T> extends AndroidViewModel {
|
||||
|
||||
private final ListRepository mRepository;
|
||||
|
||||
public Factory(@NonNull Application application, OnListLoadListener loadListener) {
|
||||
public Factory(@NonNull Application application, OnDataObservable onDataObservable) {
|
||||
mApplication = application;
|
||||
mRepository = new ListRepository(loadListener);
|
||||
mRepository = new ListRepository(onDataObservable);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
60
app/src/main/java/com/gh/gamecenter/baselist/LoadParams.java
Normal file
60
app/src/main/java/com/gh/gamecenter/baselist/LoadParams.java
Normal file
@ -0,0 +1,60 @@
|
||||
package com.gh.gamecenter.baselist;
|
||||
|
||||
/**
|
||||
* Created by khy on 6/03/18.
|
||||
*/
|
||||
|
||||
public class LoadParams {
|
||||
|
||||
public final static int DEFAULT_PAGE_SIZE = 20;
|
||||
|
||||
private int loadSize;
|
||||
|
||||
private int loadOffset;
|
||||
|
||||
private Object loadKey;
|
||||
|
||||
public LoadParams(int loadOffset) {
|
||||
this.loadOffset = loadOffset;
|
||||
}
|
||||
|
||||
public LoadParams(int loadSize, int loadOffset) {
|
||||
this.loadSize = loadSize;
|
||||
this.loadOffset = loadOffset;
|
||||
}
|
||||
|
||||
public LoadParams(int loadSize, int loadOffset, Object loadKey) {
|
||||
this.loadSize = loadSize;
|
||||
this.loadOffset = loadOffset;
|
||||
this.loadKey = loadKey;
|
||||
}
|
||||
|
||||
public int getLoadSize() {
|
||||
return loadSize <= 0 ? DEFAULT_PAGE_SIZE : loadSize;
|
||||
}
|
||||
|
||||
public void setLoadSize(int loadSize) {
|
||||
this.loadSize = loadSize;
|
||||
}
|
||||
|
||||
public int getLoadOffset() {
|
||||
return loadOffset;
|
||||
}
|
||||
|
||||
public void setLoadOffset(int loadOffset) {
|
||||
this.loadOffset = loadOffset;
|
||||
}
|
||||
|
||||
public Object getLoadKey() {
|
||||
return loadKey;
|
||||
}
|
||||
|
||||
public void setLoadKey(Object loadKey) {
|
||||
this.loadKey = loadKey;
|
||||
}
|
||||
|
||||
|
||||
public void init() {
|
||||
loadOffset = 0;
|
||||
}
|
||||
}
|
||||
@ -6,6 +6,7 @@ package com.gh.gamecenter.baselist;
|
||||
|
||||
public enum LoadStatus {
|
||||
|
||||
// todo 删除
|
||||
ERROR,
|
||||
|
||||
OVER,
|
||||
@ -14,5 +15,19 @@ public enum LoadStatus {
|
||||
|
||||
RETRY,
|
||||
|
||||
NORMAL
|
||||
NORMAL,
|
||||
|
||||
|
||||
//初始化加载状态
|
||||
INIT_LOADING,
|
||||
INIT_LOADED,
|
||||
INIT_FAILED,
|
||||
INIT_OVER,
|
||||
INIT_EMPTY,
|
||||
|
||||
//列表分页加载状态
|
||||
LIST_LOADING,
|
||||
LIST_LOADED,
|
||||
LIST_FAILED,
|
||||
LIST_OVER
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package com.gh.gamecenter.baselist;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
/**
|
||||
* Created by khy on 19/03/18.
|
||||
*/
|
||||
|
||||
public interface OnDataObservable {
|
||||
<T> Observable<List<T>> provideDataObservable(int offset);
|
||||
}
|
||||
@ -5,8 +5,8 @@ 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;
|
||||
@ -35,8 +35,8 @@ public class AnswerFragment extends ListFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getCollestionAnswer(getListOffset());
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getCollestionAnswer(offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -16,6 +16,7 @@ import com.gh.gamecenter.ask.AnswerDetailFragment;
|
||||
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;
|
||||
@ -30,7 +31,7 @@ import rx.Observable;
|
||||
* 消息-评论
|
||||
*/
|
||||
|
||||
public class MessageFragment extends ListFragment {
|
||||
public class MessageFragment extends ListFragment<MessageEntity,ListViewModel> {
|
||||
|
||||
MessageFragmentAdapter mAdapter;
|
||||
|
||||
@ -50,8 +51,8 @@ public class MessageFragment extends ListFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<MessageEntity>> provideDataObservable() {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getMessageData(getListOffset());
|
||||
public Observable<List<MessageEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getMessageData(offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user