I have an Adapter created and I want to populate two "different" RecyclerViews that share the same layout and class attributes. So I wanted to use the same adapter for both recyclerview since it seemed to me not very useful to create two different ones, I have done it in the following way:
This would be the code of my Adapter:
class EducationAdapter : RecyclerView.Adapter<EducationViewHolder>() {
var education: MutableList<Education> = ArrayList()
lateinit var context: Context
fun EducationAdapter(education: MutableList<Education>, context: Context){
this.education = education
this.context = context
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EducationViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return EducationViewHolder(
layoutInflater.inflate(
R.layout.education_design,
parent,
false
)
)
}
override fun getItemCount(): Int {
return education.size
}
override fun onBindViewHolder(holder: EducationViewHolder, position: Int) {
val item = education.get(position)
holder.bind(item)
}
}
class EducationViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val educationUrl = view.findViewById(R.id.ivEducationImage) as ImageView
val educationTitle = view.findViewById(R.id.tvEducationTitle) as TextView
val educationPlace = view.findViewById(R.id.tvEducationPlace) as TextView
val educationDesc = view.findViewById(R.id.tvEducationDescription) as TextView
fun bind(education: Education){
educationUrl.loadUrl(education.url)
educationTitle.text = education.title
educationPlace.text = education.place
educationDesc.text = education.description
}
fun ImageView.loadUrl (url: String){
Glide.with(context).load(url).transform(CenterCrop(), CircleCrop()).into(this)
}
}
This would be the code of my class where I want the elements of the RecyclerView to be displayed:
class EducationFragment : Fragment() {
lateinit var mRecyclerViewAcademic: RecyclerView
lateinit var mRecyclerViewComplementary: RecyclerView
val mAdapter: EducationAdapter = EducationAdapter()
companion object {
fun newInstance(): EducationFragment =
EducationFragment()
}
fun setUpRecyclerView() {
mRecyclerViewAcademic = rvEducationAcademic
mRecyclerViewAcademic.setHasFixedSize(true)
mRecyclerViewAcademic.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
mAdapter.EducationAdapter(getEducationAcademic(), requireContext())
mRecyclerViewAcademic.adapter = mAdapter
mRecyclerViewComplementary = rvEducationComplementary
mRecyclerViewComplementary.setHasFixedSize(true)
mRecyclerViewComplementary.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
mAdapter.EducationAdapter(getEducationComplementary(), requireContext())
mRecyclerViewComplementary.adapter = mAdapter
}
fun getEducationAcademic(): MutableList<Education> {
var educationAcademic: MutableList<Education> = ArrayList()
educationAcademic.add(Education("https://einatec.com/wp-content/uploads/2019/03/errores-disenadores-graficos.jpg", "Prueba", "Prueba", "Prueba"))
educationAcademic.add(Education("https://einatec.com/wp-content/uploads/2019/03/errores-disenadores-graficos.jpg", "Prueba", "Prueba", "Prueba"))
return educationAcademic
}
fun getEducationComplementary(): MutableList<Education> {
var educationComplementary: MutableList<Education> = ArrayList()
educationComplementary.add(Education("https://cdn57.androidauthority.net/wp-content/uploads/2017/05/Android-Studio-Coding2-840x473.jpg", "Prueba", "Prueba", "Prueba"))
return educationComplementary
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_education, container, false)
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setUpRecyclerView()
}
}
In my head it looked good, but of course, I guess the problem is that I am using the same list for both recyclerview, but I have no idea how to specify a different list inside the adapter, it helps a beginner, thanks.
You have an adapter so you must generate 2 different instances of it and assign them to each of the recyclers: