Skip to content

Instantly share code, notes, and snippets.

View gastsail's full-sized avatar
🎯
Focusing

Gastón Saillén gastsail

🎯
Focusing
View GitHub Profile
abstract class BaseConcatHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun bind(adapter: T)
}
val animalAdapter = AnimalAdapter(this)
animalAdapter.setAnimals(mutableListOf(Animal("Dog","DogImgUrl"),Animal("Cat","CatImgUrl"),Animal("Hamster","HamsterImgUrl"),Animal("Shark","SharkImgUrl"))
val breedAdapter = DogBreedAdapter(this)
breedAdapter.setBreeds(mutableListOf(Breed("Bulldog"),Breed("Beagle"),Breed("Bulldog"),Breed("Golden retriever"))
//Lets create now the ConcatAdapter with these adapters ready
val concatAdapter = ConcatAdapter(GridConcatAdapter(this,animalAdapter,4),breedAdapter)
animal_rv.layoutManager = LinearLayoutManager(requireContext())
animal_rv.adapter = concatAdapter
class BaseGridConcatAdapter(private val context: Context, private val animalAdapter: AnimalAdapter,private val spanCount:Int) :
RecyclerView.Adapter<BaseConcatHolder<*>>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseConcatHolder<*> {
val view = LayoutInflater.from(context).inflate(R.layout.animal_concat_row,parent,false)
view.rv_animal_concat.layoutManager = GridLayoutManager(context, spanCount)
return ConcatViewHolder(view)
}
override fun getItemCount(): Int = 1
val animalAdapter = AnimalAdapter(this)
animalAdapter.setAnimals(mutableListOf(Animal("Dog","DogImgUrl"),Animal("Cat","CatImgUrl"),Animal("Hamster","HamsterImgUrl"),Animal("Shark","SharkImgUrl"))
val breedAdapter = DogBreedAdapter(this)
breedAdapter.setBreeds(mutableListOf(Breed("Bulldog"),Breed("Beagle"),Breed("Bulldog"),Breed("Golden retriever"))
//Lets create now the ConcatAdapter with these adapters ready
val concatAdapter = ConcatAdapter(animalAdapter,breedAdapter)
animal_rv.layoutManager = LinearLayoutManager(requireContext())
animal_rv.adapter = concatAdapter
class DogBreedAdapter(
val context: Context,
) : RecyclerView.Adapter<BaseViewHolder<*>>() {
private var dogBreedList = mutableListOf<Breed>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
return DogBreedViewHolder(
LayoutInflater.from(context).inflate(R.layout.breeds_row, parent, false)
)
abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun bind(item: T,position:Int)
}
class AnimalAdapter(private val context: Context) :
RecyclerView.Adapter<BaseViewHolder<*>>() {
private var animalList = mutableListOf<Animal>()
companion object {
const val ITEMS_PER_PAGE = 4
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
sharedViewModel.getCart.observe(viewLifecycleOwner, Observer { cart ->
navigateToCart(cart)
})
val getCart: LiveData<MutableList<Cart>> = Transformations.switchMap(cartHashMap) {
liveData(context = viewModelScope.coroutineContext + Dispatchers.IO) {
emit(it.values.toMutableList())
}
}
//ViewModel
fun getHashMapData():LiveData<HashMap<String,Cart>>{
return cartHashMap
}
//UI that gets this hashmap
sharedViewModel.getHashMapData().observe(viewLifecycleOwner, Observer { dataHashMap ->
//Here I need to transform this HashMap into a MutableList<Cart>