繁体   English   中英

单击警报生成器 Android 中的项目时更改/更新回收站查看项目

[英]Change/Update Recycler View Item On Click of Item In Alert Builder Android

我想实现一种排序/过滤系统,其中:

  • 用户单击 AlertBuilder,他会看到“按名称排序”和“按日期排序”。
  • 点击其中任何一个,recyclerview 应该被更新。

我尝试了什么:

音乐数据库

我在哪里获取所有歌曲并根据用户选择进行排序。 (我正在使用 dataStore 来保存用户首选项)

    fun getSongs(): List<Songs> {
    val preferenceStorageImpl = PreferenceStorageImpl(context)
    preferenceStorageImpl.sortOrder().asLiveData().observeForever(Observer {
       if (it == "name"){
            sortMusic = "${MediaStore.Audio.Media.DISPLAY_NAME} ASC"
        }
        else{
            sortMusic = "${MediaStore.Audio.Media.DATE_ADDED} DESC"
        }
    })
    //Get songs from provider
    context.contentResolver.query(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        null,
        sortMusic //Sort in alphabetical or date order based on what was selected (Default - alphabetical).
    ).use

AllSongsAdapter

    private val diffCallback = object : DiffUtil.ItemCallback<Songs>() {
    override fun areItemsTheSame(oldItem: Songs, newItem: Songs): Boolean {          return oldItem.mediaId == newItem.mediaId
    }
    override fun areContentsTheSame(oldItem: Songs, newItem: Songs): Boolean {
        return oldItem.hashCode() == newItem.hashCode()
    }
}
 var songs: List<Songs>
    get() = differ.currentList
    set(value) = differ.submitList(value)

  .......//onbindviewholder
 override fun onBindViewHolder(holder: AllSongsViewHolder, position: Int) 
 {
    val song = songs[position]
    with(holder) {
        with(songs[position]) {
        .......
  }}

歌曲片段

//Observe and show all songs
 mainViewModel.mediaItems.observe(viewLifecycleOwner){ result ->
        when(result.status){
            Status.SUCCESS -> {
                result.data?.let { songs ->
                    allSongsAdapter.songs = songs
                    allSongs = songs
                }
            }
            Status.ERROR -> Unit
            Status.LOADING -> Unit
        }
    }

//AlertBuilder, On click of sort by name or date
 private fun sortByName(){
    val sortOptionName = "name"
    storageViewModel.changeSortOption(sortOptionName)
    allSongsAdapter.notifyDataSetChanged()
}

private fun sortByDateAdded(){
    val sortOptionDate = "date"
    storageViewModel.changeSortOption(sortOptionDate)
    allSongsAdapter.notifyDataSetChanged()
}

存储视图模型

class StorageViewModel @ViewModelInject constructor(private val 
preferenceStorage: PreferenceStorage) : ViewModel() {
val sortOrder = preferenceStorage.sortOrder().asLiveData()
fun changeSortOption(order: String) {
    viewModelScope.launch {
        preferenceStorage.setSortOrder(order)
    }
 }
}

我也试过打电话

  • 当 sortOption 的值更改时,来自 MusicDatabase 的allSongsAdapter.notifyDataSetChanged()

例如

 if (it == "name"){
 sortMusic = "${MediaStore.Audio.Media.DISPLAY_NAME} ASC"
 allSongsAdapter.notifyDataSetChanged()
  }

没用。

当我关闭应用程序并打开时,音乐列表排序良好(根据最后选择的排序选项)

但它不是实时更新的(用户点击按名称或日期排序的那一刻)

您可以在每次要更新时重新创建适配器,或者如果您可以使用diffutil有效地比较重新创建整个回收器视图或使用invalidate()回收器视图,这将重新初始化它

在 kotlin 中重新创建适配器,如下所示:

private fun createAdapter() {
        val mLinearLayoutManager = GridLayoutManager(context, 2)
        mRecyclerViewMediaList.layoutManager = mLinearLayoutManager
        itemListSaved = getListFiles(
            File(
                Environment.getExternalStorageDirectory()
                    .toString() + DIRECTORY_TO_SAVE_MEDIA_NOW
            )
        )!!
          mRecyclerViewMediaList.adapter = mRecyclerViewMediaAdapte  
    }

在您调用 notifydatasetchanged() 的地方调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM