我对RecyclerView
中的帖子执行喜欢和不喜欢的操作,更改视图后调用了api,但问题是,当RecyclerView
通知时,它总是滚动到顶部更新。
这是我的适配器代码:
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)
}
}
})
这是我的活动代码
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)
}
我的initAdapter函数:
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
}
我的回调函数:
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)
}
})
}
我想保持滚动状态而不滚动到顶部
答案 0 :(得分:2)
一些提示:
不要使用适配器的多个对象。仅使用单个对象并尝试更新数据。更新数据后,请勿再设置适配器。
更新列表后,只需调用adapter.notifydatasetchanged()
就可以解决您的问题。
希望这能解决您的问题。
答案 1 :(得分:1)
视图需要有一个 id 才能保存它们的状态,因此请确保您的 RecyclerViews 的所有父布局都有一个 id。
答案 2 :(得分:0)
不确定这是否可以解决您的问题,但是我遇到了同样的问题,这是因为在通知后我正在调用setAdapter。因此,通知后不要调用setAdapter解决了问题。
mAdapter.notifyItemRangeInserted(mAdapter.getItemCount(), list.size() - 1);
recyclerView.setAdapter();
//删除此行。
//仅当您首次设置数据或适配器为null时,才执行此操作。