Не отображается recyclerview в fragment
Программа запускается без каких либо серьёзных проблем, но вылезает ошибка "E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No adapter attached; skipping layout" Скорее всего из-за чего и на экране ничего не отображается. Помогите пожалуйста уже 3 день мучаюсь с этой проблемой. Что мне нужно сделать?
Нижу прикрепляю свой код:
Класс CatalogFragment:
class CatalogFragment : Fragment(R.layout.fragment_catalog) {
private var categoryRc: RecyclerView? = categoryRecycler
private var categoryAdapter: CategoryAdapter? = null
private var productRc: RecyclerView? = productRecycler
private var nomenclatureAdapter: NomenclatureAdapter? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val categoryList: MutableList<Category> = ArrayList()
categoryList.add(Category(1, "Твёрдый сорт"))
categoryList.add(Category(2, "Полутвёрдый сорт"))
categoryList.add(Category(3, "Молодой сорт"))
setCategoryRecycler(categoryList)
val productList: MutableList<Nomenclature> = ArrayList()
productList.add(Nomenclature(1, "Сыр Губернаторский", "100гр", 140))
setProductRecycler(productList)
}
private fun setProductRecycler(productList: MutableList<Nomenclature>) {
val productManager: RecyclerView.LayoutManager =
GridLayoutManager(context, 3)
productRc?.layoutManager = productManager
nomenclatureAdapter = NomenclatureAdapter(productList)
productRc?.adapter = nomenclatureAdapter
productRc?.adapter?.notifyDataSetChanged()
}
private fun setCategoryRecycler(categoryList: MutableList<Category>) {
val categoryManager: RecyclerView.LayoutManager =
LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
categoryRc?.layoutManager = categoryManager
categoryAdapter = CategoryAdapter(categoryList)
categoryRc?.adapter = categoryAdapter
categoryRc?.adapter?.notifyDataSetChanged()
}
}
Мой класс NomenclatureAdapter:
class NomenclatureAdapter(private var product_list: MutableList<Nomenclature>) : RecyclerView.Adapter<NomenclatureAdapter.NomenclatureViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NomenclatureViewHolder {
val productItems = LayoutInflater.from(parent.context).inflate(R.layout.product_item, parent, false)
return NomenclatureViewHolder(productItems)
}
override fun onBindViewHolder(holder: NomenclatureViewHolder, position: Int) {
holder.productTitle.text = product_list[position].title
holder.productUnit.text = product_list[position].unit
holder.productPrice.text = product_list[position].price_gr.toString()
}
override fun getItemCount(): Int {
return product_list.size
}
class NomenclatureViewHolder (itemView: View) : RecyclerView.ViewHolder(itemView) {
var productTitle: TextView = itemView.findViewById(R.id.product_title)
var productUnit: TextView = itemView.findViewById(R.id.product_unit)
var productPrice: Button = itemView.findViewById(R.id.btn_add_product)
}
}
Класс CategoryAdapter:
class CategoryAdapter(private var categories: MutableList<Category>) : RecyclerView.Adapter<CategoryViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
val categoryItems = LayoutInflater.from(parent.context).inflate(R.layout.category_item, parent, false)
return CategoryViewHolder(categoryItems)
}
override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
holder.categoryTitle.text = categories[position].title
}
override fun getItemCount(): Int {
return categories.size
}
class CategoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var categoryTitle: TextView = itemView.findViewById(R.id.categoryTitle)
}
}
Разметка CatalogFragment:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".CatalogFragment"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="50sp"
android:layout_height="50sp"
android:src="@drawable/ic______2__1_"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/categoryRecycler"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/page_profile_background"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="@+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/imageView2"
tools:itemCount="2"
tools:listitem="@layout/category_item" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/productRecycler"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/text_hint_color"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/categoryRecycler"
tools:itemCount="2"
tools:listitem="@layout/product_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
Разметка CategoryItem:
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/categoryTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/all"
android:textColor="@color/black"
android:layout_marginHorizontal="5dp"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Разметка productItem:
<?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="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="10sp"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#F3D08C">
<ImageView
android:id="@+id/product_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="12dp"
app:srcCompat="@drawable/cheese_gubernatorsky" />
<TextView
android:id="@+id/product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:textSize="14sp"
android:textColor="@color/black"
android:text="TextView" />
<TextView
android:id="@+id/product_unit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/black"
android:text="TextView"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"/>
<Button
android:id="@+id/btn_add_product"
android:layout_width="140dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/background_button"
android:text="@string/add_product"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:visibility="gone"
android:layout_marginHorizontal="3dp">
<Button
android:id="@+id/btn_minus_product"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/background_button"
android:text="-"
android:textSize="16sp"/>
<TextView
android:id="@+id/pr_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginHorizontal="30dp"
android:text="1"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="14sp" />
<Button
android:id="@+id/btn_plus_product"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/background_button"
android:text="+"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>