如何使 searchview 用于查找我的虚拟数据

问题描述 投票:0回答:1

我被卡住了,我做了一些搜索视图来查找我的虚拟项目,但仍然不起作用,有人知道它是如何工作的吗?这是我的虚拟数据

interface DummyMusicDataSource {
    fun getMusicData(context: Context): List<Music>
}

class DummyMusicDataSourceImpl() : DummyMusicDataSource {
    override fun getMusicData(context: Context): List<Music> {

        return mutableListOf(
            Music(
                title = "title",
                imgUrl = "https://raw.githubusercontent.com/ryhanhxx/img_asset/main/IMG_MATERI_3.jpg",
                desc = context.getString(R.string.desc_materi_1),
                videoUrl = "https://www.youtube.com/watch?v=XpwWeAsXnvA",
            ),
        )
    }
}

这是我的问题,我不知道如何让我的搜索视图找到我的虚拟数据,我想在搜索视图上输入标题时找到它

class MateriFragment : Fragment() {

    private lateinit var binding: FragmentMateriBinding

    private val adapter: MateriAdapter by lazy {
        MateriAdapter(AdapterLayoutMode.LINEAR){ materi: Materi ->
            navigateToDetailFragment(materi)
        }
    }

    private val datasource: DummyMateriDataSource by lazy {
        DummyMateriDataSourceImpl()
    }

    private fun navigateToDetailFragment(materi: Materi) {
        MateriActivity.startActivity(requireContext(), materi)
    }

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupList()
        setupSwitch()
    }

    private fun setSearchView(){
        //TODO: Searchview for dummy
    }

    private fun setupList() {
        val span = if(adapter.adapterLayoutMode == AdapterLayoutMode.LINEAR) 1 else 2
        binding.rvMateri.apply {
            layoutManager = GridLayoutManager(requireContext(),span)
            adapter = [email protected]
        }
        adapter.setItems(datasource.getMateriData(requireContext()))
    }

    private fun setupSwitch() {
        binding.swGrid.setOnCheckedChangeListener { _, isChecked ->
            (binding.rvMateri.layoutManager as GridLayoutManager).spanCount = if (isChecked) 2 else 1
            adapter.adapterLayoutMode = if(isChecked) AdapterLayoutMode.GRID else AdapterLayoutMode.LINEAR
            adapter.refreshList()
        }
    }
}

这是我的 recycleview 的 MateriAdapter

class MateriAdapter (
    var adapterLayoutMode: AdapterLayoutMode,
    private val onClickListener: (Materi) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val dataDiffer = AsyncListDiffer(this, object : DiffUtil.ItemCallback<Materi>() {
        override fun areItemsTheSame(oldItem: Materi, newItem: Materi): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Materi, newItem: Materi): Boolean {
            return oldItem.hashCode() == newItem.hashCode()
        }
    })

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            AdapterLayoutMode.GRID.ordinal -> (GridMateriViewHolder(
                binding = ItemCardGridBinding.inflate(
                    LayoutInflater.from(parent.context), parent, false
                ), onClickListener
            ))

            else -> {
                LinearMateriViewHolder(
                    binding = ItemCardListBinding.inflate(
                        LayoutInflater.from(parent.context), parent, false
                    ), onClickListener
                )
            }
        }
    }

    override fun getItemCount(): Int = dataDiffer.currentList.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as ViewHolderBinder<Materi>).bind(dataDiffer.currentList[position])
    }

    override fun getItemViewType(position: Int): Int {
        return adapterLayoutMode.ordinal
    }

    fun setItems(data: List<Materi>) {
        dataDiffer.submitList(data)
    }

    fun refreshList() {
        notifyItemRangeChanged(0, dataDiffer.currentList.size)
    }
}
android kotlin search searchview dummy-variable
1个回答
0
投票

首先,尝试在 MusicDataSource 类中添加一个函数来根据标题检索数据

fun searchMusicByTitle(query: String): List<Music> {
    return getMusicData().filter { it.title.contains(query, ignoreCase = true) }
}

然后更新 searchView 函数,如下所示

private fun setSearchView() {
    // Assuming you have a searchView with id searchView
    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
            return false
        }

        override fun onQueryTextChange(newText: String?): Boolean {
            newText?.let {
                val filteredMusicList = datasource.searchMusicByTitle(it)
                adapter.setItems(filteredMusicList)
            }
            return true
        }
    })
}

© www.soinside.com 2019 - 2024. All rights reserved.