an>
        dialog.findViewById(R.id.bWriteComment).setOnClickListener(this);
155
        bPublish.setOnClickListener(this);
129 156
    }
130 157
131 158
    @Override
@ -148,26 +175,86 @@ public class ChatContentCommentListDialogFragment extends DialogFragment impleme
148 175
149 176
                    @Override
150 177
                    public void publish() {
178
                        if (bCommentContent.getText().toString().length() > 0) {
179
                            publishComment();
180
                        }
151 181
                    }
152 182
                });
153 183
                break;
184
            case R.id.bPublish:
185
                if (bCommentContent.getText().toString().length() > 0) {
186
                    publishComment();
187
                }
188
                break;
154 189
            case R.id.allCommentsClose:
155 190
                dialog.dismiss();
156 191
                break;
157 192
        }
158 193
    }
159 194
195
    private void publishComment() {
196
        if (!MainApplication.isLogin()) {
197
            startActivity(new Intent(getActivity().getApplicationContext(), LoginActivity.class));
198
            ToastUtil.showToast(getActivity().getApplicationContext(), "请先登录", Toast.LENGTH_SHORT);
199
            return;
200
        }
201
202
        String url = MainApplication.urlNew + "/topic/comments/publish.do";
203
        final Map<String, String> map = new HashMap<>();
204
        map.put("targetId", chatRecommendBean.targetId + "");
205
        map.put("targetType", chatRecommendBean.targetType + "");
206
        map.put("userId", MainApplication.userId);
207
        map.put("toUserId", chatRecommendBean.addUserId + "");
208
        map.put("level", "1");
209
        map.put("content", bCommentContent.getText().toString());
210
211
        CommonParams.addCommonParams(map);
212
213
        OkHttpUtils.post().params(map).url(url).build().connTimeOut(6000).readTimeOut(6000).execute(new StringCallback() {
214
            @Override
215
            public void onError(Call call, Exception e) {
216
                ToastUtil.showToast(getContext(), e.getMessage(), Toast.LENGTH_SHORT);
217
            }
218
219
            @Override
220
            public void onResponse(String response) {
221
                String code = JsonUtils.getKeyResult(response, "code");
222
                String desc = JsonUtils.getKeyResult(response, "desc");
223
                if ("1000".equals(code)) {
224
                    commentAdapter.clearData();
225
                    requestData();
226
                    chatRecommendBean.commentNums++;
227
                    allCommentTitle.setText("全部评论 " + chatRecommendBean.commentNums);
228
                    if (chatContentCommentListDialogFragmentListener != null) {
229
                        chatContentCommentListDialogFragmentListener.updateCommentNum(chatRecommendBean.commentNums);
230
                    }
231
                } else {
232
                    ToastUtil.showToast(getContext(), desc, Toast.LENGTH_SHORT);
233
                }
234
            }
235
        });
236
    }
