繁体   English   中英

在 MVVM NestedRecyclerview 中遇到困难

[英]having difficulties in MVVM NestedRecyclerview

我即将使用 Nested RecyclerView 创建一个日记应用程序。

在此处输入图像描述

从图片中可以看出,我尝试打印出特定日期的数据。

所以我做出的选择是……首先,从房间里的数据中,在String Type中只选择日期,并交付给Parent RecyclerView。

根据那个日期,出来的信息......我将把它交付给 Child RecyclerView。

但是,在这种情况下,如何编写视图 Model 是所有要做的事情。 我不知道。 一天后我也记不起来了。

我希望我能得到你的帮助。

下面是附上的代码。

    // DAO
    @Query("SELECT DISTINCT start_time FROM task ORDER BY start_time ASC")
    fun getParentTask() : LiveData<List<String>>

    @Query("SELECT * FROM TASK WHERE start_time = :start_time")
    fun getChildTask(start_time : String) : List<TaskEntity>
   // ViewModel
class ListViewModel(application : Application) : AndroidViewModel(application){

    val readAllData : LiveData<List<TaskEntity>>
    val readParentData : LiveData<List<String>>
    private val repository : ListRepository

    // getter, setter
    private val _currentData = MutableLiveData<List<TaskEntity>>()
    val currentData : LiveData<List<TaskEntity>>
        get() = _currentData

    init {
        val listDAO = ListDatabase.INSTANCE!!.listDAO()
        repository = ListRepository(listDAO)
        readAllData = repository.readAllData
        readParentData = repository.readParentData
    }

    fun getChildTask(start_time : String) {
        var tmp = repository.readChildData(start_time)
        _currentData.postValue(tmp)
    }
}
   // Fragment

    private val listViewModel : ListViewModel by viewModels()
    private val parentAdapter : ListParentAdapter by lazy { ListParentAdapter(requireContext(), listViewModel, viewLifecycleOwner)}

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.listRcview.apply {
            // this.addItemDecoration(dividerItemDecoration)
            this.layoutManager = LinearLayoutManager(requireContext())
            this.addItemDecoration(RecyclerViewDecoration(30))
            this.adapter = parentAdapter
        }

        listViewModel.readParentData.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
            parentAdapter.setParent(it)
        })
     }
    // parent
    override fun onBindViewHolder(holder: ListParentViewHolder, position: Int) {
        val parentDate = parentList[position]
        val childAdapter: ListChildAdapter by lazy { ListChildAdapter(context, listViewModel) }

        holder.date.text = parentDate
        val itemTouchHelper = ItemTouchHelper(swipeHelperCallback)
        itemTouchHelper.attachToRecyclerView(holder.child)

        holder.child.apply {
            this.addItemDecoration(divideItemDecoration)
            this.layoutManager = LinearLayoutManager(context)
            // this.addItemDecoration(RecyclerViewDecoration(20))
            this.adapter = childAdapter
        }

        listViewModel.readAllData.observe(lifecycleOwner, Observer {
            ???
        })

        listViewModel.currentData.observe(lifecycleOwner, androidx.lifecycle.Observer {
            ???
        })
    }


暂无
暂无

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

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