如何从 RecyclerView 适配器读取布尔值

How to read Boolean values from RecyclerView adapter

对于我的 RecyclerView,我如何使用 Fragment 中声明的 Boolean 值来显示或隐藏我的 RecyclerView 项目模板中的各种项目?在我的适配器 class 中,我尝试使用例如holder.mIVLanguage.visibility = (myList[position].displaySymbolLanguage) 但这没有用。

RecyclerView 项目数据class

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)

RecyclerView片段class

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

RecyclerView 适配器class

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

可用于设置视图可见性的选项是 VISIBLEINVISIBLEGONE。在您的情况下,您可能希望根据布尔值设置 VISIBLEGONE

holder.mIVLanguage.visibility = if (myList[position].displaySymbolLanguage) View.VISIBLE else View.GONE

正如我在评论中所述,Visibility 需要一个 int,而不是 boolean。

您可以简单地检查 (myList[position].displaySymbolLanguage) 的值并决定是 mIVLanguage View.VISIBLE 还是 View.GONE。这就是我要说的:

holder.mIVLanguage.visibility = if(myList[position].displaySymbolLanguage) View.VISIBLE else View.GONE

希望对您有所帮助.. 编码愉快!