237
238
160 239
    private void requestData() {
240
        List<CommentBean> beans = commentAdapter.getCurrentData();
161 241
        String url = MainApplication.urlNew + "/topic/comments/list.do";
162 242
        Map<String, String> map = new HashMap<>();
163
        map.put("page", page + "");
164 243
        map.put("limit", "20");
165
        map.put("targetId", bean.targetId + "");
166
        map.put("targetType", bean.targetType + "");
244
        map.put("targetId", chatRecommendBean.targetId + "");
245
        map.put("targetType", chatRecommendBean.targetType + "");
246
        if (beans.size() == 0) {
247
            page = 1;
248
        } else {
249
            page = beans.size() / 10 + beans.size() % 10 == 0 ? 0 : 1;
250
        }
251
        map.put("page", page + "");
252
167 253
        if (page == 1) {
168 254
            map.put("commentsId", "0");
169 255
        } else {
170
            // TODO
256
            CommentBean bean = beans.get(beans.size() - 1);
257
            map.put("commentsId", bean.commentsId + "");
171 258
        }
172 259
173 260
        CommonParams.addCommonParams(map);
@ -187,69 +274,316 @@ public class ChatContentCommentListDialogFragment extends DialogFragment impleme
187 274
188 275
            @Override
189 276
            public void onResponse(String s) {
277
                Log.d(TAG, "onResponse: " + s);
190 278
                hideLoading();
191 279
                String code = JsonUtils.getKeyResult(s, "code");
192
                Log.d(TAG, "onResponse: " + s);
193 280
                if ("1000".equals(code)) {
194 281
                    String data = JsonUtils.getKeyResult(s, "data");
195 282
                    List<CommentBean> beans = (List<CommentBean>) JsonUtils.parseToObjectList(data, CommentBean.class);
283
196 284
                    if (page == 1) {
197
                        xRefreshView.setLoadComplete(false);
198
//                        userVo = userHomePageBean.userVo;
199
//                        updateTopView();
200
//                        if (userHomePageBean.list.size() == 0) {
201
//                            zeroTopicNum.setVisibility(View.VISIBLE);
202
//                            xRefreshView.setLoadComplete(true);
203
//                        } else {
204
//                            zeroTopicNum.setVisibility(View.GONE);
205
//                            userPageAdapter.setContentData(userHomePageBean.list);
206
//                        }
207
//                        userPageAdapter.setFooterData(false);
208
                        xRefreshView.stopRefresh();
285
                        if (beans.size() == 0) {
286
                            noDataView.setVisibility(View.VISIBLE);
287
                        } else {
288
                            noDataView.setVisibility(View.GONE);
289
                            commentAdapter.setData(beans);
290
                        }
209 291
                    } else {
210
//                        if (userHomePageBean.list.size() == 0) {
211
//                            xRefreshView.setLoadComplete(true);
212
////                            userPageAdapter.setFooterData(true);
213
//                        } else {
214
//                            userPageAdapter.addContentData(userHomePageBean.list);
215
//                            userPageAdapter.setFooterData(false);
216
//                        }
217
                        xRefreshView.stopLoadMore();
292
                        if (beans.size() > 0) {
293
                            commentAdapter.addData(beans);
294
                        }
295
                    }
296
297
                    if (beans.size() < 20) {
298
                        xRefreshView.setLoadComplete(true);
218 299
                    }
300
                    xRefreshView.stopLoadMore();
301
219 302
                } else {
220 303
                    String desc = JsonUtils.getKeyResult(s, "desc");
221 304
                    ToastUtil.showToast(getContext(), desc, Toast.LENGTH_SHORT);
222
                    if (page == 1) {
223
                        xRefreshView.stopRefresh();
224
                    } else {
225
                        page--;
226
                        xRefreshView.stopLoadMore();
227
                    }
305
                    xRefreshView.stopLoadMore();
228 306
                }
229 307
230 308
            }
231 309
        });
232 310
    }
