Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从RecyclerView适配器读取布尔值_Android_Kotlin_Android Adapter_Android Recyclerview - Fatal编程技术网

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

我希望这有帮助。。快乐的编码

可见性需要整数,而不是布尔值。@我刚刚回滚了您的编辑。虽然
表达式非常好,但在布尔值上使用它们过于冗长,不会产生任何好处。