1

Estoy intentando abrir fragments o activities desde un navigation drawer segun su posicion pero me falta un parametro que no me aclaro muy bien. Tengo un arraylist.

class MainActivityInteractor

private fun createNavItemList(): ArrayList<NavItemModel> {

        val navItems:ArrayList<NavItemModel>? = ArrayList()
        navItems?.add(NavItemModel(R.drawable.ic_campo, "Campos"))
        navItems?.add(NavItemModel(R.drawable.ic_work_black_24dp, "Division"))
        return navItems!!
    }

esta clase

sealed class MainActivityState {
    class ShowNavItems(val navItems: ArrayList<NavItemModel>) : MainActivityState()
    class HandleNavItemClick(val navItem: NavItemModel) : MainActivityState()
}

en la linea

is ScreenState.Render -> setDrawerItems(screenState.renderState)

me falta el parametro positionque no se que poner

en el MainActivity

private fun updateDrawerItems(screenState: ScreenState<MainActivityState>?) {
        when (screenState) {
            is ScreenState.Render -> setDrawerItems(screenState.renderState)
        }
    }

    private fun setDrawerItems(renderState: MainActivityState, position: Int) {
        when (renderState) {
            is MainActivityState.ShowNavItems -> {
                navRecycler.apply {
                    layoutManager = GridLayoutManager(this@MainActivity, 2)
                    adapter = NavAdapter(renderState.navItems, viewModel::onNavItemClicked)
                }
            }

            is MainActivityState.HandleNavItemClick -> {

                when (position) {
                    0 -> fragmentTransaction(DivFragment())
                    1 -> fragmentTransaction(BuscarFragment())
                }    
                toast("Item Clicked")
                drawerLayout.closeDrawer(GravityCompat.START)
            }
        }
    }

y el viewModel:

class MainActivityViewModel(private val mainActivityInteractor: MainActivityInteractor) : ViewModel() {

    private lateinit var _navItemsState: MutableLiveData<ScreenState<MainActivityState>>
    val navItemsState: LiveData<ScreenState<MainActivityState>>
        get() {
            if (!::_navItemsState.isInitialized) {
                _navItemsState = MutableLiveData()
                mainActivityInteractor.getNavItems(::onNavItemsLoaded)
            }
            return _navItemsState
        }

    private fun onNavItemsLoaded(navItems: ArrayList<NavItemModel>) {
        _navItemsState.value = ScreenState.Render(MainActivityState.ShowNavItems(navItems))
    }

    fun onNavItemClicked(navItem: NavItemModel) {
        _navItemsState.value = ScreenState.Render(MainActivityState.HandleNavItemClick(navItem))
    }

    class MainActivityViewModelFactory(private val mainActivityInteractor: MainActivityInteractor) :
        ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            return MainActivityViewModel(mainActivityInteractor) as T
        }
    }

}

1 respuesta 1

1

La soloucion es muy simple, pero no me aclaraba if-else

is MainActivityState.HandleNavItemClick -> {
                    if (renderState.navItem.name == "Campos"){
                        fragmentTransaction(BuscarFragment())
                    }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.