233 311
234
    private class CommentHolder extends RecyclerView.ViewHolder {
312
    private class CommentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
235 313
        private CommentBean bean;
314
        private int position;
236 315
        private TextView content;
316
        private ImageView headImage;
317
        private TextView nickName;
318
        private TextView addDate;
319
        private ImageView likeIcon;
320
        private TextView likeLabel;
321
        private ConstraintLayout likeView;
322
        private ConstraintLayout replyInfo;
323
        private TextView replyLabel;
324
        private ConstraintLayout replyAndDeleteView;
325
        private TextView deleteComment;
237 326
238 327
        public CommentHolder(LayoutInflater layoutInflater, ViewGroup parent) {
239 328
            super(layoutInflater.inflate(R.layout.list_item_comment, parent, false));
240 329
241 330
            initView();
331
            initListener();
242 332
        }
243 333
244 334
        private void initView() {
245 335
            content = itemView.findViewById(R.id.content);
336
            headImage = itemView.findViewById(R.id.headImage);
337
            nickName = itemView.findViewById(R.id.nickName);
338
            addDate = itemView.findViewById(R.id.addDate);
339
            likeIcon = itemView.findViewById(R.id.likeIcon);
340
            likeLabel = itemView.findViewById(R.id.likeLabel);
341
            likeView = itemView.findViewById(R.id.likeView);
342
            replyAndDeleteView = itemView.findViewById(R.id.replyAndDeleteView);
343
            replyInfo = itemView.findViewById(R.id.replyInfo);
344
            replyLabel = itemView.findViewById(R.id.replyLabel);
345
            deleteComment = itemView.findViewById(R.id.deleteComment);
346
347
        }
348
349
        private void initListener() {
350
            likeView.setOnClickListener(this);
351
            replyInfo.setOnClickListener(this);
352
            deleteComment.setOnClickListener(this);
353
        }
354
355
        @Override
356
        public void onClick(View view) {
357
            switch (view.getId()) {
358
                case R.id.likeView:
359
                    doLike();
360
                    break;
361
                case R.id.replyInfo: //  点击查看回复列表
362
                    break;
363
                case R.id.deleteComment: // 删除评论操作
364
                    if (MainApplication.isLogin()) {
365
                        loginSign();
366
                    } else {
367
                        startActivity(new Intent(getActivity().getApplicationContext(), LoginActivity.class));
368
                        ToastUtil.showToast(getActivity().getApplicationContext(), "请先登录", Toast.LENGTH_SHORT);
369
                    }
370
                    break;
371
            }
246 372
        }
247 373
248
        private void bind(CommentBean bean) {
374
        private void bindView(CommentBean bean, int position) {
249 375
            this.bean = bean;
376
            this.position = position;
250 377
            content.setText(bean.content);
378
            nickName.setText(bean.nickNameFromUser);
379
            addDate.setText(bean.addDate);
380
            if (bean.likeFlg == 1) {
381
                likeIcon.setImageResource(R.drawable.like_red_icon);
382
            } else {
383
                likeIcon.setImageResource(R.drawable.like_small_icon);
384
            }
385
            if (bean.likeNums > 0) {
386
                likeLabel.setText(bean.likeNums > 999 ? "999+" : bean.likeNums + "");
387
            } else {
388
                likeLabel.setText("");
389
            }
390
391
            if (bean.replyNums > 0) {
392
                replyInfo.setVisibility(View.VISIBLE);
393
                if (MainApplication.userId.equals(bean.userId + "")) {
394
                    replyAndDeleteView.setVisibility(View.VISIBLE);
395
                } else {
396
                    replyAndDeleteView.setVisibility(View.GONE);
397
                    replyAndDeleteView.setVisibility(View.VISIBLE);
398
                }
399
            } else {
400
                replyInfo.setVisibility(View.GONE);
401
                replyLabel.setText(bean.replyNums + "");
402
                if (MainApplication.userId.equals(bean.userId + "")) {
403
                    replyAndDeleteView.setVisibility(View.VISIBLE);
404
                    deleteComment.setVisibility(View.VISIBLE);
405
                } else {
406
                    replyAndDeleteView.setVisibility(View.GONE);
407
                    deleteComment.setVisibility(View.GONE);
408
                }
409
            }
410
411
            if (TextUtils.isEmpty(bean.headImgFromUser)) {
412
                Picasso.with(getContext())
413
                        .load(R.drawable.icon_face2_0)
414
                        .placeholder(R.drawable.icon_face2_0)
415
                        .error(R.drawable.icon_face2_0)
416
                        .transform(new CircleTransform())
417
                        .into(headImage);
418
            } else {
419
                Picasso.with(getContext())
420
                        .load("http://cdz.evcharge.cc/zhannew/uploadfile/" + bean.headImgFromUser)
421
                        .placeholder(R.drawable.icon_face2_0)
422
                        .error(R.drawable.icon_face2_0)
423
                        .transform(new CircleTransform())
424
                        .into(headImage);
425
            }
426
        }
427
428
        /**
429
         * 点赞操作
430
         */
431
        private void doLike() {
432
            String url = MainApplication.urlNew + "/topic/like.do";
433
            Map<String, String> map = new HashMap<>();
434
            map.put("targetId", bean.id + "");
435
            map.put("targetType", "26");
436
            map.put("flag", "1");
437
            map.put("authorId", bean.userId + "");
438
            CommonParams.addCommonParams(map);
439
440
            OkHttpUtils.get().url(url).params(map).build().connTimeOut(6000).readTimeOut(6000).execute(new StringCallback() {
441
                @Override
442
                public void onError(Call call, Exception e) {
443
                    ToastUtil.showToast(getContext(), "点赞失败,请重试", Toast.LENGTH_SHORT);
444
                }
445
446
                @Override
447
                public void onResponse(String res) {
448
                    String code = JsonUtils.getKeyResult(res, "code");
449
                    String desc = JsonUtils.getKeyResult(res, "desc");
450
                    if ("1000".equals(code)) {
451
                        bean.likeFlg = 1;
452
                        bean.likeNums += 1;
453
                        commentAdapter.notifyItemChanged(position);
454
                    } else {
455
                        ToastUtil.showToast(getContext(), desc, Toast.LENGTH_SHORT);
456
                    }
457
                }
458
            });
459
        }
460
461
        // 服务器签名验证
462
        private void loginSign() {
463
            String url = MainApplication.urlNew + "/topic/sign.do";
464
            final Map<String, String> map = new HashMap<>();
465
            map.put("fileNames", "android");
466
            map.put("targetType", ChatRecommendBean.TARGET_TYPE_IMAGE_TEXT + "");
467
            CommonParams.addCommonParams(map);
468
469
            OkHttpUtils.post().params(map).url(url).build().connTimeOut(6000).readTimeOut(6000).execute(new StringCallback() {
470
                @Override
471
                public void onError(Call call, Exception e) {
472
                    ToastUtil.showToast(getContext(), e.getMessage(), Toast.LENGTH_SHORT);
473
                }
474
475
                @Override
476
                public void onResponse(String response) {
477
                    String code = JsonUtils.getKeyResult(response, "code");
478
                    String desc = JsonUtils.getKeyResult(response, "desc");
479
                    if ("1000".equals(code)) {
480
                        deleteComment();
481
                    } else if ("8010".equals(code)) {
482
                        startActivity(new Intent(getContext(), LoginActivity.class));
483
                        ToastUtil.showToast(getContext(), desc, Toast.LENGTH_LONG);
484
                    } else {
485
                        ToastUtil.showToast(getContext(), desc, Toast.LENGTH_SHORT);
486
                    }
487
                }
488
            });
489
        }
490
491
        private void deleteComment() {
492
            String url = MainApplication.urlNew + "/topic/del.do";
493
            final Map<String, String> map = new HashMap<>();
494
            map.put("targetId", bean.id + "");
495
            map.put("targetType", "25");
496
            map.put("userId", bean.userId + "");
497
            CommonParams.addCommonParams(map);
498
499
            OkHttpUtils.post().params(map).url(url).build().connTimeOut(6000).readTimeOut(6000).execute(new StringCallback() {
500
                @Override
501
                public void onError(Call call, Exception e) {
502
                    ToastUtil.showToast(getContext(), e.getMessage(), Toast.LENGTH_SHORT);
503
                }
504
505
                @Override
506
                public void onResponse(String response) {
507
                    String code = JsonUtils.getKeyResult(response, "code");
508
                    String desc = JsonUtils.getKeyResult(response, "desc");
509
                    if ("1000".equals(code)) {
510
                        commentAdapter.removeData(position);
511
                        commentAdapter.notifyItemRemoved(position);
512
                        if (chatRecommendBean.commentNums > 0) {
513
                            chatRecommendBean.commentNums--;
514
                        }
515
                        allCommentTitle.setText("全部评论 " + chatRecommendBean.commentNums);
516
                        if (chatContentCommentListDialogFragmentListener != null) {
517
                            chatContentCommentListDialogFragmentListener.updateCommentNum(chatRecommendBean.commentNums);
518
                        }
519
                    } else {
520
                        ToastUtil.showToast(getContext(), desc, Toast.LENGTH_SHORT);
521
                    }
522
                }
523
            });
524
        }
525
526
    }
527
528
    private class CommentAdapter extends RecyclerView.Adapter<CommentHolder> {
529
        private static final String TAG = "CommentAdapter";
530
        private List<CommentBean> beans;
531
532
        public CommentAdapter() {
533
            this.beans = new ArrayList<>();
534
        }
535
536
        @Override
537
        public CommentHolder onCreateViewHolder(ViewGroup parent, int i) {
538
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
539
            return new CommentHolder(layoutInflater, parent);
540
        }
541
542
        @Override
543
        public void onBindViewHolder(CommentHolder commentHolder, int i) {
544
            CommentBean bean = beans.get(i);
545
            commentHolder.bindView(bean, i);
546
        }
547
548
        @Override
549
        public int getItemCount() {
550
            return beans.size();
551
        }
552
553
        public void setData(List<CommentBean> beans) {
554
            this.beans.clear();
555
            this.beans.addAll(beans);
556
            notifyDataSetChanged();
557
        }
558
559
        public void addData(List<CommentBean> beans) {
560
            int size = this.beans.size();
561
            this.beans.addAll(beans);
562
            notifyItemRangeChanged(size, beans.size());
563
        }
564
565
        public void clearData() {
566
            beans.clear();
251 567
        }
568
569
        public void removeData(int position) {
570
            if (position < beans.size()) {
571
                beans.remove(position);
572
            }
573
574
        }
575
576
        public List<CommentBean> getCurrentData() {
577
            return beans;
578
        }
579
252 580
    }
253 581
254
//    private class CommentAdapter extends RecyclerView.Adapter
582
    public interface OnChatContentCommentListDialogFragmentListener {
583
        void updateCommentNum(int num);
584
    }
585
586
    public void setOnCommonDialogFragmentListener(OnChatContentCommentListDialogFragmentListener listener) {
587
        this.chatContentCommentListDialogFragmentListener = listener;
588
    }
255 589
}

