【问题标题】:Recycler View not showing results in fragment回收站视图未在片段中显示结果
【发布时间】:2022-01-21 14:46:14
【问题描述】:

我正在尝试在我的一些片段中实现回收器视图,并且我尝试在第一个片段中这样做。 IDE 中在编译时未显示任何问题,但在运行时我在控制台上收到此消息:E/RecyclerView: No layout manager attached; skipping layout。此外,我的应用程序中没有显示数据。

这是我的片段:


    var sandwiches = listOf<Sandwich>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = DataBindingUtil.inflate<FragmentSandwichesBinding>(
            inflater,
            R.layout.fragment_sandwiches, container, false
        )

        val application = requireNotNull(this.activity).application
        val dataSource = NbaCafeDB.getInstance(application).sandwichDao
        val viewModelFactory = SandwichViewModelFactory(dataSource, application)

        val sandwichViewModel =
            ViewModelProvider(this, viewModelFactory).get(SandwichViewModel::class.java)

        sandwiches = sandwichViewModel.getAll()

        val adapter = SandwichAdapter(sandwiches)
        binding.sandwichRecycler.adapter = adapter

        binding.setLifecycleOwner(this)

        return binding.root
    }

}

这是我的适配器:

class SandwichAdapter (val sandwich: List<Sandwich>) : RecyclerView.Adapter<SandwichAdapter.SandwichHolder>() {

    override fun getItemCount() = sandwich.size

    class SandwichHolder(val view: View) : RecyclerView.ViewHolder(view) {
        fun bind(sandwich: Sandwich) {
            view.findViewById<TextView>(R.id.sandwichNom).text = sandwich.nomSandwich
            view.findViewById<TextView>(R.id.sandwichDesc).text = sandwich.descSandwich
            view.findViewById<TextView>(R.id.sandwichPreu).text = (sandwich.preuSandwich.toString()+" €")
        }

        companion object {
            fun from(parent: ViewGroup): SandwichHolder {
                val layoutInflater = LayoutInflater.from(parent.context)
                val view = layoutInflater
                    .inflate(R.layout.sandwich_cell_layout, parent, false)

                return SandwichHolder(view)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SandwichHolder {
        return SandwichHolder.from(parent)
    }

    override fun onBindViewHolder(holder: SandwichHolder, position: Int) {
        holder.bind(sandwich[position])
    }

}

另外,我正在从房间数据库中检索数据并使用 viewModel 和 viewModelFactory,以防万一发生任何变化。 谢谢!

【问题讨论】:

  • 您需要将LayoutManager 设置为 RV,如下所示:binding.sandwichRecycler.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
  • @AmrJyniat 谢谢,成功了!!

标签: android android-fragments android-recyclerview


【解决方案1】:

您无需致电RecyclerView.setLayoutManager(layoutManager)

只需在您的 xml 中添加 app:layoutManagerRecyclerView

LinearLayoutManager的默认方向是VERTICAL,但是如果你想改变方向为HORIZONTAL,你可以添加android:orientation=-"horizontal"

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical|horizontal"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2015-02-26
    • 2020-07-06
    相关资源
    最近更新 更多