【问题标题】:RecycleView - No adapter attached, skipping layout in FragmentRecycleView - 没有附加适配器,在 Fragment 中跳过布局
【发布时间】:2020-06-30 10:58:28
【问题描述】:

伙计们,

我目前正在为我的 RecycleView 实现 ListAdapter。在实施时,我遵循了本教程:https://medium.com/simform-engineering/listadapter-a-recyclerview-adapter-extension-5359d13bd879

我目前正在努力解决一个问题: 代码:

adapter = UsersAdapter()
rvUsers.adapter = adapter

userListLiveData.observe(this, Observer {list->
    adapter.submitList(list)
})

我不知道我是否正确实现了这个,因为我得到了适配器未连接的错误。

这是我的代码:


      class InvoiceListAdapter : ListAdapter<InvoiceModel, InvoiceViewHolder>(InvoiceListDiffCallback()) {
       override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InvoiceViewHolder {
           return InvoiceViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.fragment_first, parent, false))
       }
   
       override fun onBindViewHolder(holder: InvoiceViewHolder, position: Int) {
           holder.bindTo(position)
       }
   
       override fun getItemCount(): Int {
           val count = super.getItemCount()
           return when(count) {
               0 -> 1
               else -> count
           }
       }
   
   }

class InvoiceListDiffCallback : DiffUtil.ItemCallback<InvoiceModel>() {
   override fun areItemsTheSame(oldItem: InvoiceModel, newItem: InvoiceModel): Boolean {
       return oldItem == newItem
   }

   override fun areContentsTheSame(oldItem: InvoiceModel, newItem: InvoiceModel): Boolean {
       return oldItem == newItem
   }

}

我的片段:

    class FirstFragment : Fragment() {
    private lateinit var invoiceAdapter: InvoiceListAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val items = mutableListOf<InvoiceModel>()
        val layoutManager = LinearLayoutManager(context)

        invoiceAdapter = InvoiceListAdapter()

        invoiceList.layoutManager = layoutManager
        invoiceList.adapter = invoiceAdapter


        for (i in 0..40) {
            print(i)
        }

        items.add(
            InvoiceModel(
                "Einkauf",
                "Digital",
                "27.05.2020 12:00 Uhr",
                "Wasser  gekauft",
                null,
                "Portmonaiee",
                true
            )
        )
        invoiceAdapter.submitList(items)
    }
}




Has anyone an idea how to fix that the adapter isnt attached?

【问题讨论】:

  • 您的应用程序崩溃了吗?
  • 不,它只显示一个白色的应用程序,就像什么都没有。

标签: android android-studio kotlin android-recyclerview


【解决方案1】:

您应该通知适配器您列表中的更改。这样它们就会显示在显示屏上。您可以尝试: invoiceAdapter.notifydatasetChanged()

或者这个: invoiceAdapter.notifyItemInserted(items.size - 1)

【讨论】:

  • 我应该在哪里添加 notifyDatasetChanged?在片段或适配器中?充气机是否正确?
  • 提交列表后
  • 它不起作用。带有 notifyDatasetChanged() 或 notifyItemInserted(items.size - 1) 的以太币 :( 还有其他想法吗?充气机正确吗?
  • 哦,我忘了说我有一个 ViewHolder,它和我的 InvoiceModel 在同一个文件中,并且它有一个 bindTo 函数:` class InvoiceViewHolder(itemView: View) : RecyclerView.ViewHolder( itemView) { fun bindTo(invoice: Int) { val items = mutableListOf() for (i in 0..40) { print(i) } items.add( InvoiceModel("Einkauf", "Digital", " 27.05.2020 12:00 Uhr", "Wasser gekauft", null, "Portmonaiee", true) ) } } `
  • 你可以尝试在 onCreate() 方法而不是 onViewCreated() 中附加适配器
猜你喜欢
  • 1970-01-01
  • 2019-04-07
  • 1970-01-01
  • 2021-09-19
  • 2018-06-12
  • 2021-12-19
  • 1970-01-01
  • 2019-10-01
相关资源
最近更新 更多