+ 25 - 2
app/src/main/res/layout/fragment_chat_content_comment_list.xml

@ -100,13 +100,36 @@
100 100
                app:layout_constraintTop_toBottomOf="@+id/loadingIndicator" />
101 101
        </android.support.constraint.ConstraintLayout>
102 102
103
        <!-- 检查网络 -->
104
103
        <!-- 没有数据 -->
105 104
        <android.support.constraint.ConstraintLayout
105
            android:id="@+id/noDataView"
106
            android:layout_width="0dp"
107
            android:layout_height="0dp"
106 108
            android:visibility="gone"
109
            app:layout_constraintBottom_toTopOf="@+id/bottomView"
110
            app:layout_constraintEnd_toEndOf="parent"
111
            app:layout_constraintStart_toStartOf="parent"
112
            app:layout_constraintTop_toBottomOf="@+id/titleView">
113
114
            <TextView
115
                android:layout_width="wrap_content"
116
                android:layout_height="wrap_content"
117
                android:layout_marginTop="20dp"
118
                android:text="还没有评论哦,快去抢沙发~"
119
                android:textColor="#ffffff"
120
                android:textSize="14sp"
121
                app:layout_constraintBottom_toBottomOf="parent"
122
                app:layout_constraintEnd_toEndOf="parent"
