首页 > 解决方案 > E/RecyclerView:没有附加适配器;跳过布局 Kotlin Android

问题描述

我创建了一个片段,在该片段中我有一个 recyclerview,但是当我的片段加载时没有显示,它给了我这个错误“E/RecyclerView:没有附加适配器;跳过布局”。下面是适配器和片段类的代码,任何帮助将不胜感激

适配器类:

class ViewAllRecipeAdapter(private val newList: ArrayList<Recipes>) :
    RecyclerView.Adapter<ViewAllRecipeAdapter.MyViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.view_all_recipe_item, parent, false)

        return MyViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val currentItem = newList[position]
        holder.recipeName.text = currentItem.recipeName
        holder.recipeDesc.text = currentItem.recipeDescription
    }

    override fun getItemCount(): Int {
        return newList.size
    }

    class MyViewHolder(itemview: View) : RecyclerView.ViewHolder(itemview) {

        val recipeName: TextView
        val recipeDesc: TextView

        init {
            recipeName = itemView.findViewById<View>(R.id.recipe_name) as TextView
            recipeDesc = itemView.findViewById<View>(R.id.recipe_description) as TextView
        }
    }
}

片段类:

class ViewAllMyRecipesFragment : Fragment() {


    private lateinit var recyclerview: RecyclerView
    private lateinit var recipeData: ArrayList<Recipes>

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View = inflater.inflate(R.layout.fragment_view_all_my_recipes, container, false)

        recipeData = dummygenerator(10)

        recyclerview = view.findViewById<RecyclerView>(R.id.recycler_view_all_recipes)
        recyclerview.adapter = ViewAllRecipeAdapter(recipeData)
        recyclerview.layoutManager = LinearLayoutManager(view.context)
        recyclerview.setHasFixedSize(true)

        // Inflate the layout for this fragment
        return view
    }

    private fun dummygenerator(size: Int) : ArrayList<Recipes>{
        val list = ArrayList<Recipes>()

        for(i in 0 until size) {
            val drawable = when (i % 3) {
                0 -> "recipeName " +i
                1 -> "recipeDescription " + i
                else -> "Else " +i
            }

            val item = Recipes("title $i", "body")
            list += item
        }

        return list
    }

}

fragment_view_all_my_recipes.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.recipe.ViewAllMyRecipesFragment">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_all_recipes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/view_all_recipe_item" />

</androidx.constraintlayout.widget.ConstraintLayout>

view_all_recipe_item.xml

<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="wrap_content">

    <ImageView
        android:id="@+id/recipe_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginEnd="331dp"
        android:layout_marginBottom="651dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/recipe_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:text="TextView"
        android:textStyle="bold"
        android:textSize="18sp"
        app:layout_constraintStart_toEndOf="@+id/recipe_image"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/recipe_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="10dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/recipe_image"
        app:layout_constraintTop_toBottomOf="@+id/recipe_name" />


</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidkotlinandroid-recyclerview

解决方案


Try moving all onCreateView logic to onViewCreated and

recyclerview.layoutManager = LinearLayoutManager(requireActivity())
recyclerview.adapter = ViewAllRecipeAdapter(recipeData)

because sometimes it causes problem when layoutManager is after adapter.


推荐阅读