Error al transferir en RecyclerView de un activty a un fragmento
Al crear un Adapter para el RecylcerView, me da el siguiente error: Type mismatch: inferred type is HomeFragment but Context! was expected. En principio quería el RecyclerView en un Activity pero finalmente lo pasé a un fragmento, y en este proceso me da el siguiente error.
class MainAdapter(private val context: HomeFragment):RecyclerView.Adapter<MainAdapter.MainViewHolder>() {
private var dataList = mutableListOf<Hobbie>()
fun setListData(data:MutableList<Hobbie>){
dataList = data
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_row,parent,false)
return MainViewHolder(view)
}
override fun getItemCount(): Int {
return if (dataList.size > 0) {
dataList.size
} else {
0
}
}
override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
val user:Hobbie = dataList[position]
holder.bindView(user)
}
inner class MainViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
fun bindView(user:Hobbie){
itemView.txt_title.text = user.nombre
}
}
}
class HomeFragment : Fragment() {
private lateinit var adapter: MainAdapter
private val ViewModels : MainViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
adapter = MainAdapter(this)
rvHobbies.layoutManager = LinearLayoutManager(requireContext())
rvHobbies.adapter = adapter
observeData()
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
fun observeData(){
ViewModels.fetchHobbieData().observe(this, Observer {
adapter.setListData(it)
adapter.notifyDataSetChanged()
})
}
}
Creo que el error es en el requiereContext pero no estoy seguro.