onesector
onesector

Reputation: 444

How to add Items to RecyclerView using a ViewModel Android Kotlin

I have a fragment in which a RecyclerView is initialized. In the same fragment, I add elements to this RecyclerView. Code below:

private lateinit var profileMenuList: MutableList<ProfileMenu>

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentProfileBinding.inflate(inflater, container, false)

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        setupRecyclerView()
    }

    private fun setupRecyclerView() {
        profileMenuList = mutableListOf(
            ProfileMenu(
                title = "Favorite",
                description = "What you liked",
                icon = R.drawable.ic_outline_favorite_light
            )
        )
        
        val adapter = ProfileAdapter(profileMenuList)
        binding.rvProfileMenu.layoutManager = LinearLayoutManager(requireContext())
        binding.rvProfileMenu.adapter = adapter
    }

Here is also the code for the Adapter:

class ProfileAdapter(private val profileMenuList: List<ProfileMenu>): RecyclerView.Adapter<ProfileAdapter.ViewHolder>() {

    class ViewHolder(private val binding: ItemProfileMenuBinding): RecyclerView.ViewHolder(binding.root) {
        fun bind(menu: ProfileMenu) = with(binding) {
            tvTitle.text = menu.title
            tvDescription.text = menu.description
            Glide.with(itemView.context).load(menu.icon).into(ivIcon)
        }

        companion object {
            fun from(parent: ViewGroup): ViewHolder {
                val layoutInflater = LayoutInflater.from(parent.context)
                val binding = ItemProfileMenuBinding.inflate(layoutInflater, parent, false)
                return ViewHolder(binding)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder.from(parent)
    }
}

But I want to put the code from the setupRecyclerView() function, which is in the fragment, into the view model. How can i do this?

Upvotes: 1

Views: 3065

Answers (2)

Ali Salehi
Ali Salehi

Reputation: 194

you shouldn't update UI or use any context in view model , you have to pass your list through liveData objects in viewModel and Observe it in your fragment or activity , something like this :

class MyViewModel: ViewModel() {

val profileMenuList = MutableLiveData<List<ProfileMenu>>()

init {
    
   
        profileMenuList .value = "YourDataRepository"
    }
}

}

then observe the liveData in fragment or activity , read Android Documentation to do it

Upvotes: 0

harun karaca
harun karaca

Reputation: 134

It is not a good practice what you want. ViewModel does not interact with UI elements.

Proper usage ; You can create live-data objects in ViewModel. Then observe the objects from your UI. Not passing recylerview to ViewModel.

Upvotes: 1

Related Questions