I have a TabLayout
, and within one of the tabs a Fragment
, which has a RecyclerView
, and each item is a CardView
.
I had just tested this part on a separate project and it worked perfectly, but when I went through the project that had the tabs and fragments, it just does not appear.
Below the code for Fragment
:
class Tab2FavoritesActivity : Fragment(){
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val r = view.findViewById<RecyclerView>(R.id.recycler)
val favoritos = ArrayList<Favorito>()
favoritos.add(Favorito("Samba", "Evento de samba mt doido"))
favoritos.add(Favorito("Pagode", "Pagodao da massa"))
favoritos.add(Favorito("Funk", "Funk 150bpm"))
r.adapter = ViewPagerAdapter(favoritos)
}
class Favorito(val title:String, val desc:String)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.favoritos_fragment,container, false)
}
Here the Adapter
:
class ViewPagerAdapter (val favoritos: ArrayList<Tab2FavoritesActivity.Favorito>): RecyclerView.Adapter<ViewPagerAdapter.ViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent?.context).inflate(R.layout.favorito, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return favoritos.size
}
override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
val favorito = favoritos[p1]
p0?.nomeEvento?.text = favorito.title
p0?.descEvento?.text = favorito.desc
}
class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
val nomeEvento = itemView.findViewById<TextView>(R.id.nomeEvento)
val descEvento = itemView.findViewById<TextView>(R.id.descEvento)
}