gpt4 book ai didi

android - 更改适配器数据时 RecyclerView 滚动到顶部位置

转载 作者:搜寻专家 更新时间:2023-11-01 09:24:01 27 4
gpt4 key购买 nike

我对 RecyclerView 中的一篇文章喜欢和不喜欢,我在更改 View 后调用了 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)
}
}
})

这是我的 Activity 代码

        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)
}

我的初始化适配器函数:

       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)
}
})
}

我想留在滚动位置而不滚动到顶部

最佳答案

一些提示:

  1. 不要使用多个适配器对象。仅使用单个对象并尝试更新数据。更新数据后不要再设置adapter。

  2. 更新列表后,只需调用 adapter.notifydatasetchanged() 即可解决您的问题。

希望这能解决您的问题。

关于android - 更改适配器数据时 RecyclerView 滚动到顶部位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52163993/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com