简体   繁体   English

RecycleView - 没有附加适配器,在 Fragment 中跳过布局

[英]RecycleView - No adapter attached, skipping layout in Fragment

Sup guys,伙计们,

I am currently implementing the ListAdapter for my RecycleView.我目前正在为我的 RecycleView 实现 ListAdapter。 While implementing I followed this tutorial: https://medium.com/simform-engineering/listadapter-a-recyclerview-adapter-extension-5359d13bd879在实施时,我遵循了本教程: https://medium.com/simform-engineering/listadapter-a-recyclerview-adapter-extension-5359d13bd879

There is a problem with which I am currently struggeling with: Code:我目前正在努力解决一个问题:代码:

adapter = UsersAdapter()
rvUsers.adapter = adapter

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

I dont know if I implemented this correctly, because I get the error that the adapter isnt attached.我不知道我是否正确实现了这一点,因为我收到适配器未连接的错误。

Here`s my Code:这是我的代码:


      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
   }

}

My Fragment:我的片段:

    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?

Well you should notify the adapter about the changes in your list.好吧,您应该将列表中的更改通知适配器。 That way they will be shown on the display.这样它们就会显示在显示屏上。 You could try with: invoiceAdapter.notifydatasetChanged()您可以尝试: invoiceAdapter.notifydatasetChanged()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “没有附加适配器,跳过布局:RecycleView - "No adapter attached, skipping layout : RecycleView Android RecycleView未附加适配器,跳过布局 - Android RecycleView No Adapter Attached, Skipping Layout 未连接适配器; 从RecycleView跳过布局 - No adapter attached; skipping layout from RecycleView Recycleview RecyclerView:未连接适配器; 跳过布局 - Recycleview RecyclerView: No adapter attached; skipping layout “未连接适配器; 在片段上跳过布局” - “No adapter attached; skipping layout” on a fragment 没有连接适配器,跳过片段上的布局 - No Adapter Attached, Skipping Layout on Fragment Android在recycleview片段选项卡布局消息中填充Firebase数据:E / RecyclerView:未连接适配器; 跳过布局 - Android populate firebase data in recycleview fragment tablayout message: E/RecyclerView: No adapter attached; skipping layout 没有连接适配器; 在 Fragment、RecyclerView 中跳过布局 - No adapter attached; skipping layout in Fragment, RecyclerView 没有连接适配器; 跳过布局 - 片段/科特林错误 - No adapter attached; skipping layout - error in fragment/kotlin 片段中的RecyclerView - 没有附加适配器,跳过布局 - RecyclerView in Fragment - No Adapter Attached, Skipping Layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM