如何使用 Groupie RecyclerView 库和 Kotlin 在项目被删除时通知和更新列表

Fra*_*pos 2 java android android-adapter kotlin android-recyclerview

我有一个使用 Groupie 库实现的 RecyclerView,我可以从列表中删除一个项目,但需要更新视图才能看到更改。我想要使​​用像notifyDataSetChanged() 这样的东西,这样列表就会立即更新。不过,我在这个阶段有点困惑,尝试了几种不同的方法来从托管我的视图持有者的类中获取接口,以便从持有适配器的片段触发,但我想如果我能得到的话,我现在就陷入困境了请帮忙。

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)
}

}
Run Code Online (Sandbox Code Playgroud)

这是我的片段。我尝试向适配器添加一个部分,看看它是否允许我检索它的侦听器,但由于我的侦听器应该在布局中的特定项目下触发,这可能不是最好的解决方案,尽管不能要么让这个工作。

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)
}

}
Run Code Online (Sandbox Code Playgroud)

非常感谢。

Car*_*mer 5

我认为你在追星族自述文件中错过了这个概念:

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

因此,要删除某个项目,请调用section.remove(item). 但是,在您的onClickItem函数中,您当前仅传递了该位置。像这样传递项目clickListener?.onClickItem(this@RecyclerProductItem)。更理想和安全的是,您应该删除 by product.id,例如clickListener?.onClickItem(this@RecyclerProductItem.product.id),然后onClickItem()您只需搜索具有该产品 ID 的项目并将其删除。如果我不清楚,请告诉我。