admin
2021-03-29 405e8b2d2ad9a2d6d51cd65173b42c9fcde0ce4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package com.weikou.beibeivideo.ui.mine;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
 
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.lcjian.library.util.common.DateUtils;
import com.lcjian.library.util.common.StringUtils;
import com.lcjian.library.util.glide.GlideCircleTransform;
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
import com.weikou.beibeivideo.BeibeiVideoAPI;
import com.weikou.beibeivideo.R;
import com.weikou.beibeivideo.entity.CommentReply;
import com.weikou.beibeivideo.ui.media.VideoDetailActivity2;
 
import org.json.JSONObject;
 
import java.util.Date;
import java.util.List;
 
import de.greenrobot.event.EventBus;
 
public class CommentMessageAdapter extends BaseAdapter {
 
    private Context context;
    private List<CommentReply> list;
    private LayoutInflater inflater;
    private RequestManager glideRequest;
 
    public CommentMessageAdapter(Context context, List<CommentReply> list) {
        this.list = list;
        this.context = context;
        glideRequest = Glide.with(context);
    }
 
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }
 
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // 初始化控件
        inflater = LayoutInflater.from(parent.getContext());
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.comment_message_item, null);
            holder.iv_portrait = (ImageView) view
                    .findViewById(R.id.iv_comment_msg_portrait);
            holder.tv_nickName = (TextView) view
                    .findViewById(R.id.tv_msg_nickname);
            holder.tv_my_comment = (TextView) view
                    .findViewById(R.id.tv_my_comment);
            holder.tv_from_nickName = (TextView) view
                    .findViewById(R.id.tv_comment_from_who);
            holder.tv_to_my_comment = (TextView) view
                    .findViewById(R.id.tv_comment_from_context);
            holder.tv_TV_and_drama = (TextView) view
                    .findViewById(R.id.tv_comment_link);
            holder.tv_reply_time = (TextView) view
                    .findViewById(R.id.tv_comment_time);
            holder.tv_reply = (TextView) view
                    .findViewById(R.id.tv_comment_reply);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // 为控件赋值
        CommentReply info = (CommentReply) getItem(position);
        // 添加头像
        try {
            glideRequest.load(info.getUser().getPortrait())
                    .placeholder(R.drawable.ic_default)
                    .error(R.drawable.ic_default)
                    .transform(new GlideCircleTransform(context))
                    .into(holder.iv_portrait);// 回复者头像
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        holder.tv_nickName.setText(StringUtils.isEmpty(info.getUser()
                .getNickname()) ? "" : info.getUser().getNickname());// 回复者昵称
        holder.tv_my_comment
                .setText(StringUtils.isEmpty(info.getContent()) ? "" : info
                        .getContent());// 回复类容
 
        holder.tv_from_nickName
                .setText(info.getParent() == null ? StringUtils.isEmpty(info
                        .getComment().getUser().getNickname()) ? "" : "回复@"
                        + info.getComment().getUser().getNickname()
                        : StringUtils.isEmpty(info.getParent().getComment()
                        .getUser().getNickname()) ? "" : "回复@"
                        + info.getParent().getComment().getUser()
                        .getNickname());// 我的昵称
        holder.tv_to_my_comment.setText(info.getParent() == null ? StringUtils
                .isEmpty(info.getComment().getContent()) ? "" : info
                .getComment().getContent() : StringUtils.isEmpty(info
                .getParent().getComment().getContent()) ? "" : info.getParent()
                .getComment().getContent());// 对我哪条类容的回复
 
        holder.tv_TV_and_drama.setText(StringUtils.isEmpty(info.getComment()
                .getVideo().getName()) ? "" : "来源:"
                + info.getComment().getVideo().getName());
 
        holder.tv_reply.setText("回复");
        holder.tv_reply_time
                .setText(StringUtils.isEmpty(info.getCreatetime()) ? ""
                        : DateUtils.getRelativeTimeStr(new Date(Long
                        .parseLong(info.getCreatetime()))));
        final CommentReply mInfo = info;
        holder.tv_TV_and_drama.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(arg0.getContext(), VideoDetailActivity2.class);
                intent.putExtra("video_info", mInfo.getComment().getVideo());
                intent.putExtra("from", "commentMsg");
                arg0.getContext().startActivity(intent);
            }
        });
        final View tv_mReply = holder.tv_reply;
        holder.tv_reply.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                VideoReviewPopupWindow popupwindow = new VideoReviewPopupWindow(
                        arg0.getContext(), mInfo);
                popupwindow.showPopupWindow(tv_mReply);
            }
        });
        return view;
    }
 
    private static class ViewHolder {
        ImageView iv_portrait;// 我的头像
        TextView tv_nickName;// 我的昵称
        TextView tv_my_comment;// 我发表的评论
        TextView tv_from_nickName;// 谁对我的回复
        TextView tv_to_my_comment;// 对我回复的类容
        TextView tv_TV_and_drama;// 来自哪一部电视剧或者电影
        TextView tv_reply_time;// 回复我的时间
        TextView tv_reply;// 回复按钮
    }
 
    /**
     * 自定义popupWindow
     *
     * @author weikou2015
     */
    public class VideoReviewPopupWindow extends PopupWindow {
        private LayoutInflater mInflater;
        private View conentView;
        private EditText tv_review_edit;
        private Button tv_review_deliver;
        private Context mContext;
        private CommentReply info;
 
        public VideoReviewPopupWindow(Context context, CommentReply info) {
            this.mContext = context;
            this.info = info;
            mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            SharedPreferences preferences = context.getSharedPreferences(
                    "user", Context.MODE_PRIVATE);
            isLogin = StringUtils
                    .isEmpty(preferences.getString("LoginUid", "")) ? false
                    : true;
            conentView = mInflater.inflate(R.layout.video_review_popupwindow,
                    null);
            this.setContentView(conentView);
 
            this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            // 软键盘不会挡着popupwindow
            this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            this.setWidth(LayoutParams.MATCH_PARENT);
            this.setHeight(LayoutParams.WRAP_CONTENT);
 
            this.setFocusable(true);
            this.setOutsideTouchable(false);
            ColorDrawable dw = new ColorDrawable(0000000000);
            this.setBackgroundDrawable(dw);
            this.update();
 
            tv_review_edit = (EditText) conentView
                    .findViewById(R.id.tv_review_edit);
            tv_review_deliver = (Button) conentView
                    .findViewById(R.id.tv_review_deliver);
 
            tv_review_deliver.setOnClickListener(new OnClickListener() {
 
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (isLogin) {
                        setDeliver();
                    }
                }
            });
 
            tv_review_edit
                    .setOnFocusChangeListener(new OnFocusChangeListener() {
 
                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {
                            if (hasFocus) {
                                setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                            }
                        }
                    });
 
        }
 
        /**
         * 显示popupWindow
         *
         * @param parent
         */
        public void showPopupWindow(View parent) {
            if (!this.isShowing()) {
                // this.showAsDropDown(parent, 0, 0, Gravity.BOTTOM);
                this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
                openKeyboard(new Handler(), 200);
            } else {
                this.dismiss();
            }
        }
 
        /**
         * 打开软键盘
         */
        private void openKeyboard(Handler mHandler, int s) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm = (InputMethodManager) mContext
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }, s);
        }
 
        /**
         * 是否已经登录
         */
        private boolean isLogin = true;// true为已经登录
 
        /**
         * 发布回复内容
         */
        private void setDeliver() {
            SharedPreferences preferences = mContext.getSharedPreferences(
                    "user", Context.MODE_PRIVATE);
            BeibeiVideoAPI.replayComment(mContext, preferences.getString(
                    "LoginUid", ""), info.getId(), info.getComment().getId(),
                    tv_review_edit.getEditableText().toString(),
                    new BasicTextHttpResponseHandler() {
 
                        @Override
                        public void onSuccessPerfect(int statusCode,
                                                     org.apache.http.Header[] headers,
                                                     JSONObject jsonObject) throws Exception {
                            if (jsonObject.optBoolean("IsPost")) {
                                EventBus.getDefault().post(1);
                            }
                        }
                    });
        }
    }
}