在 ArrayAdapter 上使用 ViewBinding

我正在尝试重构我的应用程序以使用 ViewBinding 。我经历了所有的片段和活动;但是,我有一个 ArrayAdapter 我不确定使用视图绑定来防止内存泄漏的正确约定。

在 ArrayAdapter 中使用 viewbinding 的正确方法是什么?

我一直在使用这种方法来处理片段:

private var _binding: BINDING_FILE_NAME? = null
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    _binding = BINDING_FILE_NAME.inflate(inflater, container, false)
    return binding.root
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

我这样称呼我的适配器:

   var myadapter : MyCustomAdapter = MyCustomAdapter(requireContext(), R.layout.row_autocomplete_item, myListOfStrings())

MyCustomAdapter


class MyCustomAdapter(ctx: Context, private val layout: Int, private val allItems: List<String>) : ArrayAdapter<String>(ctx, layout, allItems) {

    var filteredItems: List<String> = listOf()

    override fun getCount(): Int = filteredItems.size

    override fun getItem(position: Int): String = filteredItems[position]

    @SuppressLint("SetTextI18n")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = convertView ?: LayoutInflater.from(parent.context).inflate(layout, parent, false)

        val item = filteredItems[position]

        view.apply {
            // HERE IS WHERE I AM NEEDING TO BIND THE VIEW
            tvName?.text = item
        }

        return view
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults) {
                @Suppress("UNCHECKED_CAST")
                filteredItems = filterResults.values as List<String>
                notifyDataSetChanged()
            }

            override fun performFiltering(charSequence: CharSequence?): FilterResults {
                val queryString = charSequence?.toString()?.lowercase(Locale.ROOT)

                val results = FilterResults()

                results.values = if (queryString == null || queryString.isEmpty())
                    allItems
                else
                    allItems.filter {
                        it.lowercase(Locale.ROOT).contains(queryString)
                    }
                return results
            }
        }
    }

}
stack overflow Using ViewBinding on an ArrayAdapter
原文答案

答案:

作者头像

我确实喜欢这个,它的工作。但我不确定,这是否正确

 override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    val binding: LayoutCustomSpinnerBinding
    var row = convertView

    if (row == null) {
        val inflater =
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        binding = LayoutCustomSpinnerBinding.inflate(inflater, parent, false)
        row = binding.root
    } else {
        binding = LayoutCustomSpinnerBinding.bind(row)
    }
    binding.txtContent.text = spinnerList[position].ValueData
    return row
}
作者头像
class HoursAdapter(private val hoursList: List<HoursItem>)
   :RecyclerView.Adapter<HoursAdapter.HoursViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 
HoursViewHolder {
    val binding = HoursListItemsBinding
        .inflate(LayoutInflater.from(parent.context), parent, false)
    return HoursViewHolder(binding)
}

override fun getItemCount() = hoursList.size

override fun onBindViewHolder(holder: HoursViewHolder, position: Int) {
    with(holder){
        with(hoursList[position]) {
            binding.topLearnerName.text = name
            val hours = "$hours learning hours, $country"
            binding.topLearnerTime.text = hours
            GlideApp.with(holder.itemView.context)
                .load(badgeUrl)
                .into(binding.topLearnerImage)

            holder.itemView.setOnClickListener {
                Toast.makeText(holder.itemView.context, hours,
                    Toast.LENGTH_SHORT).show()
            }
        }
    }
}

inner class HoursViewHolder(val binding: HoursListItemsBinding)
    :RecyclerView.ViewHolder(binding.root)

}