我对“喜欢”和“不喜欢”按钮使用了逻辑
我声明了变量的地方
var user_like_dislike_state == 0
当我单击“赞”按钮时,如果 我再次按下它会变回0
if(user_like_dislike_state == 0 || user_like_dislike_state == 1){
user_like_dislike_state = -1
} else {
user_like_dislike_state = 0
}
当我尝试在Single post上使用但在Recyclerview中尝试使用此逻辑时,它可以正常工作
如果我喜欢帖子1,然后尝试喜欢帖子4,那么它将自动将值更改为0
我给了3个州
user_like_dislike_state == 1 : **Like**
user_like_dislike_state == 0 : **Non**
user_like_dislike_state == -1 : **Dislike**
Adapter.kt:
class ViewsinglepostAdapter(var commentsList: ArrayList<Comment>, val upVote : View.OnClickListener, val downVote: View.OnClickListener): RecyclerView.Adapter<ViewsinglepostAdapter.ViewHolder>(){
var user_like_dislike_state = 0
var comment_id = 0
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.user_name?.setText(commentsList.get(position).User)
holder.comment?.setText(commentsList.get(position).comment)
holder.upvote?.setOnClickListener({
if(user_like_dislike_state == 0 || user_like_dislike_state == -1){
user_like_dislike_state = 1
} else {
user_like_dislike_state = 0
}
comment_id = commentsList.get(position).Id
upVote.onClick(it)
})
holder.downvote?.setOnClickListener({it->
if(user_like_dislike_state == 0 || user_like_dislike_state == 1){
user_like_dislike_state = -1
} else {
user_like_dislike_state = 0
}
comment_id = commentsList.get(position).Id
downVote.onClick(it)
user_like_dislike_state == 0
})
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder{
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.singlecomment,parent,false)
return ViewHolder(itemView)
}
override fun getItemCount(): Int {
return commentsList.count()
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
var upvote: ImageView? = null
var downvote: ImageView? = null
var user_name: TextView? = null
var comment: TextView? = null
init {
this.upvote = itemView.findViewById(R.id.upvote_post_img)
this.downvote = itemView.findViewById(R.id.down_vote_post_img)
this.user_name = itemView.findViewById(R.id.user_name)
this.comment = itemView.findViewById(R.id.comment)
}
}
}
答案 0 :(得分:2)
您只需使用一个boolean
标志并将其初始值设置为false即可。
单击按钮后,您只需使用Turnery运算符更新其值即可
user_like_dislike_state = user_like_dislike_state?false:true;
//对于“ upvote”按钮,应将其设置为1或-1。
holder.upvote?.setOnClickListener({
user_like_dislike_state = user_like_dislike_state<1?1:-1;
comment_id = commentsList.get(position).Id
upVote.onClick(it)
})
对于降低投票按钮,您检查它是否已经降低投票或提高投票。
holder.downvote?.setOnClickListener({it->
user_like_dislike_state = user_like_dislike_state>-1?-1:0;
comment_id = commentsList.get(position).Id
downVote.onClick(it)
})
答案 1 :(得分:1)
也许由于Recyclerview的缘故,我假设当向下滚动或向上滚动时,某个帖子/行已经消失了。向上或向下滚动时,在Recyclerview中消失的每个视图/行都将再次重新加载,因此这就是为什么您总是得到0
答案 2 :(得分:0)
使用成员user_like_dislike_state创建类:Int传递该类的ArrayList来回收适配器并分别处理列表中的每个行视图。
答案 3 :(得分:0)
我建议您在喜欢/不喜欢时使用局部变量,否则var user_like_dislike_state
会保留之前喜欢/不喜欢的状态,除非您重置它
答案 4 :(得分:0)
非常简单,只需在模型类示例中获取一个变量:-var like_Status
并在recyclerview中单击时进行相应设置,然后在滚动列表时检查该变量,如
if(like_statusbutton==-1)
{
setLikeStatus="dislike"
}
else if(like_status==1)
{
setLikeStatus="like"
}
else {
setLikeStatus="none"
}
您必须维护模型类中每个“喜欢”按钮的状态。这就是为什么您在代码中面临此问题的原因。 请保持模型类中每个类似按钮的状态,并在滚动recycleview时检查该按钮的先前状态。
答案 5 :(得分:0)
我尝试了此代码并工作:
I used **Hashmap** for save my Comment_id and user_like_state
代码:
//变量声明:
键和变量均为Int,因为我的Comment_id和user_like_state为Int:
var commentHashMap = HashMap<Int, Int>()
然后
投票:
holder.upvote?.setOnClickListener({
comment_id = commentsList.get(position).post_id
if(commentHashMap .get(comment_id) == null || commentHashMap.get(comment_id)== 0 || commentHashMap.get(comment_id)== -1){
commentHashMap.put(comment_id, 1)
} else{
commentHashMap.put(comment_id, 0)
}
upVote.onClick(it)
})
投票:
holder.downVote?.setOnClickListener({
comment_id = commentsList.get(position).post_id
if(commentHashMap .get(comment_id) == null || commentHashMap.get(comment_id)== 0 || commentHashMap.get(comment_id)== 1){
commentHashMap.put(comment_id, -1)
} else{
commentHashMap.put(comment_id, 0)
}
downVote.onClick(it)
})
想要类似Facebook的代码的人可以使用此代码