kotlin 使用适配器从RecycleView筛选searchView

xqkwcwgp  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(179)

适配器类

class AppListAdapter(private val context: Context, initialChecked: ArrayList<String> = arrayListOf()) :  RecyclerView.Adapter<AppListAdapter.AppViewHolder>() {

    public val appList = arrayListOf<ApplicationInfo>()
    private val checkedAppList = arrayListOf<Boolean>()
    private val packageManager: PackageManager = context.packageManager

    init {
        context.packageManager.getInstalledApplications(PackageManager.GET_META_DATA).sortedBy { it.loadLabel(packageManager).toString() }.forEach { info ->
            if (info.packageName != context.packageName) {
                if (info.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
                    appList.add(info)
                    checkedAppList.add(initialChecked.contains(info.packageName))
                }
            }
        }
    }

    inner class AppViewHolder(private val item: ItemAppBinding) : RecyclerView.ViewHolder(item.root) {
        fun bind(data: ApplicationInfo, position: Int) {

            item.txApp.text = data.loadLabel(packageManager)
            item.imgIcon.setImageDrawable(data.loadIcon(packageManager))
            item.cbApp.isChecked = checkedAppList[position]
            item.cbApp.setOnCheckedChangeListener { _, checked ->
                checkedAppList[position] = checked
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppViewHolder {
        return AppViewHolder(ItemAppBinding.inflate(LayoutInflater.from(context), parent, false))
    }

    override fun onBindViewHolder(holder: AppViewHolder, position: Int) {
        holder.bind(appList[position], position)
    }

    override fun getItemCount(): Int {
        return appList.size
    }

主活动

binding.searchView2.setOnQueryTextListener(object : SearchView.OnQueryTextListener{

    override fun onQueryTextSubmit(query: String?): Boolean {
        binding.searchView2.clearFocus()
       // how to write code filtered by query?
        return false
    }

    override fun onQueryTextChange(newText: String?): Boolean {
        // how to write code filtered by newText?
        return false
    }

})

我是Kotlin新手..有谁能帮我吗?

2hh7jdfx

2hh7jdfx1#

我相信你想用“ApplicationInfo.txApp”过滤项目,所以我将为它写代码。
首先,您需要您的适配器类像下面这样扩展Filterable,并添加一个列表来保存所有项:

class AppListAdapter(private val context: Context, initialChecked: ArrayList<String> = arrayListOf()) :  RecyclerView.Adapter<AppListAdapter.AppViewHolder>(), Filterable {
public val appList = arrayListOf<ApplicationInfo>()
public val appListFull = ArrayList<ApplicationInfo>(appList)
// This full list because of when you delete all the typing to searchView
// so it will get back to that full form.

然后重写它的函数并编写自己的过滤器来工作,将以下代码粘贴到适配器中:

override fun getFilter(): Filter {
        return exampleFilter
    }

    private val exampleFilter = object : Filter() {
        override fun performFiltering(constraint: CharSequence?): FilterResults? {
            val filteredList: ArrayList<ApplicationInfo> = ArrayList()
            if (constraint == null || constraint.isEmpty()) {
                // when searchview is empty
                filteredList.addAll(appListFull)
            } else {
                // when you type something
                // it also uses Locale in case capital letters different.
                val filterPattern = constraint.toString().lowercase(Locale.getDefault()).trim()
                for (item in appListFull) {
                    val txApp = item.txApp
                    if (txApp.lowercase(Locale.getDefault()).contains(filterPattern)) {
                        filteredList.add(item)
                    }
                }
            }
            val results = FilterResults()
            results.values = filteredList
            return results
        }

        @SuppressLint("NotifyDataSetChanged") @Suppress("UNCHECKED_CAST")
        override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
            appList.clear()
            appList.addAll(results!!.values as ArrayList<ApplicationInfo>)
            notifyDataSetChanged()
        }
    }

最后,在搜索视图中调用以下filter方法:

binding.searchView2.setOnQueryTextListener(object : SearchView.OnQueryTextListener{

    override fun onQueryTextSubmit(query: String?): Boolean {
        yourAdapter.filter.filter(query)
        yourAdapter.notifyDataSetChanged()
        binding.searchView2.clearFocus()
        return false
    }

    override fun onQueryTextChange(newText: String?): Boolean {
        yourAdapter.filter.filter(newText)
        yourAdapter.notifyDataSetChanged()
        return false
    }

})

这些应该工作,我正在使用类似的东西,如果没有让我知道的问题。

相关问题