baseList多接口兼容完成
This commit is contained in:
@ -25,6 +25,7 @@ public abstract class BaseRecyclerViewHolder<T> extends RecyclerView.ViewHolder
|
||||
|
||||
/**
|
||||
* 具体的设置监听在childViewHolder 设置
|
||||
*
|
||||
* @param itemView
|
||||
* @param data 一般情况下只传列表数据
|
||||
* @param listClickListener 列表事件接口
|
||||
@ -35,6 +36,15 @@ public abstract class BaseRecyclerViewHolder<T> extends RecyclerView.ViewHolder
|
||||
this.mListClickListener = listClickListener;
|
||||
}
|
||||
|
||||
public BaseRecyclerViewHolder(View itemView, OnListClickListener listClickListener) {
|
||||
this(itemView);
|
||||
this.mListClickListener = listClickListener;
|
||||
}
|
||||
|
||||
public void setClickData(T clickData) {
|
||||
this.mData = clickData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
try {
|
||||
|
||||
@ -21,6 +21,7 @@ import com.gh.gamecenter.ask.entity.MeEntity;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -44,7 +45,8 @@ public class AskSelectGameAdapter extends ListAdapter<AskGameSelectEntity> {
|
||||
|
||||
@Override
|
||||
protected void setListData(List<AskGameSelectEntity> listData) {
|
||||
mEntityList.addAll(listData);
|
||||
if (listData == null) return;
|
||||
mEntityList = new ArrayList<>(listData);
|
||||
for (int i = 0; i < mEntityList.size(); i++) {
|
||||
AskGameSelectEntity entity = mEntityList.get(i);
|
||||
if (TextUtils.isEmpty(entity.getStatus())) {
|
||||
|
||||
@ -42,6 +42,11 @@ public class FooterViewHolder extends BaseRecyclerViewHolder {
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
public FooterViewHolder(View itemView, OnListClickListener listClickListener) {
|
||||
super(itemView, listClickListener);
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
// 减去recyclerView item间隔(个别列表间隔很大)
|
||||
public void initItemPadding() {
|
||||
itemView.setPadding(0, 0, 0, 0);
|
||||
|
||||
@ -37,10 +37,10 @@ public class AskQuestionsHotAdapter extends ListAdapter<AnswerEntity> {
|
||||
switch (viewType) {
|
||||
case ItemViewType.ITEM_FOOTER:
|
||||
view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false);
|
||||
return new FooterViewHolder(view, null, mListClickListener);
|
||||
return new FooterViewHolder(view, mListClickListener);
|
||||
case ItemViewType.ITEM_BODY:
|
||||
view = mLayoutInflater.inflate(R.layout.ask_questions_hot_item, parent, false);
|
||||
return new AskQuestionsHotViewHolder(view, mEntityList, mListClickListener);
|
||||
return new AskQuestionsHotViewHolder(view, mListClickListener);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -17,15 +17,9 @@ import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.OnClick;
|
||||
import rx.Observable;
|
||||
|
||||
import static com.gh.gamecenter.ask.AskFragment.COMMUNITIES_SELECT_REQUEST;
|
||||
|
||||
@ -33,7 +27,7 @@ import static com.gh.gamecenter.ask.AskFragment.COMMUNITIES_SELECT_REQUEST;
|
||||
* Created by khy on 2/12/17.
|
||||
*/
|
||||
|
||||
public class AskQuestionsHotFragment extends ListFragment<AnswerEntity, NormalListViewModel> {
|
||||
public class AskQuestionsHotFragment extends ListFragment<AnswerEntity, AskQuestionsHotViewModel> {
|
||||
|
||||
@BindView(R.id.reuse_nodata_skip_tv_btn)
|
||||
View mSkipHint;
|
||||
@ -62,13 +56,6 @@ public class AskQuestionsHotFragment extends ListFragment<AnswerEntity, NormalLi
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getAskHot(UserManager.getInstance().getCommunityId(getContext()), offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ListAdapter provideListAdapter() {
|
||||
return mAdapter == null ? mAdapter = new AskQuestionsHotAdapter(getContext(), this) : mAdapter;
|
||||
@ -83,7 +70,7 @@ public class AskQuestionsHotFragment extends ListFragment<AnswerEntity, NormalLi
|
||||
|
||||
@Override
|
||||
public void onListClick(View view, int position, Object data) {
|
||||
List<AnswerEntity> questionsList;
|
||||
AnswerEntity answerEntity;
|
||||
switch (view.getId()) {
|
||||
case R.id.footerview_item:
|
||||
if (mAdapter.isNetworkError()) {
|
||||
@ -92,15 +79,15 @@ public class AskQuestionsHotFragment extends ListFragment<AnswerEntity, NormalLi
|
||||
break;
|
||||
case R.id.ask_item_title:
|
||||
String tracers = "(首页精选)";
|
||||
questionsList = (List<AnswerEntity>) data;
|
||||
Questions questions = questionsList.get(position).getQuestions();
|
||||
answerEntity = (AnswerEntity) data;
|
||||
Questions questions = answerEntity.getQuestions();
|
||||
startActivity(QuestionsDetailActivity.getIntent(getContext(), questions.getId(), tracers));
|
||||
AskLogUtils.uploadQuestions(getContext(), tracers, questions);
|
||||
break;
|
||||
case R.id.ask_item_constraintlayout:
|
||||
questionsList = (List<AnswerEntity>) data;
|
||||
answerEntity = (AnswerEntity) data;
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString(EntranceUtils.KEY_ANSWER_ID, questionsList.get(position).getId());
|
||||
bundle.putString(EntranceUtils.KEY_ANSWER_ID, answerEntity.getId());
|
||||
bundle.putString(EntranceUtils.KEY_ENTRANCE, "(首页精选)");
|
||||
NormalActivity.startFragment(getContext(), AnswerDetailFragment.class, bundle);
|
||||
break;
|
||||
|
||||
@ -20,7 +20,7 @@ import butterknife.BindView;
|
||||
* Created by khy on 4/12/17.
|
||||
*/
|
||||
|
||||
public class AskQuestionsHotViewHolder extends BaseRecyclerViewHolder {
|
||||
public class AskQuestionsHotViewHolder extends BaseRecyclerViewHolder<AnswerEntity> {
|
||||
|
||||
|
||||
@BindView(R.id.ask_item_usericon)
|
||||
@ -36,13 +36,14 @@ public class AskQuestionsHotViewHolder extends BaseRecyclerViewHolder {
|
||||
@BindView(R.id.ask_item_votecount)
|
||||
TextView mAskVotecount;
|
||||
|
||||
public AskQuestionsHotViewHolder(View itemView, Object data, OnListClickListener listClickListener) {
|
||||
super(itemView, data, listClickListener);
|
||||
public AskQuestionsHotViewHolder(View itemView,OnListClickListener listClickListener) {
|
||||
super(itemView, listClickListener);
|
||||
itemView.setOnClickListener(this);
|
||||
mAskTitle.setOnClickListener(this);
|
||||
}
|
||||
|
||||
public void initQuestionsHotViewHolder(AnswerEntity entity) {
|
||||
setClickData(entity);
|
||||
mAskVotecount.setText(itemView.getContext().getString(R.string.ask_vote_count, AskUtils.voteCountFormat(entity.getVote())));
|
||||
|
||||
mAskContent.setText(entity.getBrief());
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
package com.gh.gamecenter.ask;
|
||||
|
||||
import android.app.Application;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.gh.gamecenter.ask.entity.AnswerEntity;
|
||||
import com.gh.gamecenter.baselist.ListViewModel;
|
||||
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 AskQuestionsHotViewModel extends ListViewModel<AnswerEntity, AnswerEntity> {
|
||||
|
||||
public AskQuestionsHotViewModel(@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);
|
||||
}
|
||||
}
|
||||
@ -14,8 +14,8 @@ 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.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -30,7 +30,7 @@ import rx.Observable;
|
||||
* Created by khy on 5/12/17.
|
||||
*/
|
||||
|
||||
public class AskQuestionsNewBodyFragment extends ListFragment<Questions, ListViewModel> {
|
||||
public class AskQuestionsNewBodyFragment extends ListFragment<Questions, NormalListViewModel> {
|
||||
|
||||
private AskQuestionsNewBodyAdapter mAdapter;
|
||||
private CheckLoginUtils.OnLoggenInListener mOnLoggenInListener;
|
||||
@ -79,7 +79,6 @@ public class AskQuestionsNewBodyFragment extends ListFragment<Questions, ListVie
|
||||
return mAdapter == null ? mAdapter = new AskQuestionsNewBodyAdapter(getContext(), this) : mAdapter;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Observable<List<Questions>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getAskQuestions(mCommunityId, mType, offset);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -54,7 +54,7 @@ import rx.schedulers.Schedulers;
|
||||
* Created by khy on 11/12/17.
|
||||
*/
|
||||
|
||||
public class SelectGameFragment extends ListFragment {
|
||||
public class SelectGameFragment extends ListFragment<AskGameSelectEntity, SelectGameViewModel> {
|
||||
|
||||
@BindView(R.id.ask_selectgame_rv_title)
|
||||
TextView mRvTitle;
|
||||
|
||||
@ -0,0 +1,29 @@
|
||||
package com.gh.gamecenter.ask;
|
||||
|
||||
import android.app.Application;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.gh.gamecenter.ask.entity.AskGameSelectEntity;
|
||||
import com.gh.gamecenter.baselist.ListViewModel;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
/**
|
||||
* Created by khy on 20/03/18.
|
||||
*/
|
||||
|
||||
public class SelectGameViewModel extends ListViewModel<AskGameSelectEntity, AskGameSelectEntity> {
|
||||
|
||||
public SelectGameViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
mResultLiveData.addSource(mListLiveData, mResultLiveData::postValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<AskGameSelectEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getApplication()).getApi().getAskGameSelect(offset);
|
||||
}
|
||||
}
|
||||
@ -11,6 +11,7 @@ import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
import java.util.List;
|
||||
@ -21,7 +22,7 @@ import rx.Observable;
|
||||
* Created by khy on 20/12/17.
|
||||
*/
|
||||
|
||||
public class ConcernQuestionsFragment extends ListFragment {
|
||||
public class ConcernQuestionsFragment extends ListFragment<Questions, NormalListViewModel> {
|
||||
|
||||
private ConcernQuestionsAdapter mAdapter;
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -25,7 +26,7 @@ import rx.Observable;
|
||||
* Created by khy on 20/12/17.
|
||||
*/
|
||||
|
||||
public class MyAnswerFragment extends ListFragment {
|
||||
public class MyAnswerFragment extends ListFragment<AnswerEntity, NormalListViewModel> {
|
||||
|
||||
private MyAnswerAdapter mAdapter;
|
||||
|
||||
|
||||
@ -16,6 +16,7 @@ import com.gh.gamecenter.ask.questionsdetail.AnswerEditFragment;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -27,7 +28,7 @@ import rx.Observable;
|
||||
* Created by khy on 20/12/17.
|
||||
*/
|
||||
|
||||
public class MyDraftFragment extends ListFragment {
|
||||
public class MyDraftFragment extends ListFragment<AnswerEntity, NormalListViewModel> {
|
||||
|
||||
private static final int ANSWER_DRAFT_REQUEST = 101;
|
||||
|
||||
|
||||
@ -11,6 +11,7 @@ import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -22,7 +23,7 @@ import rx.Observable;
|
||||
* Created by khy on 20/12/17.
|
||||
*/
|
||||
|
||||
public class MyQuestionsFragment extends ListFragment {
|
||||
public class MyQuestionsFragment extends ListFragment<Questions, NormalListViewModel> {
|
||||
|
||||
private MyQuestionsAdapter mAdapter;
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
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;
|
||||
@ -16,6 +15,7 @@ import com.gh.gamecenter.ask.entity.AnswerEntity;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
import java.util.List;
|
||||
@ -27,7 +27,7 @@ import rx.Observable;
|
||||
* Created by khy on 11/12/17.
|
||||
*/
|
||||
|
||||
public class AnswerFoldFragment extends ListFragment {
|
||||
public class AnswerFoldFragment extends ListFragment<AnswerEntity, NormalListViewModel> {
|
||||
|
||||
@BindView(R.id.ask_fold_hint)
|
||||
View mFlodHint;
|
||||
@ -66,7 +66,6 @@ public class AnswerFoldFragment extends ListFragment {
|
||||
return new VerticalItemDecoration(getContext(), 8, false);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getContext()).getApi().getQuestionsAnswer(mQuestionsId, true, offset);
|
||||
|
||||
@ -31,6 +31,7 @@ import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.LoadStatus;
|
||||
import com.google.android.flexbox.FlexboxLayout;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -61,15 +62,19 @@ public class QuestionsDetailAdapter extends ListAdapter<AnswerEntity> {
|
||||
|
||||
@Override
|
||||
protected void setListData(List<AnswerEntity> listData) {
|
||||
// todo 分页动画有问题
|
||||
if (listData != null && listData.size() != 0 && mQuestionsDetailEntity != null) {
|
||||
mEntityList.addAll(listData);
|
||||
notifyItemRangeInserted(mEntityList.size() - listData.size() + TOP_ITEM_COUNT, listData.size());
|
||||
// int oldDataSize = mEntityList == null ? 0 : mEntityList.size();
|
||||
mEntityList = new ArrayList<>(listData);
|
||||
notifyDataSetChanged();
|
||||
// notifyItemRangeInserted(oldDataSize + TOP_ITEM_COUNT, listData.size() - oldDataSize);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadChange(LoadStatus status) {
|
||||
switch (status) {
|
||||
case INIT_OVER:
|
||||
case LIST_OVER:
|
||||
mIsOver = true;
|
||||
break;
|
||||
|
||||
@ -30,6 +30,7 @@ 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.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
import com.lightgame.utils.Utils;
|
||||
@ -52,7 +53,7 @@ import rx.schedulers.Schedulers;
|
||||
* Created by khy on 7/12/17.
|
||||
*/
|
||||
|
||||
public class QuestionsDetailFragment extends ListFragment {
|
||||
public class QuestionsDetailFragment extends ListFragment<AnswerEntity, NormalListViewModel> {
|
||||
|
||||
@BindView(R.id.questionsdetail_answer)
|
||||
RelativeLayout mAnswer;
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
package com.gh.gamecenter.ask.questionsdetail;
|
||||
|
||||
import android.app.Application;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.gh.gamecenter.ask.entity.AnswerEntity;
|
||||
import com.gh.gamecenter.baselist.ListViewModel;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
/**
|
||||
* Created by khy on 20/03/18.
|
||||
*/
|
||||
|
||||
public class QuestionsDetailViewModel extends ListViewModel<AnswerEntity, AnswerEntity> {
|
||||
|
||||
public QuestionsDetailViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getApplication()).getApi().getQuestionsAnswer("", false, offset);
|
||||
}
|
||||
}
|
||||
@ -14,6 +14,7 @@ import com.gh.gamecenter.ask.entity.MeEntity;
|
||||
import com.gh.gamecenter.ask.entity.QuestionsDetailEntity;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
@ -35,7 +36,7 @@ import rx.schedulers.Schedulers;
|
||||
* Created by khy on 7/12/17.
|
||||
*/
|
||||
|
||||
public class QuestionsInviteFragment extends ListFragment {
|
||||
public class QuestionsInviteFragment extends ListFragment<InviteEntity, NormalListViewModel> {
|
||||
|
||||
private QuestionsDetailEntity mQuestionsDetailEntity;
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@ import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -35,7 +36,7 @@ import rx.Observable;
|
||||
* Created by khy on 8/12/17.
|
||||
*/
|
||||
|
||||
public class AskSearchFragment extends ListFragment {
|
||||
public class AskSearchFragment extends ListFragment<AskSearchEntity, NormalListViewModel> {
|
||||
|
||||
@BindView(R.id.list_ask_hint)
|
||||
View mSearchHint;
|
||||
|
||||
@ -95,19 +95,22 @@ public abstract class ListAdapter<DataType> extends BaseRecyclerAdapter {
|
||||
|
||||
public void loadChange(LoadStatus status) {
|
||||
switch (status) {
|
||||
case INIT_OVER:
|
||||
case LIST_OVER:
|
||||
mIsOver = true;
|
||||
mIsNetworkError = false;
|
||||
break;
|
||||
case LIST_FAILED:
|
||||
mIsOver = false;
|
||||
mIsNetworkError = true;
|
||||
break;
|
||||
case INIT:
|
||||
case LIST_LOADED:
|
||||
mIsNetworkError = false;
|
||||
break;
|
||||
case LIST_LOADING:
|
||||
if (!mIsNetworkError && !mIsOver) return;
|
||||
mIsNetworkError = false;
|
||||
mIsOver = false;
|
||||
return;
|
||||
break;
|
||||
case REFRESH:
|
||||
mEntityList.clear();
|
||||
notifyDataSetChanged();
|
||||
|
||||
@ -15,6 +15,7 @@ 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;
|
||||
@ -53,6 +54,16 @@ public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位
|
||||
return new VerticalItemDecoration(getContext(), 8, true);
|
||||
}
|
||||
|
||||
protected VM provideListViewModel() {
|
||||
Class<VM> viewModelClass = getViewModelClass();
|
||||
if (!NormalListViewModel.class.getName().equals(viewModelClass.getName())) {
|
||||
return ViewModelProviders.of(this).get(viewModelClass);
|
||||
} else {
|
||||
NormalListViewModel.Factory factory = new NormalListViewModel.Factory(HaloApp.getInstance().getApplication(), this);
|
||||
return ViewModelProviders.of(this, factory).get(viewModelClass);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.fragment_list_base;
|
||||
@ -61,6 +72,7 @@ public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位
|
||||
/**
|
||||
* 是否自动加载
|
||||
*/
|
||||
|
||||
protected boolean isAutomaticLoad() {
|
||||
return true;
|
||||
}
|
||||
@ -68,8 +80,7 @@ public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Class<VM> viewModelClass = getViewModelClass();
|
||||
mListViewModel = ViewModelProviders.of(this).get(viewModelClass);
|
||||
mListViewModel = provideListViewModel();
|
||||
mListViewModel.getObsListData().observe(this, this);
|
||||
mListViewModel.getLoadStatusLiveData().observe(this, o -> {
|
||||
if (o instanceof LoadStatus) {
|
||||
@ -81,10 +92,12 @@ public abstract class ListFragment<T, VM extends ListViewModel /* 该泛型位
|
||||
case INIT_FAILED:
|
||||
onLoadError();
|
||||
break;
|
||||
case INIT_OVER:
|
||||
case INIT_LOADED:
|
||||
onLoadDone();
|
||||
break;
|
||||
case INIT_OVER:
|
||||
onLoadDone();
|
||||
case INIT:
|
||||
case LIST_OVER:
|
||||
case LIST_LOADED:
|
||||
case LIST_FAILED:
|
||||
|
||||
@ -44,9 +44,9 @@ public class ListRepository<LD> {
|
||||
LoadStatus curStatus = mLoadStatusLiveData.getValue();
|
||||
|
||||
if (listObservable == null || curStatus != null &&
|
||||
curStatus != LoadStatus.INIT_LOADED &&
|
||||
curStatus != LoadStatus.LIST_LOADED &&
|
||||
curStatus != LoadStatus.INIT) return;
|
||||
curStatus != LoadStatus.INIT_LOADED &&
|
||||
curStatus != LoadStatus.LIST_LOADED &&
|
||||
curStatus != LoadStatus.INIT) return;
|
||||
|
||||
if (mCurLoadParams.getLoadOffset() == 0) {
|
||||
mLoadStatusLiveData.postValue(LoadStatus.INIT_LOADING);
|
||||
|
||||
@ -26,6 +26,12 @@ public abstract class ListViewModel<LD /*ListData*/, ID /*ItemData*/> extends An
|
||||
mListLiveData = mRepository.getResultLiveData();
|
||||
}
|
||||
|
||||
public ListViewModel(@NonNull Application application, ListRepository repository) {
|
||||
super(application);
|
||||
mRepository = repository;
|
||||
mListLiveData = mRepository.getResultLiveData();
|
||||
}
|
||||
|
||||
public LiveData<List<ID>> getObsListData() {
|
||||
return mResultLiveData;
|
||||
}
|
||||
|
||||
@ -1,29 +1,48 @@
|
||||
package com.gh.gamecenter.baselist;
|
||||
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.ViewModel;
|
||||
import android.arch.lifecycle.ViewModelProvider;
|
||||
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.
|
||||
* Created by khy on 21/03/18.
|
||||
*/
|
||||
|
||||
public class NormalListViewModel extends ListViewModel<AnswerEntity, AnswerEntity> {
|
||||
public class NormalListViewModel<T> extends ListViewModel<T, T> {
|
||||
|
||||
public NormalListViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
|
||||
public NormalListViewModel(@NonNull Application application, ListRepository repository) {
|
||||
super(application, repository);
|
||||
mResultLiveData.addSource(mListLiveData, mResultLiveData::postValue);
|
||||
}
|
||||
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public Observable<List<AnswerEntity>> provideDataObservable(int offset) {
|
||||
return RetrofitManager.getInstance(getApplication()).getApi().getAskHot(UserManager.getInstance().getCommunityId(getApplication()), offset);
|
||||
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 NormalListViewModel(mApplication, mRepository);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@ import com.gh.gamecenter.ask.entity.Questions;
|
||||
import com.gh.gamecenter.baselist.ListAdapter;
|
||||
import com.gh.gamecenter.baselist.ListFragment;
|
||||
import com.gh.gamecenter.baselist.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
import java.util.List;
|
||||
@ -24,7 +25,7 @@ import rx.Observable;
|
||||
* Created by khy on 22/12/17.
|
||||
*/
|
||||
|
||||
public class AnswerFragment extends ListFragment {
|
||||
public class AnswerFragment extends ListFragment<AnswerEntity, NormalListViewModel> {
|
||||
|
||||
private AnswerAdapter mAdapter;
|
||||
|
||||
|
||||
@ -16,8 +16,8 @@ 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.LoadType;
|
||||
import com.gh.gamecenter.baselist.NormalListViewModel;
|
||||
import com.gh.gamecenter.entity.MessageEntity;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
@ -30,7 +30,7 @@ import rx.Observable;
|
||||
* 消息-评论
|
||||
*/
|
||||
|
||||
public class MessageFragment extends ListFragment<MessageEntity,ListViewModel> {
|
||||
public class MessageFragment extends ListFragment<MessageEntity,NormalListViewModel> {
|
||||
|
||||
MessageFragmentAdapter mAdapter;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user