我创建了一个简单的应用程序,该应用程序从API加载了一个随机笑话,并将其显示在TextView中。出于学习原因,我已经使用数据绑定实现了该应用程序。最初,我创建了一个ViewModel,可以从API获取数据
class RandomJokeViewModel: ViewModel() {
private val jokeService = RetrofitService()
fun getRandomJoke(): MutableLiveData<RandomJoke> ? {
Log.e("getRandomJokeData", "yes")
return jokeService.loadRandomJoke()
}
在我的片段中,我调用该方法来加载笑话并更新视图
class RandomJokeFragment : Fragment() {
private lateinit var binding: FragmentRandomJokeBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(layoutInflater, R.layout.fragment_random_joke, container, false)
binding.lifecycleOwner = this
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_random_joke, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
loadRandomJoke()
}
private fun loadRandomJoke() {
Log.e("getAndroidVersion", "yes")
val randomJokeViewModel =
ViewModelProviders.of(requireActivity()).get(RandomJokeViewModel::class.java)
randomJokeViewModel.getRandomJoke()?.observe(this, Observer<RandomJoke> { randomJoke ->
binding.randomJoke = randomJoke
Log.e("RandomJokeFragment", "This is your Joke: ${randomJoke.value}")
})
}
我的数据通话如下:
data class RandomJoke (
@SerializedName("categories") val categories: ArrayList<String>,
@SerializedName("created_at") val createdAt: String,
@SerializedName("icon_url") val iconUrl: String,
@SerializedName("id") val id: String,
@SerializedName("updated_at:") val updatedAt: String,
@SerializedName("url") val url: String,
@SerializedName("value") val value: String)
在“片段布局”中,我定义了这样的数据绑定资源
<data>
<variable
name="randomJoke" type="com.example.jokegenerator.data.remote.response.RandomJoke"
/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginStart="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="@{randomJoke.value}"
android:textColor="#ffff"
tools:text="Hello World"/>
</androidx.constraintlayout.widget.ConstraintLayout>
因此,当我运行该应用程序时,我的TextView为空。在Logcat和Debugger中,我验证了randomJoke
响应中是否包含数据。为什么获取数据后视图没有更新?我想念什么?
答案 0 :(得分:3)
不能完全确定这是原因,但是:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(layoutInflater, R.layout.fragment_random_joke, container, false)
binding.lifecycleOwner = this
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_random_joke, container, false)
}
在这里您将布局膨胀2次。
请尝试这样:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// This will inflate your layout. You just have to return the root view.
binding = DataBindingUtil.inflate(layoutInflater, R.layout.fragment_random_joke, container, false)
binding.lifecycleOwner = this
return binding.root // Returning your root view
}
数据绑定可能是两种布局的混合。
答案 1 :(得分:2)
在您的onCreateView()中,您将返回:
return inflater.inflate(R.layout.fragment_random_joke, container, false)
但您需要返回:
return binding.root