android Fragment不显示从BottomSheetFragment完成的更新(Room + Viewmodel)

xnifntxz  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(155)

我有ToDoList应用程序,当我按下回收项目的底部sheetfragment(ModifyFragment)显示我选择的任务做修改工作。但更新过程没有完成,因为我离开主页片段和重新创建。但如果我离开应用程序,并再次回来(当oncreate再次工作),我可以看到我的变化。我分享我的代码:

  1. My TaskViewModel ( which is sharedviewmodel for both my home fragment and my bottom sheet fragment :
  2. @HiltViewModel
  3. class TaskViewModel @Inject constructor(private var tRepo: TaskRepository) : ViewModel() {
  4. val notesList = MutableLiveData<List<Task>>()
  5. init {
  6. loadAllTask()
  7. }
  8. fun loadAllTask() {
  9. CoroutineScope(Dispatchers.Main).launch {
  10. notesList.postValue(tRepo.getAllTask())
  11. }
  12. }
  13. fun delete(task: Task) = CoroutineScope(Dispatchers.Main).launch {
  14. tRepo.delete(task)
  15. loadAllTask()
  16. }
  17. fun update(task: Task) = CoroutineScope(Dispatchers.Main).launch{
  18. tRepo.update(task)
  19. }
  20. fun search(searchText : String) = CoroutineScope(Dispatchers.Main).launch {
  21. notesList.value = tRepo.search(searchText)
  22. }
  23. }

字符串
我的底表片段:

  1. class ModifyFragment(val task: Task) : BottomSheetDialogFragment() {
  2. private lateinit var binding: FragmentModifyBinding
  3. private val viewModel: TaskViewModel by activityViewModels()
  4. override fun onCreateView(
  5. inflater: LayoutInflater, container: ViewGroup?,
  6. savedInstanceState: Bundle?
  7. ): View {
  8. binding = FragmentModifyBinding.inflate(inflater, container, false)
  9. val date = task.date
  10. val priority = task.priority
  11. val taskCurrent = task.task
  12. binding.date.text = date
  13. binding.task.setText(taskCurrent)
  14. binding.add.setOnClickListener {
  15. val task = Task(task.id,priority,binding.task.text.toString(),task.date,task.isDone)
  16. viewModel.update(task)
  17. dismiss()
  18. }
  19. return binding.root
  20. }
  21. }


家园碎片:

  1. @AndroidEntryPoint
  2. class HomeFragment : Fragment() {
  3. private lateinit var binding: FragmentHomeBinding
  4. private lateinit var viewmodel: TaskViewModel
  5. override fun onCreateView(
  6. inflater: LayoutInflater, container: ViewGroup?,
  7. savedInstanceState: Bundle?
  8. ): View? {
  9. binding = FragmentHomeBinding.inflate(inflater, container, false)
  10. val snapHelper = LinearSnapHelper()
  11. snapHelper.attachToRecyclerView(binding.recyclerDays)
  12. binding.fab.setOnClickListener {
  13. Navigation.findNavController(it).navigate(R.id.toSave)
  14. }
  15. binding.search.setOnQueryTextListener(object : OnQueryTextListener {
  16. override fun onQueryTextSubmit(query: String?): Boolean {
  17. query?.let { viewmodel.search(it) }
  18. return true
  19. }
  20. override fun onQueryTextChange(newText: String?): Boolean {
  21. newText?.let { viewmodel.search(it) }
  22. return true
  23. }
  24. })
  25. val adapter = TaskAdapter(requireContext(), emptyList(), viewmodel, this)
  26. binding.recyclerTask.adapter = adapter
  27. viewmodel.notesList.observe(viewLifecycleOwner) {
  28. Log.e("tag","observer")
  29. adapter.updateList(it)
  30. }
  31. return binding.root
  32. }


另外,我想分享我的适配器,我已经使用了updateList方法(也许问题是),因为在其他方式视图模型没有观察我的变化,当我想创建新的任务.适配器:

  1. class TaskAdapter(private val mContext : Context, private var list : List<Task>, private val viewModel: TaskViewModel, private val fragment : Fragment) : RecyclerView.Adapter<TaskAdapter.TaskDesignHolder>(){
  2. inner class TaskDesignHolder( val binding : TaskItemBinding) : RecyclerView.ViewHolder(binding.root)
  3. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskDesignHolder {
  4. val inflater = LayoutInflater.from(parent.context)
  5. val binding = TaskItemBinding.inflate(inflater,parent,false)
  6. return TaskDesignHolder(binding)
  7. }
  8. override fun getItemCount(): Int {
  9. return list.size
  10. }
  11. override fun onBindViewHolder(holder: TaskDesignHolder, position: Int) {
  12. val b = holder.binding
  13. val itemlist = list[position]
  14. b.task.text = itemlist.task
  15. b.time.text = itemlist.date
  16. if (itemlist.isDone){
  17. b.task.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
  18. b.time.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
  19. b.checkbox.isChecked = true
  20. }
  21. b.root.setOnClickListener{
  22. if (!itemlist.isDone) {
  23. val bottomSheetFragment = ModifyFragment(itemlist)
  24. bottomSheetFragment.show(fragment.childFragmentManager, bottomSheetFragment.tag)
  25. }
  26. else{
  27. Toast.makeText(mContext,"You can't modify finished tasks.",Toast.LENGTH_SHORT).show()
  28. }
  29. }
  30. b.priority.backgroundTintList = when(itemlist.priority){
  31. "Low" -> ContextCompat.getColorStateList(mContext, R.color.green)
  32. "Medium" -> ContextCompat.getColorStateList(mContext, R.color.yellow)
  33. "High" -> ContextCompat.getColorStateList(mContext, R.color.red)
  34. else -> ContextCompat.getColorStateList(mContext, R.color.green)
  35. }
  36. b.delete.setOnClickListener {
  37. viewModel.delete(itemlist)
  38. }
  39. b.checkbox.setOnCheckedChangeListener { _, isChecked ->
  40. if (isChecked){
  41. b.task.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
  42. b.time.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
  43. val task = Task(itemlist.id,itemlist.priority,itemlist.task,itemlist.date,true)
  44. viewModel.update(task)
  45. }
  46. else{
  47. b.task.paintFlags = 0
  48. b.time.paintFlags = 0
  49. val task = Task(itemlist.id,itemlist.priority,itemlist.task,itemlist.date,false)
  50. viewModel.update(task)
  51. }
  52. }
  53. }
  54. fun updateList(list : List<Task>){
  55. this.list = list
  56. notifyDataSetChanged()
  57. }
  58. }


我试着用OnResume获取我的数据,也在我的更新函数中调用loadAllTask来再次检索。不起作用。

sg2wtvxw

sg2wtvxw1#

我看到了VM和DI的一些问题,前者是罪魁祸首。你的TaskViewmodel没有在HomeFragment中示例化。你应该像在第一个片段中一样示例化它。
private瓦尔viewModel:TaskViewModel by activityViewModels()
如果您打算将对象注入到片段中,还应该记得使用@AndroidEntryPoint注解片段。否则您的应用可能会崩溃。

相关问题