RecyclerView,数据绑定和ViewModel没有数据

时间:2018-09-25 16:51:10

标签: android android-recyclerview kotlin android-databinding android-viewmodel

Android开发的新手,并试图将我的头放在最新的Architecture Components上。使用Android Studio 3.2,Room,LiveData,ViewModel,Data Binding和RecyclerView,我与Data Binding的战斗已经有好几天了。我的活动中有一个片段,应该显示ViewModel / Query中的数据的RecyclerView部分为空白/空。即使相关的Room查询有效并返回项目,适配器的getItemCount中的Log.d也会显示零项目。

请用线索棒打我,让我知道我在想什么。我的代码的相关部分:

实体与道

import org.threeten.bp.Instant

data class ActionDetails(val time: Instant,
                     val firstName: String,
                     ... )

@Query("SELECT time, first_name as firstName...")
fun liveStatus(): LiveData<List<ActionDetails>>

片段类

class MainFragment : Fragment() {
...
private lateinit var viewModel: MainViewModel
private lateinit var adapter: ViewAdapter

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

    val factory = Utilities.provideMainViewModelFactory(context)
    viewModel = ViewModelProviders.of(this, factory).get(MainViewModel::class.java)

    adapter = ViewAdapter(listOf())

    binding.apply {
        rvActionDetails.setHasFixedSize(true)
        rvActionDetails.layoutManager = LinearLayoutManager(context)
        rvActionDetails.adapter = adapter
        vm = viewModel
        setLifecycleOwner(this@MainFragment)
    }

    return binding.root
}

ViewModel

class MainViewModel(private val repository: DataRepository) : ViewModel() {
    val actions: LiveData<List<ActionDetails>> = repository.liveStatus()
}

适配器

import ...FragmentActionDetailBinding

class ViewAdapter(private val actions: List<ActionDetails>) : RecyclerView.Adapter<ViewAdapter.ViewHolder>() {
private val TAG = this::class.java.simpleName

class ViewHolder(val binding: FragmentActionDetailBinding) : RecyclerView.ViewHolder(binding.root) {
    fun bind(action: ActionDetails) {
            binding.apply {
            vm = action
            executePendingBindings()
            }
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val binding = FragmentActionDetailBinding.inflate(inflater, parent, false)
    return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.bind(actions[position])
}

override fun getItemCount(): Int {
    Log.d(TAG, "Adapter has ${actions.size} items!")
    return actions.size
}
}

主要片段

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>
    <variable
        name="vm" type=".MainViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainFragment" >
    ...
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_action_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/fragment_action_detail" />

</androidx.constraintlayout.widget.ConstraintLayout>

片段XML

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>
    <variable
        name="vm" type=".ActionDetails" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".ActionDetailFragment" >

    <TextView
        android:id="@+id/first_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{vm.firstName}"
        tools:text="John"/>
    ...
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

最终解决了该问题。不确定究竟是什么解决了它—我认为这是由于在片段中的binding.apply中配置RecyclerView之前,先移动了ViewModel并运行了executePendingBindings(),但我不确定—因为在学习之后我还做了一些其他更改关于BindingAdapters。

如果对其他人有帮助,我的更改将在下面显示-我也希望就如何改善此问题发表评论。

干杯。

片段类

class MainFragment : Fragment() {
...

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View {
...
    binding.apply {
        //Need to do this before configuring the RecyclerView
        vm = viewModel
        setLifecycleOwner(this@MainFragment)
        executePendingBindings()

        rvActionDetails.setHasFixedSize(true)
        rvActionDetails.layoutManager = LinearLayoutManager(context)
        rvActionDetails.adapter = adapter
    }

    return binding.root
}

适配器

import ...FragmentActionDetailBinding

class ViewAdapter(private val actions: List<ActionDetails>) : RecyclerView.Adapter<ViewAdapter.ViewHolder>() {
...
    override fun setData(data: List<ActionDetails>) {
        actions = data
        notifyDataSetChanged()
}

绑定适配器

@BindingAdapter("listData")
fun <T> setRecyclerViewList (recyclerView: RecyclerView, data: T) {
    if (recyclerView.adapter is BindableListAdapter<*>) {
        (recyclerView.adapter as BindableListAdapter<T>).setData(data)
    }
}

interface BindableListAdapter<T> {
    fun setData(data: T)
}

主要片段

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>
    <variable
        name="vm" type=".MainViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    ...
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_action_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:listData="@{vm.actions}"
        tools:listitem="@layout/fragment_action_detail" />

</androidx.constraintlayout.widget.ConstraintLayout>

片段XML

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>
    <variable
        name="vm" type=".ActionDetails" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".ActionDetailFragment" >

    <TextView
        android:id="@+id/first_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{vm.firstName}"
        tools:text="John"/>
    ...
</LinearLayout>