@@ -292,7 +292,12 @@ public void onClick(View view) {
292
292
return ;
293
293
}
294
294
295
- final ReaderComment comment = getItem (position );
295
+ int currentPosition = holder .getBindingAdapterPosition ();
296
+ if (currentPosition == RecyclerView .NO_POSITION ) {
297
+ return ;
298
+ }
299
+
300
+ ReaderComment comment = getItem (currentPosition );
296
301
if (comment == null ) {
297
302
return ;
298
303
}
@@ -311,26 +316,26 @@ public void onClick(View view) {
311
316
String avatarUrl = WPAvatarUtils .rewriteAvatarUrl (comment .getAuthorAvatar (), mAvatarSz );
312
317
mImageManager .loadIntoCircle (commentHolder .mImgAvatar , ImageType .AVATAR , avatarUrl );
313
318
314
- // tapping avatar or author name opens blog preview
315
- if (comment .hasAuthorBlogId ()) {
316
- View .OnClickListener authorListener = new View .OnClickListener () {
317
- @ Override
318
- public void onClick (View view ) {
319
+ View .OnClickListener authorListener = new View .OnClickListener () {
320
+ @ Override
321
+ public void onClick (View view ) {
322
+ int authorCurrentPosition = commentHolder .getBindingAdapterPosition ();
323
+ if (authorCurrentPosition == RecyclerView .NO_POSITION ) return ;
324
+
325
+ ReaderComment currentComment = getItem (authorCurrentPosition );
326
+ // tapping avatar or author name opens blog preview
327
+ if (currentComment != null && currentComment .hasAuthorBlogId ()) {
319
328
ReaderActivityLauncher .showReaderBlogPreview (
320
329
view .getContext (),
321
- comment .authorBlogId ,
330
+ currentComment .authorBlogId ,
322
331
mPost .isFollowedByCurrentUser ,
323
332
ReaderTracker .SOURCE_COMMENT ,
324
333
mReaderTracker
325
334
);
326
335
}
327
- };
328
- commentHolder .mAuthorContainer .setOnClickListener (authorListener );
329
- commentHolder .mAuthorContainer .setOnClickListener (authorListener );
330
- } else {
331
- commentHolder .mAuthorContainer .setOnClickListener (null );
332
- commentHolder .mAuthorContainer .setOnClickListener (null );
333
- }
336
+ }
337
+ };
338
+ commentHolder .mAuthorContainer .setOnClickListener (authorListener );
334
339
335
340
// author name uses different color for comments from the post's author
336
341
if (comment .authorId == mPost .authorId ) {
@@ -382,17 +387,30 @@ public void onClick(View view) {
382
387
menuPopup .setAnchorView (commentHolder .mActionButton );
383
388
menuPopup .setModal (true );
384
389
menuPopup .setOnItemClickListener ((parent , view , position1 , id ) -> {
385
- mCommentMenuActionListener
386
- .onCommentMenuItemTapped (comment , actions .get (position1 ).getType ());
390
+ int menuCurrentPosition = commentHolder .getBindingAdapterPosition ();
391
+ if (menuCurrentPosition != RecyclerView .NO_POSITION ) {
392
+ ReaderComment currentComment = getItem (menuCurrentPosition );
393
+ if (currentComment != null ) {
394
+ mCommentMenuActionListener
395
+ .onCommentMenuItemTapped (currentComment , actions .get (position1 ).getType ());
396
+ }
397
+ }
387
398
menuPopup .dismiss ();
388
399
});
389
400
menuPopup .show ();
390
401
});
391
402
} else {
392
403
commentHolder .mActionButton .setImageResource (R .drawable .ic_share_white_24dp );
393
- commentHolder .mActionButtonContainer .setOnClickListener (
394
- v -> mCommentMenuActionListener
395
- .onCommentMenuItemTapped (comment , ReaderCommentMenuActionType .SHARE ));
404
+ commentHolder .mActionButtonContainer .setOnClickListener (v -> {
405
+ int shareCurrentPosition = commentHolder .getBindingAdapterPosition ();
406
+ if (shareCurrentPosition != RecyclerView .NO_POSITION ) {
407
+ ReaderComment currentComment = getItem (shareCurrentPosition );
408
+ if (currentComment != null ) {
409
+ mCommentMenuActionListener
410
+ .onCommentMenuItemTapped (currentComment , ReaderCommentMenuActionType .SHARE );
411
+ }
412
+ }
413
+ });
396
414
}
397
415
398
416
// show indentation spacer for comments with parents and indent it based on comment level
@@ -441,8 +459,12 @@ public void onClick(View view) {
441
459
commentHolder .mReplyView .setOnClickListener (new View .OnClickListener () {
442
460
@ Override
443
461
public void onClick (View v ) {
444
- if (mReplyListener != null ) {
445
- mReplyListener .onRequestReply (comment .commentId );
462
+ int replyCurrentPosition = commentHolder .getBindingAdapterPosition ();
463
+ if (replyCurrentPosition == RecyclerView .NO_POSITION ) return ;
464
+
465
+ ReaderComment currentComment = getItem (replyCurrentPosition );
466
+ if (currentComment != null && mReplyListener != null ) {
467
+ mReplyListener .onRequestReply (currentComment .commentId );
446
468
}
447
469
}
448
470
});
@@ -461,11 +483,11 @@ public void run() {
461
483
}
462
484
}
463
485
464
- showLikeStatus (commentHolder , position );
486
+ showLikeStatus (commentHolder );
465
487
466
488
// if we're nearing the end of the comments and we know more exist on the server,
467
489
// fire request to load more
468
- if (mMoreCommentsExist && mDataRequestedListener != null && (position >= getItemCount () - NUM_HEADERS )) {
490
+ if (mMoreCommentsExist && mDataRequestedListener != null && (currentPosition >= getItemCount () - NUM_HEADERS )) {
469
491
mDataRequestedListener .onRequestData ();
470
492
}
471
493
}
@@ -495,7 +517,12 @@ public void refreshPost() {
495
517
}
496
518
}
497
519
498
- private void showLikeStatus (final CommentHolder holder , int position ) {
520
+ private void showLikeStatus (final CommentHolder holder ) {
521
+ int position = holder .getBindingAdapterPosition ();
522
+ if (position == RecyclerView .NO_POSITION ) {
523
+ return ;
524
+ }
525
+
499
526
ReaderComment comment = getItem (position );
500
527
if (comment == null ) {
501
528
return ;
@@ -515,8 +542,7 @@ private void showLikeStatus(final CommentHolder holder, int position) {
515
542
holder .mCountLikes .setOnClickListener (new View .OnClickListener () {
516
543
@ Override
517
544
public void onClick (View v ) {
518
- int clickedPosition = holder .getBindingAdapterPosition ();
519
- toggleLike (v .getContext (), holder , clickedPosition );
545
+ toggleLike (v .getContext (), holder );
520
546
}
521
547
});
522
548
}
@@ -526,11 +552,16 @@ public void onClick(View v) {
526
552
}
527
553
}
528
554
529
- private void toggleLike (Context context , CommentHolder holder , int position ) {
555
+ private void toggleLike (Context context , CommentHolder holder ) {
530
556
if (!NetworkUtils .checkConnection (context )) {
531
557
return ;
532
558
}
533
559
560
+ int position = holder .getBindingAdapterPosition ();
561
+ if (position == RecyclerView .NO_POSITION ) {
562
+ return ;
563
+ }
564
+
534
565
ReaderComment comment = getItem (position );
535
566
if (comment == null ) {
536
567
ToastUtils .showToast (context , R .string .reader_toast_err_generic );
@@ -548,7 +579,7 @@ private void toggleLike(Context context, CommentHolder holder, int position) {
548
579
ReaderComment updatedComment = ReaderCommentTable .getComment (comment .blogId , comment .postId , comment .commentId );
549
580
if (updatedComment != null ) {
550
581
mComments .set (position - NUM_HEADERS , updatedComment );
551
- showLikeStatus (holder , position );
582
+ showLikeStatus (holder );
552
583
}
553
584
554
585
mReaderTracker .trackPost (isAskingToLike
0 commit comments