I try to make a simple recycler view on Kotlin with Android Studio. But it doesn't display item. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
I have a MainActivity.kt :
package fr.monarch.viper
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import fr.monarch.viper.fragments.CommandesFragment
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//injecter le fragment commandes fragment
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, CommandesFragment())
transaction.addToBackStack(null)
transaction.commit()
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
A activity_main.xml:
<?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=".MainActivity">
<TextView
android:id="@+id/textView"
style="@style/DefaultTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/default_margin"
android:layout_marginTop="@dimen/default_margin"
android:text="@string/app_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/commandes_effectuees"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginStart="@dimen/default_margin"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/commandes_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/>
</LinearLayout>
Fragment class.kt
package fr.monarch.viper.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import fr.monarch.viper.R
import fr.monarch.viper.adapter.ItemAdapter
class CommandesFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_commandes, container,false)
val verticalRecycler = view.findViewById<RecyclerView>(R.id.commandes_recycler)
verticalRecycler.adapter = ItemAdapter(R.layout.item_commande)
return view
}
}
ItemAdapter class .kt
package fr.monarch.viper.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import fr.monarch.viper.R
class ItemAdapter(private val layoutId : Int) : RecyclerView.Adapter<ItemAdapter.ViewHolder>(){
//Pour ranger tous les items
class ViewHolder(view: View): RecyclerView.ViewHolder(view){
//image item
var article_image = view.findViewById<ImageView>(R.id.article_image)
var article_name = view.findViewById<TextView>(R.id.article_name)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater
.from(parent.context)
.inflate(layoutId, parent,false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
}
override fun getItemCount(): Int = 5
}
and my item layout
<?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="wrap_content">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="80dp"
android:layout_height="80dp"
app:cardCornerRadius="10dp"
app:cardElevation="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/article_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/item_photo"
/>
</androidx.cardview.widget.CardView>
<View
android:id="@+id/view_separation"
android:layout_width="match_parent"
android:layout_height="1dp"
android:visibility="invisible"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/article_name"
style="@style/DefaultTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/commande_item_name"
app:layout_constraintBottom_toTopOf="@+id/view_separation"
app:layout_constraintStart_toEndOf="@+id/cardView"
android:layout_marginStart="10dp"/>
<TextView
android:id="@+id/article_price"
style="@style/SousTitreStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/commande_item_price"
app:layout_constraintStart_toStartOf="@+id/article_name"
app:layout_constraintTop_toBottomOf="@+id/view_separation" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem is that it display no item in the recycler view and I get no error.