使用Groupie RecyclerView库和Kotlin删除项目后如何通知和更新列表 [英] How to notify and update list when item has been deleted using Groupie RecyclerView library and Kotlin

查看:92
本文介绍了使用Groupie RecyclerView库和Kotlin删除项目后如何通知和更新列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Groupie库实现的RecyclerView,可以从列表中删除一个项目,但是需要更新视图才能看到更改.我想使用类似notifyDataSetChanged()之类的东西,因此列表会立即更新.我在这个阶段有点困惑,尝试了几种不同的方法来从承载我的视图持有者的类中获取一个接口,该接口是从保存适配器的片段中触发的,但是我认为如果可以得到的话,我现在就被困住了请一些帮助.

I have a RecyclerView implemented with the Groupie library and I can delete an item from the list fine, however need to update the view to see the change. I'd like to have something like notifyDataSetChanged() instead, so the list updates immediately. I'm a bit confused at this stage though, tried a few different ways to get an interface from the class that hosts my view holder to be triggered from the fragment that holds the adapter but I think I'm stuck now if I could get some help please.

class RecyclerProductItem(
private val activity: MainActivity,
private val product: Product, private val adapterListener: AdapterListener
) : Item<GroupieViewHolder>() {

companion object {
    var clickListener: AdapterListener? = null
}

override fun bind(viewHolder: GroupieViewHolder, position: Int) {

    viewHolder.apply {

        with(viewHolder.itemView) {

            clickListener = adapterListener

            ivTrash.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    if (clickListener != null) {
                        Toast.makeText(context, "delete method to be added here", Toast.LENGTH_SHORT).show()
                        clickListener?.onClickItem(position)
                    } 
                }
            })

        }

    }
}

override fun getLayout() = R.layout.recyclerview_item_row

interface AdapterListener {
    fun onClickItem(position: Int)
}

}

这是我的片段.我试图在适配器上添加一个部分,以查看它是否允许我检索它的侦听器,但是由于我的侦听器应在布局内的特定项下触发,因此这可能不是最佳解决方案,尽管无法也可以进行这项工作.

Here it's my fragment. I tried to add a section to the adapter to see if it would allow me to retrieve a listener for it, but as my listener should be triggered under a specific item within the layout, this may not be the best solution, although couldn't make this work either.

class ProductsListFragment : Fragment(), RecyclerProductItem.AdapterListener {

private lateinit var adapter: GroupAdapter<GroupieViewHolder>
private val section = Section()

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_products_list, container, false)
}

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

    val linearLayoutManager = LinearLayoutManager(activity)
    recyclerView.layoutManager = linearLayoutManager

    adapter = GroupAdapter()

    adapter.add(section)


    recyclerView.adapter = adapter

    loadProducts()

}


private fun loadProducts() {

    GetProductsAPI.postData(object : GetProductsAPI.ThisCallback {

        override fun onSuccess(productList: List<JsonObject>) {

            for (jo in productList) {

                val gson = GsonBuilder().setPrettyPrinting().create()
                val product: Product =
                    gson.fromJson(jo, Product::class.java)

                adapter.add(
                    RecyclerProductItem(
                        activity as MainActivity,
                        Product(
                            product.id,
                            product.title,
                            product.description,
                            product.price
                        ),adapterListenerToBePassedHere 
                    )
                ) // This part is where I should be giving the listener, but get a red line since not sure how to get it to include it here.

            }

        }

    })

}


companion object {
    fun newInstance(): ProductsListFragment {
        return ProductsListFragment()
    }
}


override fun onClickItem(position: Int) {
    
    adapter.notifyItemRemoved(position)
}

}

非常感谢.

推荐答案

我认为您在分组阅读器自述文件中缺少此概念:

I think you are missing this concept from the groupie Readme:

以任何方式修改GroupAdapter的内容都会自动发送更改通知.添加项目调用notifyItemAdded();添加组会调用notifyItemRangeAdded()等.

Modifying the contents of the GroupAdapter in any way automatically sends change notifications. Adding an item calls notifyItemAdded(); adding a group calls notifyItemRangeAdded(), etc.

因此要删除项目,请调用 section.remove(item).但是,在您的 onClickItem 函数中,当前您仅传递位置.传递类似 clickListener?.onClickItem(this @ RecyclerProductItem)之类的项目.更理想和更安全的是,您应该通过 product.id 删除,例如 clickListener?.onClickItem(this@RecyclerProductItem.product.id),然后在 onClickItem()中,您只需搜索具有该产品ID的商品并将其删除即可.让我知道是否不清楚.

So to remove an item, call section.remove(item). However, in your onClickItem function you currently only pass the position. Pass the item like clickListener?.onClickItem(this@RecyclerProductItem) instead. Even more ideally and safely you should remove by product.id, e.g. clickListener?.onClickItem(this@RecyclerProductItem.product.id) then in onClickItem() you just search for the item with that product id and remove it. Let me know if I'm not clear.

这篇关于使用Groupie RecyclerView库和Kotlin删除项目后如何通知和更新列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