|
package com.electric.chargingpile.view;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.electric.chargingpile.R;
import com.electric.chargingpile.activity.LoginActivity;
import com.electric.chargingpile.adapter.VideoCommentAdapter;
import com.electric.chargingpile.application.MainApplication;
import com.electric.chargingpile.data.TalkRecommendBean;
import com.electric.chargingpile.data.VideoCommentBean;
import com.electric.chargingpile.util.JsonUtils;
import com.electric.chargingpile.util.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
public class VideoCommentDialog extends Dialog implements View.OnClickListener {
private Context context;
private TextView showCommentTitle;
private View showCommentClose;
private RecyclerView showCommentLv;
private EditText showCommentEd;
private TextView showCommentPublish, show_comment_no;
private TalkRecommendBean talkRecommendBean;
private LinearLayout show_comment_progress_ll, show_comment_network_ll;
private VideoCommentAdapter videoCommentAdapter;
public VideoCommentDialog(Context context, TalkRecommendBean talkRecommendBean) {
super(context, R.style.bottomDialogStyle);
this.context = context;
this.talkRecommendBean = talkRecommendBean;
imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View inflate = LayoutInflater.from(context).inflate(R.layout.view_show_comment, null);
initView(inflate);
setContentView(inflate);
Window window = this.getWindow();
window.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(params);
}
public void getData() {
setComment(talkRecommendBean.targetType, talkRecommendBean.targetId, 0);
}
public void setComment(int targetType, long targetId, final int commentId) {
String url = MainApplication.urlNew + "/topic/comments/list.do";
Map<String, String> map = new HashMap<>();
map.put("targetType", targetType + "");
map.put("targetId", targetId + "");
map.put("flag", commentId + "");
if (MainApplication.isLogin()) {
map.put("user_id", MainApplication.userId);
}
OkHttpUtils
.get()
.params(map)
.url(url)
.build()
.connTimeOut(6000)
.readTimeOut(6000)
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
show_comment_progress_ll.setVisibility(View.GONE);
show_comment_network_ll.setVisibility(View.VISIBLE);
}
@Override
public void onResponse(String response) {
String rtnCode = JsonUtils.getKeyResult(response, "code");
if ("1000".equals(rtnCode)) {
String rtnMsg = JsonUtils.getKeyResult(response, "data");
ArrayList<VideoCommentBean> qadata = (ArrayList<VideoCommentBean>) JsonUtils.parseToObjectList(rtnMsg, VideoCommentBean.class);
if (videoCommentAdapter != null) {
if (commentId == 0) {
videoCommentAdapter.cleanData();
}
if (qadata.size() == 0) {
show_comment_no.setVisibility(View.VISIBLE);
} else {
videoCommentAdapter.addData(qadata);
}
}
if(videoCommentAdapter.getAllCount()>0)
showCommentTitle.setText("全部评论 " + videoCommentAdapter.getAllCount());
}
show_comment_progress_ll.setVisibility(View.GONE);
}
});
}
private void initView(View convertView) {
show_comment_no = convertView.findViewById(R.id.show_comment_no);
show_comment_progress_ll = convertView.findViewById(R.id.show_comment_progress_ll);
show_comment_network_ll = convertView.findViewById(R.id.show_comment_network_ll);
showCommentTitle = convertView.findViewById(R.id.show_comment_title);
showCommentClose = convertView.findViewById(R.id.show_comment_close);
showCommentLv = convertView.findViewById(R.id.show_comment_lv);
showCommentEd = convertView.findViewById(R.id.show_comment_ed);
showCommentPublish = convertView.findViewById(R.id.show_comment_publish);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
showCommentLv.setLayoutManager(linearLayoutManager);
convertView.findViewById(R.id.show_comment_network_retry).setOnClickListener(this);
show_comment_progress_ll.setVisibility(View.VISIBLE);
showCommentEd.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
int i2 = editable.length();
if(i2>200){
Toast.makeText(context, "评论最多200字,请删减后再发布", Toast.LENGTH_SHORT).show();
showCommentEd.setTextColor(Color.parseColor("#E90A0A"));
}else {
showCommentEd.setTextColor(Color.parseColor("#ffffff"));
}
if (i2 > 0) {
showCommentPublish.setTextColor(Color.parseColor("#00EDB2"));
} else {
showCommentPublish.setTextColor(Color.parseColor("#7E7E7E"));
}
}
});
videoCommentAdapter = new VideoCommentAdapter(context);
showCommentLv.setAdapter(videoCommentAdapter);
showCommentClose.setOnClickListener(this);
showCommentPublish.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.show_comment_close:
dismiss();
break;
case R.id.show_comment_publish:
if (MainApplication.isLogin()) {
String commentCon = showCommentEd.getText().toString().trim();
if (TextUtils.isEmpty(commentCon)) {
Toast.makeText(context, "请输入评论内容!", Toast.LENGTH_SHORT).show();
return;
}
publishComment(commentCon, talkRecommendBean.targetId, talkRecommendBean.targetType,
"0", "0", 1, "0");
}else {
Toast.makeText(context, "请登录!", Toast.LENGTH_SHORT).show();
context.startActivity(new Intent(context, LoginActivity.class));
}
break;
case R.id.show_comment_network_retry:
show_comment_progress_ll.setVisibility(View.VISIBLE);
show_comment_network_ll.setVisibility(View.GONE);
setComment(talkRecommendBean.targetType, talkRecommendBean.targetId, 0);
break;
default:
break;
}
}
private void publishComment(String commentCon, long targetId, int targetType, String toUserId,
String parentId, int level, String toCommentIds) {
String url = MainApplication.urlNew + "/topic/comments/publish.do";
Map<String, String> map = new HashMap<>();
map.put("targetType", targetType + "");
map.put("targetId", targetId + "");
map.put("toUserId", toUserId);
map.put("parentId", parentId);
map.put("level", level + "");
map.put("toCommentsId", toCommentIds);
map.put("content", commentCon);
if (MainApplication.isLogin()) {
map.put("userId", MainApplication.userId);
}
OkHttpUtils
.post()
.params(map)
.url(url)
.build()
.connTimeOut(6000)
.readTimeOut(6000)
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e) {
ToastUtil.showToast(context, "请求失败,请重试", Toast.LENGTH_SHORT);
}
@Override
public void onResponse(String response) {
String rtnCode = JsonUtils.getKeyResult(response, "code");
if ("1000".equals(rtnCode)) {
String rtnMsg = JsonUtils.getKeyResult(response, "data");
VideoCommentBean qadata = JsonUtils.parseToObjectBean(rtnMsg, VideoCommentBean.class);
ToastUtil.showToast(context, "评论成功!", Toast.LENGTH_SHORT);
showCommentPublish.setText("");
hideKeyboard(showCommentPublish);
if (videoCommentAdapter != null) {
videoCommentAdapter.addDataSingle(qadata);
int count = videoCommentAdapter.getItemCount();
if (count > 0) {
showCommentTitle.setText("全部评论 " + count);
show_comment_no.setVisibility(View.GONE);
}else
showCommentTitle.setText("全部评论 " + 0);
}
}else{
String rtnMsg = JsonUtils.getKeyResult(response, "desc");
ToastUtil.showToast(context, rtnMsg, Toast.LENGTH_SHORT);
}
}
});
}
InputMethodManager imm;
public boolean hideKeyboard(View v) {
if (imm.isActive(v)) {
//因为是在fragment下,所以用了getView()获取view,也可以用findViewById()来获取父控件
getCurrentFocus().requestFocus();//强制获取焦点,不然getActivity().getCurrentFocus().getWindowToken()会报错
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
imm.restartInput(v);
return true;
}
return false;
}
}
|