【问题标题】:Fragment not getting inflated with RecyclerView片段没有被 RecyclerView 膨胀
【发布时间】:2021-08-30 17:22:20
【问题描述】:

我创建了一个RecyclerView,它应该显示在我的片段中。

recyclerView 填充了我用房间创建的数据库中的数据,我还提供了一些虚拟数据,因此它从一开始就不是空的。

但是当我打开应用程序时,片段只是空的。我还尝试添加一个 textview 只是为了查看我的片段是否膨胀但回收器是空的,但即使是 TextView 也没有显示。

这是片段:

@AndroidEntryPoint
 class HistoryFragment : Fragment(R.layout.fragment_history) {

private val viewModel: PurchaseViewmodel by viewModels()

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

    val binding = FragmentHistoryBinding.bind(view)

    val exampleAdapter = ExampleAdapter()

    binding.apply{
        recyclerView.apply{

            layoutManager = LinearLayoutManager(requireContext())
            adapter = exampleAdapter
            setHasFixedSize(true)
        }
    }
    viewModel.receipts.observe(viewLifecycleOwner){ 
        exampleAdapter.submitList(it)
    }
}
}

还有适配器:

class ExampleAdapter : ListAdapter<Receipts,ExampleAdapter.ReceiptsViewHolder>(DiffCallback()) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReceiptsViewHolder {
    val binding = ReceiptsBinding.inflate(LayoutInflater.from(parent.context),parent,false)
    return ReceiptsViewHolder(binding)
}

override fun onBindViewHolder(holder: ReceiptsViewHolder, position: Int) {
    val currentItem = getItem(position)
    holder.bind(currentItem)
}

class ReceiptsViewHolder(private val binding: ReceiptsBinding) : RecyclerView.ViewHolder(binding.root){ 
    fun bind (receipts: Receipts) {
        binding.apply {
            storeHistory.text = receipts.store
            amountHistory.text = receipts.total
            dateHistory.text  = receipts.date
        }
    }
}
class DiffCallback : DiffUtil.ItemCallback<Receipts>() {
    override fun areItemsTheSame(oldItem: Receipts, newItem: Receipts) =
        oldItem.id == newItem.id


    override fun areContentsTheSame(oldItem: Receipts, newItem: Receipts) =
        oldItem == newItem
}

}

【问题讨论】:

  • 你有override getItemCount()吗?
  • 不,我没有那个实现。我该怎么做?
  • 抱歉,虽然您从 RecyclerView.Adapter 扩展而来,但 ListAdapter 不需要它
  • 啊该死的好吧。
  • 我有机会查看提供的示例,问题似乎出在布局中。请看看我的回答

标签: android kotlin android-fragments android-recyclerview listadapter


【解决方案1】:

问题很可能是因为您没有覆盖片段的onCreateView()

您应该覆盖它并从中返回绑定根:

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    binding = FragmentHistoryBinding.inflate(inflater, container, false)
    val view = binding.root
    return view
}

【讨论】:

  • 遗憾的是,即使使用 onCreateView() 也不会改变
  • 奇怪,那我怀疑你的片段没有附加,你能分享一下 GitHub 上的最小工作示例吗?
  • github.com/UniDueJonasQuurck/NLP_Expense_tracker 这是项目的链接,对不起,我没有可用的最小工作项目。
【解决方案2】:

问题出在History片段布局中,需要将RecyclerView的高度从wrap_content改为match_parent

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.History.HistoryFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="4dp"
        android:clipToPadding="false"
        android:padding="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    相关资源
    最近更新 更多