我有ToDoList应用程序,当我按下回收项目的底部sheetfragment(ModifyFragment)显示我选择的任务做修改工作。但更新过程没有完成,因为我离开主页片段和重新创建。但如果我离开应用程序,并再次回来(当oncreate再次工作),我可以看到我的变化。我分享我的代码:
My TaskViewModel ( which is sharedviewmodel for both my home fragment and my bottom sheet fragment :
@HiltViewModel
class TaskViewModel @Inject constructor(private var tRepo: TaskRepository) : ViewModel() {
val notesList = MutableLiveData<List<Task>>()
init {
loadAllTask()
}
fun loadAllTask() {
CoroutineScope(Dispatchers.Main).launch {
notesList.postValue(tRepo.getAllTask())
}
}
fun delete(task: Task) = CoroutineScope(Dispatchers.Main).launch {
tRepo.delete(task)
loadAllTask()
}
fun update(task: Task) = CoroutineScope(Dispatchers.Main).launch{
tRepo.update(task)
}
fun search(searchText : String) = CoroutineScope(Dispatchers.Main).launch {
notesList.value = tRepo.search(searchText)
}
}
字符串
我的底表片段:
class ModifyFragment(val task: Task) : BottomSheetDialogFragment() {
private lateinit var binding: FragmentModifyBinding
private val viewModel: TaskViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentModifyBinding.inflate(inflater, container, false)
val date = task.date
val priority = task.priority
val taskCurrent = task.task
binding.date.text = date
binding.task.setText(taskCurrent)
binding.add.setOnClickListener {
val task = Task(task.id,priority,binding.task.text.toString(),task.date,task.isDone)
viewModel.update(task)
dismiss()
}
return binding.root
}
}
型
家园碎片:
@AndroidEntryPoint
class HomeFragment : Fragment() {
private lateinit var binding: FragmentHomeBinding
private lateinit var viewmodel: TaskViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentHomeBinding.inflate(inflater, container, false)
val snapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(binding.recyclerDays)
binding.fab.setOnClickListener {
Navigation.findNavController(it).navigate(R.id.toSave)
}
binding.search.setOnQueryTextListener(object : OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
query?.let { viewmodel.search(it) }
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
newText?.let { viewmodel.search(it) }
return true
}
})
val adapter = TaskAdapter(requireContext(), emptyList(), viewmodel, this)
binding.recyclerTask.adapter = adapter
viewmodel.notesList.observe(viewLifecycleOwner) {
Log.e("tag","observer")
adapter.updateList(it)
}
return binding.root
}
型
另外,我想分享我的适配器,我已经使用了updateList方法(也许问题是),因为在其他方式视图模型没有观察我的变化,当我想创建新的任务.适配器:
class TaskAdapter(private val mContext : Context, private var list : List<Task>, private val viewModel: TaskViewModel, private val fragment : Fragment) : RecyclerView.Adapter<TaskAdapter.TaskDesignHolder>(){
inner class TaskDesignHolder( val binding : TaskItemBinding) : RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskDesignHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = TaskItemBinding.inflate(inflater,parent,false)
return TaskDesignHolder(binding)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: TaskDesignHolder, position: Int) {
val b = holder.binding
val itemlist = list[position]
b.task.text = itemlist.task
b.time.text = itemlist.date
if (itemlist.isDone){
b.task.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
b.time.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
b.checkbox.isChecked = true
}
b.root.setOnClickListener{
if (!itemlist.isDone) {
val bottomSheetFragment = ModifyFragment(itemlist)
bottomSheetFragment.show(fragment.childFragmentManager, bottomSheetFragment.tag)
}
else{
Toast.makeText(mContext,"You can't modify finished tasks.",Toast.LENGTH_SHORT).show()
}
}
b.priority.backgroundTintList = when(itemlist.priority){
"Low" -> ContextCompat.getColorStateList(mContext, R.color.green)
"Medium" -> ContextCompat.getColorStateList(mContext, R.color.yellow)
"High" -> ContextCompat.getColorStateList(mContext, R.color.red)
else -> ContextCompat.getColorStateList(mContext, R.color.green)
}
b.delete.setOnClickListener {
viewModel.delete(itemlist)
}
b.checkbox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked){
b.task.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
b.time.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
val task = Task(itemlist.id,itemlist.priority,itemlist.task,itemlist.date,true)
viewModel.update(task)
}
else{
b.task.paintFlags = 0
b.time.paintFlags = 0
val task = Task(itemlist.id,itemlist.priority,itemlist.task,itemlist.date,false)
viewModel.update(task)
}
}
}
fun updateList(list : List<Task>){
this.list = list
notifyDataSetChanged()
}
}
型
我试着用OnResume获取我的数据,也在我的更新函数中调用loadAllTask来再次检索。不起作用。
1条答案
按热度按时间sg2wtvxw1#
我看到了VM和DI的一些问题,前者是罪魁祸首。你的TaskViewmodel没有在HomeFragment中示例化。你应该像在第一个片段中一样示例化它。
private瓦尔viewModel:TaskViewModel by activityViewModels()
如果您打算将对象注入到片段中,还应该记得使用@AndroidEntryPoint注解片段。否则您的应用可能会崩溃。