我有一个带有适配器的recyclerView ... recyclerview的项目带有相似的按钮。当我单击(喜欢)按钮时,(不喜欢)应该变得不可见,反之亦然。.一切正常,但是当我单击(喜欢)某项时,其他项目中的(不喜欢)也变得不可见,就像我在点击一样其他视图中的相同按钮 我已经尝试过此答案,但没有成功Android: Changing visibility of a view in recyclerView 这是适配器代码:
package com.example.user.myapplication;
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.opengl.Visibility
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.*
import android.widget.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
import kotlinx.android.synthetic.main.item.view.*
import kotlinx.android.synthetic.main.itemm.view.*
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
/**
* Created by Ahmed on 10/14/2018.
*/
class ItemmAdapter(private val mItems: Array<File>,private val activity:Activity) : RecyclerView.Adapter<ItemmAdapter.ItemViewHolder>() {
lateinit var parent:ViewGroup
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.itemm, parent, false)
this.parent=parent
return ItemViewHolder(view)
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val item = mItems[position]
holder.itemView.layoutParams=RecyclerView.LayoutParams(parent.context.resources.displayMetrics.widthPixels/2,RecyclerView.LayoutParams.MATCH_PARENT)
//holder.itemView.findViewById<ImageView>(R.id.image).setImageResource(R.drawable.icons8_cycling_104)
//holder.itemView.findViewById<TextView>(R.id.description).text="abcdefghijklmnopqrstuvwxyzضصثقفغعهخحجدطكمنتالبيسشذئءؤرلاىة"
//holder.itemView.findViewById<TextView>(R.id.price).text="10000$"
holder.like.setOnClickListener{
holder.like.visibility=View.GONE
holder.unlike.visibility=View.VISIBLE
}
holder.unlike.setOnClickListener{
holder.unlike.visibility=View.GONE
holder.like.visibility=View.VISIBLE
}
holder.shopping.setOnClickListener{
holder.shopping.visibility=View.GONE
holder.notshopping.visibility=View.VISIBLE
}
holder.notshopping.setOnClickListener{
holder.notshopping.visibility=View.GONE
holder.shopping.visibility=View.VISIBLE
}
}
override fun getItemCount(): Int {
return mItems.size
}
inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val like=itemView.findViewById<ImageView>(R.id.like)
val unlike=itemView.findViewById<ImageView>(R.id.unlike)
val shopping=itemView.findViewById<ImageView>(R.id.shopping)
val notshopping=itemView.findViewById<ImageView>(R.id.notShopping)
}
}
请帮助我
答案 0 :(得分:0)
仅在每个项目中使用绑定设置项目内容,并将侦听器从绑定方法移动到所有者类:
inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var liked = false
var shopped = false
val like=itemView.findViewById<ImageView>(R.id.like)
val unlike=itemView.findViewById<ImageView>(R.id.unlike)
val shopping=itemView.findViewById<ImageView>(R.id.shopping)
val notshopping=itemView.findViewById<ImageView>(R.id.notShopping)
init{
like?.setOnClickListener{
liked = true
like.visibility=View.GONE
unlike.visibility=View.VISIBLE
}
unlike?.setOnClickListener{
liked = false
unlike.visibility=View.GONE
like.visibility=View.VISIBLE
}
shopping?.setOnClickListener{
shopped = true
shopping.visibility=View.GONE
notshopping.visibility=View.VISIBLE
}
notshopping?.setOnClickListener{
shopped = false
notshopping.visibility=View.GONE
shopping.visibility=View.VISIBLE
}
}
fun bind(){// call this from onBind method of adapter
if(liked) like?.performClick() else unlike?.performClick()
if(shopped) shopping?.performClick() else notshopping?.performClick()
}
}
答案 1 :(得分:0)
之所以发生这种情况,是因为其具有回收站视图(因此,当您滚动视图时,该视图将被回收,并且类似或不同的木棒在那里可见。
修改项目对象以包含布尔值isLiked
,并在onClick上将其设置为true或false:
holder.like.setOnClickListener{
item.setIsLiked(true);
}
holder.unlike.setOnClickListener{
item.setIsLiked(false);
}
并基于此设置视图可见性:
holder.like.visibility=item.getIsLiked() ? View.VISIBLE : View.GONE;
holder.unlike.visibility=item.getIsLiked() ? View.GONE : View.VISIBLE;
注释:如果您使用自动生成的getter和setter方法,则setIsLiked(boolean like)和getIsLiked()可能是不同的名称;
对于购物不购物也要这么做