What is a better way to switch between Grid and Linear of RecyclerView with MVVM? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of What is a better way to switch between Grid and Linear of RecyclerView with MVVM? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a RecyclerView and a FAB in a Fragment. The FAB switches the layout of the RecyclerView between GRID and LINEAR.

I have a enum class in the ViewModel.

enum class LAYOUT { GRID, LINEAR }

private val _layout = MutableLiveData<LAYOUT>()
val layout: LiveData<LAYOUT>
     get() = _layout

And the fragment observes the MutableLiveData of the layout value in ViewModel.

I now kind of get it working by having 2 ListAdapter, one for Grid and one for Linear. Below is my code of the Fragment.

        val gridAdapter = MovieGridAdapter()

        viewModel.layout.observe(viewLifecycleOwner, Observer {
            if (it == GRID) {
                binding.recyclerViewMovie.adapter = gridAdapter
                binding.recyclerViewMovie.layoutManager = GridLayoutManager(context, 3)
            } else {
                binding.recyclerViewMovie.adapter = linearAdapter
                binding.recyclerViewMovie.layoutManager = LinearLayoutManager(context)
            }
        })

        viewModel.trendingMovies.observe(viewLifecycleOwner, Observer {
            it?.let {
                gridAdapter.submitList(it)
                linearAdapter.submitList(it)
            }
        })

I feel like this is not the best way to do it, as I now have 2 adapters in the fragment, and I need to submitList for both adapters.

Please let me know if there is a better way to do this, thanks!

Answer

You don’t need to use two adapters one adapter can do the work

class ProductListAdapter(
private val listener: OnProductListener) : PagedListAdapter<Product, ProductViewHolder>( PRODUCT_COMPARATOR ) {

var layoutId: Int = R.layout.list_item_product

override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {

    val product = getItem(position)

    with(holder) {
        bindTo(product,true)
        product.let { product ->
            itemView.setOnClickListener {
                product.product_id?.let { it1 -> listener.onProductSelected(it1) }
            }
        }
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
    val view =
        LayoutInflater.from(parent.context)
            .inflate(layoutId, parent, false)

    return ProductViewHolder(view)
}

override fun getItemViewType(position: Int): Int {
    return position
}

fun setLayoutResourceId(layoutId: Int) {
    this.layoutId = layoutId
}


companion object {

    private val PRODUCT_COMPARATOR = object : DiffUtil.ItemCallback<Product>() {
        override fun areItemsTheSame(oldItem: Product, newItem: Product): Boolean =
            TextUtils.equals(
                oldItem.product_id,
                newItem.product_id
            ) 

        override fun areContentsTheSame(
            oldItem: Product,
            newItem: Product
        ): Boolean =
            oldItem == newItem

    }

 }

}

in you fragment define the adapter

private var productsAdapter: ProductListAdapter = ProductListAdapter(this)

viewModel.trendingMovies.observe(viewLifecycleOwner, Observer {
        it?.let {
            productsAdapter.submitList(it)

        }
    })

showList

  private fun showList() {
    rv_products.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
    productsAdapter.setLayoutResourceId(R.layout.list_item_list_product)
    rv_products.adapter = productsAdapter

}

showGrid

private fun showGrid() {
    rv_products.layoutManager = GridLayoutManager(activity, 3)
    productsAdapter.setLayoutResourceId(R.layout.list_item_grid_product)
    rv_products.adapter = productsAdapter
}
We are here to answer your question about What is a better way to switch between Grid and Linear of RecyclerView with MVVM? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji