我已经遵循了本教程:https://developer.android.com/codelabs/android-room-with-a-view-kotlin。
代码工作完美,但我想将 RecyclerView 移动到片段。 现在列表会无限期加载,并且 Logcat 中没有错误。
我在
words
功能中记录了 observe
。
它可以很好地记录 Word 对象...
移动到片段时我对代码进行了一些更改:
viewModels
-> activityViewModels
application
-> requireActivity().application
owner: this
-> viewLifecycleOwner
所有这些都没有帮助。
这是我的代码:
主要活动
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
WordListFragment
class WordListFragment: ListFragment(){
private val wordViewModel: WordViewModel by activityViewModels {
WordViewModelFactory((requireActivity().application as WordsApplication).repository)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val recyclerView =
inflater.inflate(R.layout.fragment_recycler, container, false) as RecyclerView
val adapter = WordListAdapter()
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireActivity())
wordViewModel.allWords.observe(viewLifecycleOwner) { words ->
words.let { adapter.submitList(it) }
}
return super.onCreateView(inflater, container, savedInstanceState)
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.android.roomwordssample.WordListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_recycler.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/recyclerview_item"
android:padding="@dimen/big_padding"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
其余部分与原始版本相同(https://github.com/android/codelab-android-room-with-a-view/archive/kotlin.zip)
您只需在 onCreateView 中膨胀视图,然后在 onViewCreated 中开始执行与 recyclerView 相关的任何操作。
这是 WordListFragment 的代码:
package com.example.android.roomwordssample
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.ListFragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class WordListFragment: ListFragment(){
private val wordViewModel: WordViewModel by activityViewModels {
WordViewModelFactory((requireActivity().application as WordsApplication).repository)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_recycler, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val recyclerView: RecyclerView = view.findViewById(R.id.recyclerview)
val adapter = WordListAdapter()
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireActivity())
wordViewModel.allWords.observe(viewLifecycleOwner) { words ->
words.let { adapter.submitList(it) }
}
}
}