159 lines
6.4 KiB
Java
159 lines
6.4 KiB
Java
package com.gh.gamecenter.collection;
|
|
|
|
import android.content.Context;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
|
|
import com.gh.base.OnListClickListener;
|
|
import com.gh.common.constant.ItemViewType;
|
|
import com.gh.common.syncpage.ISyncAdapterHandler;
|
|
import com.gh.common.util.CollectionUtils;
|
|
import com.gh.common.util.DialogHelper;
|
|
import com.gh.common.util.DialogUtils;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
|
|
import com.gh.gamecenter.baselist.ListAdapter;
|
|
import com.gh.gamecenter.baselist.LoadType;
|
|
import com.gh.gamecenter.databinding.CommunityAnswerItemBinding;
|
|
import com.gh.gamecenter.qa.answer.CommunityAnswerItemViewHolder;
|
|
import com.gh.gamecenter.qa.answer.detail.AnswerDetailActivity;
|
|
import com.gh.gamecenter.qa.entity.AnswerEntity;
|
|
import com.gh.gamecenter.qa.entity.Questions;
|
|
import com.gh.gamecenter.qa.questions.detail.QuestionsDetailActivity;
|
|
import com.lightgame.utils.Utils;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
import kotlin.Pair;
|
|
|
|
/**
|
|
* Created by khy on 22/12/17.
|
|
*/
|
|
|
|
public class AnswerAdapter extends ListAdapter<AnswerEntity> implements ISyncAdapterHandler {
|
|
|
|
private OnListClickListener mListClickListener;
|
|
|
|
private AnswerViewModel mListViewModel;
|
|
|
|
private String mEntrance;
|
|
|
|
public AnswerAdapter(Context context, AnswerViewModel viewModel, OnListClickListener listClickListener, String entrance) {
|
|
super(context);
|
|
mListViewModel = viewModel;
|
|
mListClickListener = listClickListener;
|
|
mEntrance = entrance;
|
|
}
|
|
|
|
@Override
|
|
public int getItemViewType(int position) {
|
|
if (position == getItemCount() - 1) return ItemViewType.ITEM_FOOTER;
|
|
return ItemViewType.ITEM_BODY;
|
|
}
|
|
|
|
@Override
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
View view;
|
|
switch (viewType) {
|
|
case ItemViewType.ITEM_FOOTER:
|
|
view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false);
|
|
return new FooterViewHolder(view, mListClickListener);
|
|
case ItemViewType.ITEM_BODY:
|
|
view = mLayoutInflater.inflate(R.layout.community_answer_item, parent, false);
|
|
return new CommunityAnswerItemViewHolder(CommunityAnswerItemBinding.bind(view));
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
|
switch (getItemViewType(position)) {
|
|
case ItemViewType.ITEM_BODY:
|
|
CommunityAnswerItemViewHolder viewHolder = (CommunityAnswerItemViewHolder) holder;
|
|
AnswerEntity entity = mEntityList.get(position);
|
|
String path;
|
|
if (AnswerFragment.COLLECTION.equals(mListViewModel.getType())) {
|
|
path = "我的收藏-回答列表";
|
|
} else if (AnswerFragment.HISTORY.equals(mListViewModel.getType())) {
|
|
path = "浏览记录-回答列表";
|
|
viewHolder.itemView.setOnLongClickListener(v -> {
|
|
DialogHelper.showDialog(holder.itemView.getContext(),
|
|
"删除记录",
|
|
"删除浏览记录将不可恢复,确定删除吗?",
|
|
"确定",
|
|
"取消",
|
|
() -> {
|
|
mListViewModel.removeHistory(entity);
|
|
},
|
|
() -> {
|
|
},
|
|
false, "", "");
|
|
return false;
|
|
});
|
|
} else {
|
|
path = "插入回答-收藏回答列表";
|
|
}
|
|
viewHolder.bindAnswerItem(entity, mEntrance, path);
|
|
|
|
holder.itemView.setOnClickListener(v -> {
|
|
if (entity.getActive()) {
|
|
mContext.startActivity(AnswerDetailActivity.getIntent(mContext, entity.getId(), mEntrance, path));
|
|
} else {
|
|
showDeleteDialog(entity.getId());
|
|
}
|
|
|
|
if (!entity.getRead()) {
|
|
entity.setRead(true);
|
|
notifyItemChanged(position);
|
|
mListViewModel.postCollectionAnswerRead(entity.getId());
|
|
}
|
|
});
|
|
viewHolder.getBinding().title.setOnClickListener(v -> {
|
|
Questions questions = entity.getQuestions();
|
|
mContext.startActivity(QuestionsDetailActivity.getIntent(mContext, questions.getId(), mEntrance, path));
|
|
});
|
|
break;
|
|
case ItemViewType.ITEM_FOOTER:
|
|
FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
|
|
footerViewHolder.initItemPadding();
|
|
footerViewHolder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver, R.string.ask_loadover_hint);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void showDeleteDialog(String answerId) {
|
|
DialogUtils.showCancelAlertDialog(mContext, "提示"
|
|
, "内容已被删除,是否取消收藏?"
|
|
, "取消收藏", "暂不"
|
|
, () -> CollectionUtils.INSTANCE.deleteCollection(mContext, answerId
|
|
, CollectionUtils.CollectionType.answer, new CollectionUtils.OnCollectionListener() {
|
|
@Override
|
|
public void onSuccess() {
|
|
Utils.toast(mContext, R.string.collection_cancel);
|
|
mListViewModel.load(LoadType.REFRESH);
|
|
}
|
|
|
|
@Override
|
|
public void onError() {
|
|
Utils.toast(mContext, R.string.collection_cancel_failure);
|
|
}
|
|
}), null);
|
|
}
|
|
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return mEntityList == null || mEntityList.isEmpty() ? 0 : mEntityList.size() + FOOTER_ITEM_COUNT;
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public Pair<String, Object> getSyncData(int position) {
|
|
if (position >= mEntityList.size()) return null;
|
|
AnswerEntity entity = mEntityList.get(position);
|
|
return new Pair(entity.getId(), entity);
|
|
}
|
|
}
|