I am doing the like and dislike for a post in a RecyclerView
, I call the api after changing the view but the problem is that the RecyclerView
always scrolls to the top when I notify it with the updates.
this is my adapter code :
likeAnim.setOnLikeListener(object : OnLikeListener {
override fun liked(p0: LikeButton?) {
if (commentList[position]!!.liked) {
//
likeButton.setBackgroundResource(R.drawable.heart_ico)
isLiked = false
callback(commentList[position], 2, layoutPosition)
} else {
// likeButton.setBackgroundResource(R.drawable.filled_like_ico)
isLiked = true
callback(commentList[position], 1, layoutPosition)
}
}
override fun unLiked(p0: LikeButton?) {
if (commentList[position]!!.liked) {
// likeButton.setBackgroundResource(R.drawable.heart_ico)
isLiked = false
callback(commentList[position], 2, layoutPosition)
} else {
// likeButton.setBackgroundResource(R.drawable.filled_like_ico)
isLiked = true
callback(commentList[position], 1, layoutPosition)
}
}
})
this is my activity code
private fun onCommentActionSuccess(list: LikeRequestModel, type: Int, position: Int) {
when (type) {
1 -> Log.d("Success", "success: comment like done")
2 -> Log.d("Success", "success: comment dislike done")
}
viewModel.getClassComments(LoginClass.login.user!!.data!!.school_classes_id!![0].id).observe(this@ClassHomeScreenActivity, Observer {
when {
it!!.isSuccess() -> onSuccessRefrech(it.data!!.data, position)
}
})
}
private fun onSuccessRefrech(list: MutableList<ClasseCommentModel.Data?>,
position: Int) {
Log.d("Success", "success: comment loaded")
//initCommentAdapter(list)
adapter.commentList = list
// val adapter = ClassCommentsListAdapter(this@ClassHomeScreenActivity,
list) { it, type, pos ->
// likeComment(it!!.id.toString(), type, pos)//Kotlin callback
instead of interface in java
// }
// binding.postRV.swapAdapter(adapter, false)
// adapter.notifyItemChanged(position)
binding.postRV.scrollToPosition(position)
}
my initAdapter function :
private fun initCommentAdapter(list:
MutableList<ClasseCommentModel.Data?>) {
adapter = ClassCommentsListAdapter(this@ClassHomeScreenActivity, list) {
it, type, position ->
likeComment(it!!.id.toString(), type, position)//Kotlin callback
instead of interface in java
}
binding.postRV.setHasFixedSize(true) //
binding.postRV.itemAnimator = null // remove recycle view animation
binding.postRV.layoutManager =
LinearLayoutManager(this@ClassHomeScreenActivity,
OrientationHelper.VERTICAL, false)
binding.postRV.adapter = adapter
}
my callback function :
private fun likeComment(commentId: String, type: Int, position: Int) {
when (type) {
1 -> {
viewModel.likeComment(commentId).observe(this@ClassHomeScreenActivity, Observer {
when {
it!!.isError() -> onErrorAction(it.error)
it.isSuccess() -> onCommentActionSuccess(it.data!!, type, position)
}
})
}
2 -> {
viewModel.dislikeComment(commentId).observe(this@ClassHomeScreenActivity, Observer {
when {
it!!.isError() -> onErrorAction(it.error)
it.isSuccess() -> onCommentActionSuccess(it.data!!, type, position)
}
})
}
}
}
private fun onCommentActionSuccess(list: LikeRequestModel, type: Int,
position: Int) {
when (type) {
1 -> Log.d("Success", "success: comment like done")
2 -> Log.d("Success", "success: comment dislike done")
}
viewModel.getClassCommentsSingleEvent
(LoginClass.login.user!!.data!!.school_clas
ses_id!![0].id).observe(this@ClassHomeScreenActivity, Observer {
when {
it!!.isSuccess() -> onSuccessRefrech(it.data!!.data, position)
}
})
}
I wanna stay in the scroll position without scrolling to top