Files
assistant-android/app/src/main/java/com/gh/gamecenter/adapter/CommentDetailAdapter.java
2017-10-19 09:11:22 +08:00

163 lines
5.4 KiB
Java

package com.gh.gamecenter.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.ViewGroup;
import com.gh.common.constant.ItemViewType;
import com.gh.common.util.CommentUtils;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.CommentViewHolder;
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
import com.gh.gamecenter.entity.CommentEntity;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.lightgame.adapter.BaseRecyclerAdapter;
import com.lightgame.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import retrofit2.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* Created by khy on 2017/3/22.
*/
public class CommentDetailAdapter extends BaseRecyclerAdapter<ViewHolder> {
private List<CommentEntity> mCommentList;
private String mCommentId;
private boolean mIsOver;
private boolean mIsLoading;
private boolean mIsNetworkError;
public CommentDetailAdapter(Context context, String commentId) {
super(context);
mCommentId = commentId;
mCommentList = new ArrayList<>();
loadData(0);
}
public void loadData(int offset) {
mIsLoading = true;
RetrofitManager.getInstance(mContext).getApi()
.getCommentTrace(mCommentId, 20, offset)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<CommentEntity>>() {
@Override
public void onResponse(List<CommentEntity> response) {
super.onResponse(response);
mCommentList.addAll(response);
if (response.size() < 20) {
mIsOver = true;
}
mIsLoading = false;
notifyItemRangeChanged(0, getItemCount() - 1);
}
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
Utils.toast(mContext, mContext.getString(R.string.comment_failure_hint));
mIsNetworkError = true;
mIsLoading = false;
notifyItemChanged(getItemCount() - 1);
}
});
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ItemViewType.LOADING) {
View view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false);
return new FooterViewHolder(view);
} else {
View view = mLayoutInflater.inflate(R.layout.comment_item, parent, false);
return new CommentViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof CommentViewHolder) {
initCommentViewHolder((CommentViewHolder) holder, position);
} else if (holder instanceof FooterViewHolder) {
initFooterViewHolder((FooterViewHolder) holder);
}
}
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1) {
return ItemViewType.LOADING;
}
return 100;
}
@Override
public int getItemCount() {
return mCommentList.size() + 1;
}
private void initCommentViewHolder(final CommentViewHolder holder, int position) {
final CommentEntity commentEntity = mCommentList.get(position);
holder.commentLine.setVisibility(View.GONE);
holder.commentLineBottom.setVisibility(View.VISIBLE);
CommentUtils.setCommentUserView(mContext, holder, commentEntity);
CommentUtils.setCommentTime(holder.commentTimeTv, commentEntity.getTime());
if (commentEntity.getParent() != null) {
holder.commentContentTv.setText("@dsdasd" + commentEntity.getParent().getUser().getName() + ": " + commentEntity.getContent());
} else {
holder.commentContentTv.setText(commentEntity.getContent());
}
holder.commentLikeIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CommentUtils.postVote(mContext, commentEntity, holder.commentLikeCountTv, holder.commentLikeIv, null);
}
});
}
private void initFooterViewHolder(FooterViewHolder holder) {
if (mIsNetworkError) {
holder.loading.setVisibility(View.GONE);
holder.hint.setText(R.string.loading_error_network);
} else if (!mIsOver) {
holder.hint.setText(R.string.loading);
holder.loading.setVisibility(View.VISIBLE);
} else if (mCommentList.size() == 0) {
holder.loading.setVisibility(View.GONE);
holder.hint.setText(R.string.comment_empty);
} else {
holder.hint.setText(R.string.comment_nomore);
holder.loading.setVisibility(View.GONE);
}
}
public boolean isOver() {
return mIsOver;
}
public boolean isLoading() {
return mIsLoading;
}
}