我有三个带有底部导航栏的片段应用程序,我使用NavigationUI进行切换。我还有一个视图模型,它创建数据(来自资产傻瓜(,片段观察我用来填充回收器视图的实时数据的数组列表。
我的问题是,每当我切换片段时,当片段被重新创建时,我不希望每次重新创建片段时都进行数据检索。因此使用了视图模型。但在我的情况下,视图模型中的数据不会被保留。我已经附上了片段和视图模型代码。我不确定这里出了什么问题。
我试着记录了aaraylist中的条目数量,如果我不调用填充arraylist的例程,它会返回0。
歌曲片段
private const val TAG = "Songs Fragment"
class SongsFragment : Fragment(), android.widget.SearchView.OnQueryTextListener {
private val viewmodel: SongListViewModel by lazy { ViewModelProviders.of(this).get(SongListViewModel::class.java)}
private val songListAdapter = SongListAdapter(arrayListOf())
private var raga = "All"
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
Log.d(TAG, "onCreateView called")
return inflater.inflate(R.layout.fragment_songs, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.d(TAG, "onViewCreated called")
val context: FragmentActivity? = activity
(activity as AppCompatActivity).supportActionBar?.subtitle = "Songs"
val assetsPath: AssetManager? = context?.assets
val assetList = assetsPath?.list("")
if (assetList != null) {
for (item in assetList)
Log.d("SongsFragment", assetsPath.toString() + item)
}
arguments?.let {
raga = SongsFragmentArgs.fromBundle(it).raga
}
Log.d("Song Fragment", "Raga passed: " + raga)
Log.d(TAG, "Number of songs: " + viewmodel.songList.size.toString())
if (raga == "All") {
viewmodel.allSongs()
viewmodel.allSongs(assetsPath!!)
} else {
viewmodel.songsForRaga(assetsPath!!, raga)
}
Log.d(TAG, ": Songlist size: " + viewmodel.songList.size.toString())
songList_RecyclerView.apply {
layoutManager = LinearLayoutManager(context)
adapter = songListAdapter
}
songSearchView.setOnQueryTextListener(this)
observeViewModel()
}
override fun onQueryTextSubmit(query: String): Boolean {
return false
}
override fun onQueryTextChange(newText: String): Boolean {
Log.i("Song Fragment", "Text change:" + newText.length.toString())
viewmodel.songSearchFilter(newText)
return false
}
fun observeViewModel() {
viewmodel.songs.observe(this, Observer { songs ->
songs?.let {
Log.d("Song Fragment", "ObserveViewModel")
songListAdapter.updateSongList(songs)
}
})
}
}
SongListViewModel
private const val TAG = "SongListViewModel"
class SongListViewModel : ViewModel() {
val songs = MutableLiveData<ArrayList<Song>>()
var songList = arrayListOf<Song>()
fun allSongs() {
songList = getAllSongs()
songs.value = ArrayList(songList.sortedWith(compareBy({ it.songName })))
}
fun allSongs(assetsPath: AssetManager) {
Log.d(TAG, "allSongs called")
Log.d(TAG, "Number of songs: " + songList.size.toString())
getAllSongs(assetsPath)
songs.value = ArrayList(songList.sortedWith(compareBy({ it.songName })))
}
fun songSearchFilter(text: String) {
var filteredList = arrayListOf<Song>()
filteredList.clear()
if (text.length != 0) {
for (song in songList) {
if (song.songName.toLowerCase().contains(text)) {
filteredList.add(song)
}
}
songs.value = ArrayList(filteredList.sortedWith(compareBy({ it.songName })))
} else {
songs.value = ArrayList(songList.sortedWith(compareBy({ it.songName })))
}
}
fun songsForRaga(assetsPath: AssetManager, raga: String) {
Log.d(TAG, "songsForRaga called")
var filteredList = arrayListOf<Song>()
filteredList.clear()
allSongs(assetsPath)
for (song in songList) {
if (song.raga == raga) {
filteredList.add(song)
}
}
songs.value = ArrayList(filteredList.sortedWith(compareBy({ it.songName })))
}
fun getAllSongs(assetsPath: AssetManager) {
Log.d(TAG, "getAllSongs called")
val bufferedReader = assetsPath.open("test.csv").bufferedReader()
val lineList = mutableListOf<String>()
bufferedReader.useLines { lines -> lines.forEach { lineList.add(it) } }
lineList.forEach {
val parts = it.split(",")
songList.add(Song(parts[0], parts[1], parts[2], parts[3], ""))
}
}
}
通过向视图模型提供者传递片段父活动而不是片段本身来检索视图模型。
ViewModelProviders.of(activity(.get(SongListViewModel::class.java(
我不认为这是一个答案,只是一个小小的解释,你的viewModel生命周期与你的碎片生命周期相连,所以当你更改碎片时,它会被破坏,而关于你的viewModel也会被破坏。解决方案是将创建视图模型转移到活动中,并像Nezih建议的那样进行粘贴,但我不确定这是否是最佳选择。
注意,我们需要在activity scope中创建ViewModel
实例,否则android将创建一个单独的实例,而不是共享同一个实例,我们将无法获得数据。
对于碎片,这样做:
activity?.let {
val viewmodel = ViewModelProviders.of(it).get(SongListViewModel::class.java)
viewmodel.songs.observe(this, Observer { songs ->
songs?.let {
Log.d("Song Fragment", "ObserveViewModel")
songListAdapter.updateSongList(songs)
}
})
}