android 在数组适配器上使用ViewBinding

aamkag61  于 2023-01-19  发布在  Android
关注(0)|答案(3)|浏览(165)

我正在尝试重构我的应用程序以使用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())

我的自定义适配器

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

}
r8uurelv

r8uurelv1#

我确实喜欢这个,它的工作。但我不确定,是否是正确的方式或没有

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

avwztpqn2#

根据this答案,得出以下结果:
如果convertView不为null,则绑定到该视图。否则进行Inflate。

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    val binding: MyLayoutBinding =
        if (convertView != null) MyLayoutBinding.bind(convertView)
        else MyLayoutBinding.inflate(LayoutInflater.from(context), parent, false)

    // use binding
    val item = getItem(position)
    binding.text = item.name

    return binding.root
}
z3yyvxxp

z3yyvxxp3#

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)

}

相关问题