Skip to content
Permalink
Browse files

Rewrite ViewPagerAdapter

  • Loading branch information
zsmb13 committed Jan 7, 2020
1 parent e9625b1 commit 2cf3ba92c5521a6f57f22c46155109f678807e72
@@ -11,10 +11,6 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.ui.dashboard.about.AboutFragment
import dev.marcosfarias.pokedex.ui.dashboard.evolution.EvolutionFragment
import dev.marcosfarias.pokedex.ui.dashboard.moves.MovesFragment
import dev.marcosfarias.pokedex.ui.dashboard.stats.StatsFragment
import dev.marcosfarias.pokedex.utils.PokemonColorUtil
import kotlinx.android.synthetic.main.fragment_dashboard.*

@@ -80,22 +76,7 @@ class DashboardFragment : Fragment() {

val pager = viewPager
val tabs = tabs


val adapter = ViewPagerAdapter(fragmentManager!!)
adapter.addFragment(
AboutFragment.newInstance(pokemon?.id),
getString(R.string.dashboard_tab_1)
)
adapter.addFragment(
StatsFragment.newInstance(pokemon?.id),
getString(R.string.dashboard_tab_2)
)
adapter.addFragment(EvolutionFragment(), getString(R.string.dashboard_tab_3))
adapter.addFragment(MovesFragment(), getString(R.string.dashboard_tab_4))

pager.adapter = adapter

pager.adapter = ViewPagerAdapter(fragmentManager!!, context!!, pokemon?.id!!)
tabs.setupWithViewPager(pager)
}

@@ -1,29 +1,52 @@
package dev.marcosfarias.pokedex.ui.dashboard

import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.ui.dashboard.about.AboutFragment
import dev.marcosfarias.pokedex.ui.dashboard.evolution.EvolutionFragment
import dev.marcosfarias.pokedex.ui.dashboard.moves.MovesFragment
import dev.marcosfarias.pokedex.ui.dashboard.stats.StatsFragment

class ViewPagerAdapter(supportFragmentManager: FragmentManager) :
FragmentStatePagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
class ViewPagerAdapter(
supportFragmentManager: FragmentManager,
context: Context,
private val pokemonId: String
) : FragmentStatePagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
data class Page(val title: String, val ctor: () -> Fragment)

@Suppress("MoveLambdaOutsideParentheses")
private val pages = listOf(
Page(
context.getString(R.string.dashboard_tab_1),
{ AboutFragment.newInstance(pokemonId) }
),
Page(
context.getString(R.string.dashboard_tab_2),
{ StatsFragment.newInstance(pokemonId) }
),
Page(
context.getString(R.string.dashboard_tab_3),
{ EvolutionFragment() }
),
Page(
context.getString(R.string.dashboard_tab_4),
{ MovesFragment() }
)
)

override fun getItem(position: Int): Fragment {
return mFragmentList.get(position)
return pages[position].ctor()
}

override fun getCount(): Int {
return mFragmentList.size
return pages.size
}

override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList[position]
}

fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
return pages[position].title
}
}
}

0 comments on commit 2cf3ba9

Please sign in to comment.
You can’t perform that action at this time.