0

STORY:-

I'm making app like Instagram Reels, with viewPager2 with RecyclerViewAdapter, all functionality working fine,

only one problem I'm facing right now is when I click on comment button the Comment screen is opening in another activity and when I getting back on reels activity after commenting or without commenting,

video playing again and mixing with current audio that means notifyItemChanged is not working properly in my case,

I just want to update that comment count only without affecting whole adapter items.

Class- AdapterClass : RecyclerView.Adapter

onClickListener for Comment button

binding.ivComment.setOnClickListener(View.OnClickListener {
            if (Utility.isOnline(context)) {
                itemClick(post)
            } else {
                val toast: Toast = Toast.makeText(
                    context,
                    R.string.msg_no_internet,
                    Toast.LENGTH_SHORT
                )
                toast.show()
            }
        })

fun itemClick(postList: PostList?) {
    if (Utility.isOnline(context)) {
        val intent = Intent(context, BitesCommentActivity::class.java)
        intent.putExtra(PostList.SENDEXTRA, postList)
        context.startActivity(intent)
    } else {
        val toast: Toast = Toast.makeText(context, R.string.msg_no_internet, Toast.LENGTH_SHORT)
        toast.show()
    }
}

Class- Activity Class

Using BroadcastReceiver for update comment count from CommentActivity

private val mMessageReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val isActivityInForeground = this@BitesSwappableVideosActivity.lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)
            if (!isActivityInForeground) {
                val message = intent.getStringExtra("message")
                val epost_id = intent.extras!!
                    .getLong(Constant.ScreenExtras.EPOST_ID, -1)
                val action = intent.extras!!.getInt(Constant.ScreenExtras.ACTION)

                for (i in fav.indices) {
                    val postList: PostList = fav[i]
                    val updateIndex: Int

                    if (action == Constant.COMMENT) {
                        if (postList.postId == epost_id) {
                            postList.totalComment = postList.totalComment + 1
                            eBiteReels_adapter.notifyItemChanged(i)
                            break
                        }
                    }
                }
            } else {
                val message = intent.getStringExtra("message")
                val epost_id = intent.extras!!
                    .getLong(Constant.ScreenExtras.EPOST_ID, -1)
                val action = intent.extras!!.getInt(Constant.ScreenExtras.ACTION)
                for (i in fav.indices) {
                    val postList: PostList = fav[i]
                    if (action == Constant.COMMENT) {
                        if (postList.postId == epost_id) {
                            postList.totalComment = postList.totalComment + 1
                            eBiteReels_adapter.notifyItemChanged(i)
                            break
                        }
                    }
                }
            }
        }
    }

I've tried

eBiteReels_adapter.notifyItemChanged(i)

but it cause problem with another items especially with videoplayer.

if I remove notifyItemChanged from my code it refreshes comment count when I restart my SwipableVideoActivity

0 Answers0