4

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

jle
  • 646
  • 1
  • 7
  • 22
B.mansouri
  • 375
  • 5
  • 14

2 Answers2

2

Some tips:

  1. Don't use multiple objects of adapter. Use only single object and try to update the data. Don't set adapter again after updating data.

  2. After updating list just calling adapter.notifydatasetchanged() will solve your problem.

Hopefully this will solve your problem.

jake
  • 520
  • 2
  • 10
0

Not sure if this solves your problem but I was having the same issue and it was because I was calling setAdapter after notify. So, not calling setAdapter after notify solved the problem.

mAdapter.notifyItemRangeInserted(mAdapter.getItemCount(), list.size() - 1);

recyclerView.setAdapter(); // remove this line. //Do this only when you're setting the data for the first time or when adapter is null.

Erselan Khan
  • 458
  • 4
  • 12