android - 如何在 MVVM 架构的 RecyclerView 适配器中观察 LiveData?

标签 android kotlin mvvm android-recyclerview android-livedata

我有一个 RecyclerView 适配器和它的项目中的一个按钮。

当我点击按钮时,我想从服务器中删除它的项目,然后从 RecyclerView 中删除。

我想通过观察 LiveData 来做到这一点(当它从服务器中删除时,我必须将它从回收器 View 中删除,因此我需要服务器的结果)

最佳实践方法是什么 - 我必须在 fragment 中观察并将监听器传递给适配器并在 fragment 中实现它,当用户单击按钮时调用 fragment 中的方法或者有更好的方法这样做?

最佳答案

找了好几个帖子,终于找到了推荐的解决方案。 第 1 步:在您的适配器中声明一个接口(interface),如下所示:

class AddExpenseLabelAdapter(
    val items: List<LabelResponse>, 
    val context: Context, 
    val listener: OnLabelClickListener
) : RecyclerView.Adapter<AddExpenseLabelAdapter.ViewHolder>() {

    interface OnLabelClickListener {
        fun onLabelDeleteButtonClicked(request : SubCategoryLabelRequest)
    }

    lateinit var binding: ItemListExpenseAddLabelBinding

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

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

    override fun getItemCount(): Int = items.size

    inner class ViewHolder(val binding: ItemListExpenseAddLabelBinding) : RecyclerView.ViewHolder(binding.root), OnClickListener {
        lateinit var item: LabelResponse
        fun bind(item: LabelResponse) {
            this.item = item
            binding.itemListLabelLayout.setBackgroundColor(Color.parseColor("#" + item.color))
            binding.labelResponse = item
            binding.onClickListener = this
            binding.executePendingBindings()
        }

        override fun onClick(view: View) {
            if (view.id == binding.itemListLabelLayout.id) {
                val subCategoryLabelRequest = SubCategoryLabelRequest(item.id)
                listener.onLabelDeleteButtonClicked(subCategoryLabelRequest)
            }
        }
    }
}

第 2 步:在您的 View 中实现接口(interface)并将其传递给您的适配器,如下所示:

class AddExpenseLabelDialog : DialogFragment(), AddExpenseLabelAdapter.OnLabelClickListener {

    lateinit var binding: DialogAddExpenseLabelBinding

    lateinit var view: Any

    var expenseId: Int = 0
    var categoryId: Int = 0

    lateinit var application: MyApplication

    lateinit var addExpenseLabelViewModel: AddExpenseLabelViewModel

    fun newInstance(expenseId: Int, categoryId: Int): AddExpenseLabelDialog = 
        AddExpenseLabelDialog().also { fragment ->
            arguments = Bundle().also { bundle ->
                bundle.putInt("expenseId", expenseId)
                bundle.putInt("categoryId", categoryId)
            }
        }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate(layoutInflater, R.layout.dialog_add_expense_label, container, false)
        addExpenseLabelViewModel = ViewModelProviders.of(this).get(AddExpenseLabelViewModel::class.java)
        expenseId = arguments!!.getInt("expenseId")
        categoryId = arguments!!.getInt("categoryId")
        initialize()
        view = binding.root
        return view as View
    }

    fun initialize() {

        binding.labelRec.layoutManager = LinearLayoutManager(context)
        addExpenseLabelViewModel.liveData.observe(this, Observer { response ->
            binding.labelRec.adapter = AddExpenseLabelAdapter(response as ArrayList<LabelResponse>, context!!, this)
        })
    }

    override fun onLabelDeleteButtonClicked(request : SubCategoryLabelRequest) {
        addExpenseLabelViewModel.createExpenseLabel(categoryId, expenseId, request).observe(this, Observer { response ->
            when (response?.status) {
                Status.LOADING -> Toast.makeText(activity, "LOADING", Toast.LENGTH_SHORT).show()
                Status.SUCCESS -> {
                    dismiss()
                    Toast.makeText(activity, "SUCCESS", Toast.LENGTH_SHORT).show()
                }
                else -> Toast.makeText(activity, InjectorUtil.convertCodeToMessage(response?.error?.code!!), Toast.LENGTH_SHORT).show()
            }
        })
    }
}

关于android - 如何在 MVVM 架构的 RecyclerView 适配器中观察 LiveData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54109820/

相关文章:

android - 通过 XML 单击时使 Android 按钮更改背景

c# - 绑定(bind)有错误的验证更新 View

wpf - 使用 Prism WPF 在 Datagrid 中绑定(bind)命令

wpf - 具有DataTemplate的Window.Content

java - Android将字符串转换为数组字符串

android - 如何从android中的asp页面获取VIEWSTATE和EVENTVALIDATION值

java - 注解处理: how to change annotation target in Kotlin code?

Android Koin,如何使用两个数据源制作存储库?

mongodb - 使用 Spring Data 将 Kotlin 内联类存储到 MongoDB

android - 同一个asynctask可以被调用多次吗