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

时间:2019-01-12 16:47:11

标签: android kotlin android-recyclerview android-adapter recycler-adapter

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

enter image description here

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)

RecyclerView片段类

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

正如我在评论中所述,“可见性”需要一个int而不是一个布尔值。

您可以简单地检查(myList[position].displaySymbolLanguage)的值并决定是否将mIVLanguage设置为View.VISIBLE或View.GONE。这就是我在说的:

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

我希望这会有所帮助。.编码愉快!