123
                app:layout_constraintStart_toStartOf="parent"
124
                app:layout_constraintTop_toTopOf="parent" />
125
        </android.support.constraint.ConstraintLayout>
126
127
        <!-- 检查网络 -->
128
        <android.support.constraint.ConstraintLayout
107 129
            android:id="@+id/noNetView"
108 130
            android:layout_width="0dp"
109 131
            android:layout_height="wrap_content"
132
            android:visibility="gone"
110 133
            app:layout_constraintBottom_toTopOf="@+id/bottomView"
111 134
            app:layout_constraintEnd_toEndOf="parent"
112 135
            app:layout_constraintStart_toStartOf="parent"

+ 7 - 5
app/src/main/res/layout/list_item_comment.xml

@ -94,16 +94,17 @@
94 94
        tools:text="充电桩挺多的,但是油车也多,充电的车也多。园区进门就收费,还好去了两次都充上了。"></TextView>
95 95
96 96
    <android.support.constraint.ConstraintLayout
97
        android:id="@+id/replyView"
98
        android:layout_width="match_parent"
97
        android:id="@+id/replyAndDeleteView"
98
        android:layout_width="wrap_content"
99 99
        android:layout_height="42dp"
100 100
        android:paddingLeft="29dp"
101
        app:layout_constraintLeft_toLeftOf="parent"
101 102
        app:layout_constraintBottom_toBottomOf="parent"
102 103
        app:layout_constraintTop_toBottomOf="@+id/content"
103 104
        tools:background="#06648f">
104 105
105 106
        <android.support.constraint.ConstraintLayout
106
            android:id="@+id/replayInfo"
107
            android:id="@+id/replyInfo"
107 108
            android:layout_width="wrap_content"
108 109
            android:layout_height="match_parent"
109 110
            android:layout_marginLeft="10dp"
@ -138,6 +139,7 @@
138 139
        </android.support.constraint.ConstraintLayout>
139 140
140 141
        <TextView
142
            android:id="@+id/deleteComment"
141 143
            android:layout_width="wrap_content"
142 144
            android:layout_height="42dp"
143 145
            android:gravity="center"
@ -147,7 +149,7 @@
147 149
            android:textColor="#c2c2c2"
148 150
            android:textSize="12sp"
149 151
            app:layout_constraintBottom_toBottomOf="parent"
150
            app:layout_constraintLeft_toRightOf="@+id/replayInfo"
152
            app:layout_constraintLeft_toRightOf="@+id/replyInfo"
151 153
            app:layout_constraintTop_toTopOf="parent"
152 154
            tools:background="#790f59" />
153 155
    </android.support.constraint.ConstraintLayout>
@ -159,5 +161,5 @@
159 161
        android:layout_marginTop="10dp"
160 162
        android:background="#505050"
161 163
        app:layout_constraintBottom_toBottomOf="parent"
162
        app:layout_constraintTop_toBottomOf="@+id/replyView" />
164
        app:layout_constraintTop_toBottomOf="@+id/replyAndDeleteView" />
163 165
</android.support.constraint.ConstraintLayout>

Sign In - Gogs: Go Git Service

Sign In