The cards are not showing when im running the simulator (the recyclerView is showing). I think the problem is in "onCreateView" at AdvertsFragement. My MainActivity activity is with binding inflated. The advertsfragement is embedded in the MainActivity thru a navigation bottom bar.
Card Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_margin="5dp"
app:cardBackgroundColor="@color/oceanBlue"
app:cardCornerRadius="12dp"
app:cardElevation="3dp"
app:contentPadding="4dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/forestGreen"
android:id="@+id/relativeLayout"
android:padding="16dp"
>
<ImageView
android:id="@+id/item_image"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<TextView
android:id="@+id/item_title"
android:textColor="@color/black"
android:layout_width="236dp"
android:layout_height="39dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/item_image"
android:layout_marginStart="16dp"
android:textSize="30sp"/>
<TextView
android:id="@+id/item_detail"
android:textColor="@color/black"
android:layout_width="236dp"
android:layout_height="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/item_title"
app:layout_constraintLeft_toRightOf="@+id/item_image"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Fragment
class AdvertsFragment : Fragment() {
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: RecyclerView.Adapter<RecyclerAdapter.ViewHolder>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
R.id.recycler_view.apply {
// set a LinearLayoutManager to handle Android
// RecyclerView behavior
layoutManager = LinearLayoutManager(activity)
// set the custom adapter to the RecyclerView
adapter = RecyclerAdapter()
}
return inflater.inflate(R.layout.fragment_adverts, container, false)
}
override fun onViewCreated(itemView: View, savedInstanceState: Bundle?) {
super.onViewCreated(itemView, savedInstanceState)
}
}
RecyclerAdapter and ViewHolder
class RecyclerAdapter: RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
private var titles = arrayOf("Test1", "Test2", "Test3", "Test4", "Test5")
private var details = arrayOf("Detail1", "Detail2", "Detail3", "Detail4", "Detail5")
private var images = intArrayOf(R.drawable.ic_notifications_black_24dp, R.drawable.ic_notifications_black_24dp, R.drawable.ic_notifications_black_24dp, R.drawable.ic_notifications_black_24dp, R.drawable.ic_notifications_black_24dp)
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerAdapter.ViewHolder{
val v = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.card_layout, viewGroup, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return titles.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemTitle.text = titles[position]
holder.itemDetail.text = details[position]
holder.itemImage.setImageResource(images[position])
}
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
var itemImage: ImageView
var itemTitle: TextView
var itemDetail: TextView
init {
itemImage = itemView.findViewById(R.id.item_image)
itemTitle = itemView.findViewById(R.id.item_title)
itemDetail = itemView.findViewById(R.id.item_detail)
itemView.setOnClickListener {
val position: Int = absoluteAdapterPosition
Toast.makeText(itemView.context, "you clicked on ${titles[position]}", Toast.LENGTH_LONG).show()
}
}
}
}
I just needed to use findViewById() in onViewCreated instead of "R.id.recycler_view.apply" in onCreateView:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_adverts, container, false)
}
override fun onViewCreated(itemView: View, savedInstanceState: Bundle?) {
super.onViewCreated(itemView, savedInstanceState)
itemView.findViewById<RecyclerView>(R.id.recycler_view).apply{
// set a LinearLayoutManager to handle Android
// RecyclerView behavior
layoutManager = LinearLayoutManager(activity)
// set the custom adapter to the RecyclerView
adapter = RecyclerAdapter()
}
}