Android 如何从RecyclerView适配器读取布尔值
对于我的Android 如何从RecyclerView适配器读取布尔值,android,kotlin,android-adapter,android-recyclerview,Android,Kotlin,Android Adapter,Android Recyclerview,对于我的RecyclerView,如何使用片段中声明的布尔值来显示或隐藏我的RecyclerView项目模板中的各种项目?在我的适配器类中,我尝试使用例如holder.mIVLanguage.visibility=(myList[position].displaySymbolLanguage),但没有成功 回收视图项目数据类 data class Facility (val arrowExpandCollapse: Drawable, val to
RecyclerView
,如何使用片段中声明的布尔值来显示或隐藏我的RecyclerView
项目模板中的各种项目?在我的适配器类中,我尝试使用例如holder.mIVLanguage.visibility=(myList[position].displaySymbolLanguage)
,但没有成功
回收视图项目数据类
data class Facility (val arrowExpandCollapse: Drawable,
val topicSymbol: Drawable,
val displaySymbolLanguage: Boolean,
val displaySymbolPets: Boolean,
val displaySymbolVerifiedUser: Boolean,
val displaySymbolTransport: Boolean,
val displaySymbolSeating: Boolean,
val displaySymbolFingerPrint: Boolean,
val displaySymbolArrival: Boolean,
val displaySymbolDeparture: Boolean)
class MyFragment : androidx.fragment.app.Fragment() {
private lateinit var mRecyclerView: RecyclerView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_rv, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
mRecyclerView = v!!.findViewById<RecyclerView>(R.id.my_recyclerview)
mRecyclerView.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
val symbolsArray = intArrayOf(R.drawable.ic_info_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_pets_black_24dp)
val myList = ArrayList<Facility>()
myList.add(Facility(resources.getDrawable(R.drawable.ic_keyboard_arrow_down, null),
resources.getDrawable(R.drawable.ic_info_black_24dp, null), true, false, false, false, true, true, true, false))
}
}
val mAdapter = RVAdapterFacilities(myList)
mRecyclerView.adapter = mAdapter
super.onActivityCreated(savedInstanceState)
}
}
class RVAdapterFacilities(val myList: ArrayList<Facility>): RecyclerView.Adapter<RVAdapterFacilities.ViewHolder>() {
override fun getItemCount(): Int {
return myList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.mIVExpandCollapse.setImageDrawable(myList[position].arrowExpandCollapse)
holder.mIVTopic.setImageDrawable(myList[position].topicSymbolol)
holder.mIVLanguage.visibility = ¿What goes here?
holder.mIVPets.visibility = ¿What goes here?
holder.mIVVerifiedUser.visibility = ¿What goes here?
holder.mIVTransport.visibility = ¿What goes here?
holder.mIVSeat.visibility = ¿What goes here?
holder.mIVFingerprint.visibility = ¿What goes here?
holder.mIVDeparture.visibility = ¿What goes here?
holder.mIVArrival.visibility = ¿What goes here?
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.cv_facility_information, parent, false)
return ViewHolder(v)
}
class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
val mCV = itemView.findViewById<CardView>(R.id.cv_facilities)
val mIVExpandCollapse = itemView.findViewById<ImageView>(R.id.iv_expandcollapsearrow)!!
val mIVTopic = itemView.findViewById<ImageView>(R.id.iv_topicsymbol)!!
val mLLSymbols = itemView.findViewById<LinearLayout>(R.id.ll_symbols)!!
}
}
回收视图片段类
data class Facility (val arrowExpandCollapse: Drawable,
val topicSymbol: Drawable,
val displaySymbolLanguage: Boolean,
val displaySymbolPets: Boolean,
val displaySymbolVerifiedUser: Boolean,
val displaySymbolTransport: Boolean,
val displaySymbolSeating: Boolean,
val displaySymbolFingerPrint: Boolean,
val displaySymbolArrival: Boolean,
val displaySymbolDeparture: Boolean)
class MyFragment : androidx.fragment.app.Fragment() {
private lateinit var mRecyclerView: RecyclerView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_rv, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
mRecyclerView = v!!.findViewById<RecyclerView>(R.id.my_recyclerview)
mRecyclerView.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
val symbolsArray = intArrayOf(R.drawable.ic_info_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_pets_black_24dp)
val myList = ArrayList<Facility>()
myList.add(Facility(resources.getDrawable(R.drawable.ic_keyboard_arrow_down, null),
resources.getDrawable(R.drawable.ic_info_black_24dp, null), true, false, false, false, true, true, true, false))
}
}
val mAdapter = RVAdapterFacilities(myList)
mRecyclerView.adapter = mAdapter
super.onActivityCreated(savedInstanceState)
}
}
class RVAdapterFacilities(val myList: ArrayList<Facility>): RecyclerView.Adapter<RVAdapterFacilities.ViewHolder>() {
override fun getItemCount(): Int {
return myList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.mIVExpandCollapse.setImageDrawable(myList[position].arrowExpandCollapse)
holder.mIVTopic.setImageDrawable(myList[position].topicSymbolol)
holder.mIVLanguage.visibility = ¿What goes here?
holder.mIVPets.visibility = ¿What goes here?
holder.mIVVerifiedUser.visibility = ¿What goes here?
holder.mIVTransport.visibility = ¿What goes here?
holder.mIVSeat.visibility = ¿What goes here?
holder.mIVFingerprint.visibility = ¿What goes here?
holder.mIVDeparture.visibility = ¿What goes here?
holder.mIVArrival.visibility = ¿What goes here?
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.cv_facility_information, parent, false)
return ViewHolder(v)
}
class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
val mCV = itemView.findViewById<CardView>(R.id.cv_facilities)
val mIVExpandCollapse = itemView.findViewById<ImageView>(R.id.iv_expandcollapsearrow)!!
val mIVTopic = itemView.findViewById<ImageView>(R.id.iv_topicsymbol)!!
val mLLSymbols = itemView.findViewById<LinearLayout>(R.id.ll_symbols)!!
}
}
classmyfragment:androidx.fragment.app.fragment(){
私有lateinit var mRecyclerView:RecyclerView
覆盖创建视图(充气机:布局充气机,容器:ViewGroup?,savedInstanceState:Bundle?):视图{
返回充气机。充气(右布局图。碎片_rv,容器,假)
}
覆盖活动创建的乐趣(savedInstanceState:Bundle?){
val v=视图
mRecyclerView=v!!.findViewById(R.id.my\u recyclerview)
mRecyclerView.layoutManager=LinearLayoutManager(活动,RecyclerView.VERTICAL,false)
val SYMBOLSRARY=intArrayOf(R.drawable.ic\U info\U black\U 24dp,
R.drawable.ic_语言_黑色_24dp,
R.drawable.ic_宠物_黑色_24dp,
R.drawable.ic_语言_黑色_24dp,
R.drawable.ic_宠物_黑色_24dp,
R.drawable.ic_语言_黑色_24dp,
R.drawable.ic_宠物_黑色_24dp,
R.drawable.ic_语言_黑色_24dp,
R.drawable.ic_宠物_黑色_24dp,
R.drawable.ic_宠物_黑色_24dp)
val myList=ArrayList()
添加(Facility(resources.getDrawable(R.drawable.ic\u keyboard\u arrow\u down,null),
getDrawable(R.drawable.ic_info_black_24dp,null),真,假,假,假,真,真,真,假)
}
}
val mAdapter=RVADAPTER设施(myList)
mRecyclerView.adapter=mAdapter
super.onActivityCreated(savedInstanceState)
}
}
RecyclerView适配器类
data class Facility (val arrowExpandCollapse: Drawable,
val topicSymbol: Drawable,
val displaySymbolLanguage: Boolean,
val displaySymbolPets: Boolean,
val displaySymbolVerifiedUser: Boolean,
val displaySymbolTransport: Boolean,
val displaySymbolSeating: Boolean,
val displaySymbolFingerPrint: Boolean,
val displaySymbolArrival: Boolean,
val displaySymbolDeparture: Boolean)
class MyFragment : androidx.fragment.app.Fragment() {
private lateinit var mRecyclerView: RecyclerView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_rv, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
mRecyclerView = v!!.findViewById<RecyclerView>(R.id.my_recyclerview)
mRecyclerView.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
val symbolsArray = intArrayOf(R.drawable.ic_info_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_language_black_24dp,
R.drawable.ic_pets_black_24dp,
R.drawable.ic_pets_black_24dp)
val myList = ArrayList<Facility>()
myList.add(Facility(resources.getDrawable(R.drawable.ic_keyboard_arrow_down, null),
resources.getDrawable(R.drawable.ic_info_black_24dp, null), true, false, false, false, true, true, true, false))
}
}
val mAdapter = RVAdapterFacilities(myList)
mRecyclerView.adapter = mAdapter
super.onActivityCreated(savedInstanceState)
}
}
class RVAdapterFacilities(val myList: ArrayList<Facility>): RecyclerView.Adapter<RVAdapterFacilities.ViewHolder>() {
override fun getItemCount(): Int {
return myList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.mIVExpandCollapse.setImageDrawable(myList[position].arrowExpandCollapse)
holder.mIVTopic.setImageDrawable(myList[position].topicSymbolol)
holder.mIVLanguage.visibility = ¿What goes here?
holder.mIVPets.visibility = ¿What goes here?
holder.mIVVerifiedUser.visibility = ¿What goes here?
holder.mIVTransport.visibility = ¿What goes here?
holder.mIVSeat.visibility = ¿What goes here?
holder.mIVFingerprint.visibility = ¿What goes here?
holder.mIVDeparture.visibility = ¿What goes here?
holder.mIVArrival.visibility = ¿What goes here?
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.cv_facility_information, parent, false)
return ViewHolder(v)
}
class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
val mCV = itemView.findViewById<CardView>(R.id.cv_facilities)
val mIVExpandCollapse = itemView.findViewById<ImageView>(R.id.iv_expandcollapsearrow)!!
val mIVTopic = itemView.findViewById<ImageView>(R.id.iv_topicsymbol)!!
val mLLSymbols = itemView.findViewById<LinearLayout>(R.id.ll_symbols)!!
}
}
class-rvadapterfaccility(val-myList:ArrayList):RecyclerView.Adapter(){
重写getItemCount():Int{
返回myList.size
}
覆盖onBindViewHolder(holder:ViewHolder,position:Int){
holder.mIVExpandCollapse.setImageDrawable(myList[position].arrowExpandCollapse)
holder.mIVTopic.setImageDrawable(myList[position].TopicSymbol)
holder.mIVLanguage.visibility=?这里是什么?
holder.mIVPets.visibility=?这里是什么?
holder.mIVVerifiedUser.visibility=?这里是什么?
holder.mIVTransport.visibility=?这里是什么?
holder.mIVSeat.visibility=?这里是什么?
holder.mIVFingerprint.visibility=?这里是什么?
holder.mIVDeparture.visibility=?这里是什么?
holder.mIVArrival.visibility=?这里是什么?
}
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val v=LayoutFlater.from(parent.context)。充气(R.layout.cv\u facility\u信息,parent,false)
返回视窗支架(v)
}
类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
val mCV=itemView.findviewbyd(R.id.cv_设施)
val mIVExpandCollapse=itemView.findviewbyd(R.id.iv_expandcollapsarrow)!!
val mIVTopic=itemView.findViewById(R.id.iv_topicsymbol)!!
val mLLSymbols=itemView.findviewbyd(R.id.ll_符号)!!
}
}
可用于设置视图可见性的选项有可见
、不可见
和消失
。在您的情况下,您可能希望根据布尔值设置可见
或消失
:
holder.mIVLanguage.visibility = if (myList[position].displaySymbolLanguage) View.VISIBLE else View.GONE
正如我在评论中所说,可见性需要int,而不是boolean
您只需检查(myList[position].displaySymbolLanguage)
的值,然后决定是将mIVLanguage
View.VISIBLE还是View.GONE。下面是我要说的:
holder.mIVLanguage.visibility = if(myList[position].displaySymbolLanguage) View.VISIBLE else View.GONE
我希望这有帮助。。快乐的编码 可见性需要整数,而不是布尔值。@我刚刚回滚了您的编辑。虽然表达式非常好,但在布尔值上使用它们过于冗长,不会产生任何好处。