Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bottom Navigationでタブの状態を保持する

Bottom Navigationでタブの状態を保持する

shibuya.apk #17

Keisuke Kobayashi

August 04, 2017
Tweet

More Decks by Keisuke Kobayashi

Other Decks in Programming

Transcript

  1. class FixedViewPager @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null

    ): ViewPager(context, attrs) { override fun onTouchEvent(ev: MotionEvent?): Boolean { return false } override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean { return false } }
  2. class FixedViewPager @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null

    ): ViewPager(context, attrs) { override fun onTouchEvent(ev: MotionEvent?): Boolean { return false } override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean { return false } } λονΛແޮԽ
  3. class MainPagerAdapter(fm: FragmentManager): FragmentPagerAdapter(fm) { override fun getCount(): Int {

    return 3 } override fun getItem(position: Int): Fragment { return when (position) { 0 -> OneFragment.newInstance() 1 -> TwoFragment.newInstance() 2 -> ThreeFragment.newInstance() else -> throw IllegalArgumentException() } } }
  4. class MainPagerAdapter(fm: FragmentManager): FragmentPagerAdapter(fm) { override fun getCount(): Int {

    return 3 } override fun getItem(position: Int): Fragment { return when (position) { 0 -> OneFragment.newInstance() 1 -> TwoFragment.newInstance() 2 -> ThreeFragment.newInstance() else -> throw IllegalArgumentException() } } } λϒͷ਺
  5. class MainPagerAdapter(fm: FragmentManager): FragmentPagerAdapter(fm) { override fun getCount(): Int {

    return 3 } override fun getItem(position: Int): Fragment { return when (position) { 0 -> OneFragment.newInstance() 1 -> TwoFragment.newInstance() 2 -> ThreeFragment.newInstance() else -> throw IllegalArgumentException() } } } ֤λϒͷFragmentΛฦ͢
  6. // MainActivity.kt override fun onCreate(savedInstanceState: Bundle?) { // লུ pager.adapter

    = MainPagerAdapter(supportFragmentManager) bottomBar.setOnTabSelectListener { tabId -> when (tabId) { R.id.tab1 -> showTab1() R.id.tab2 -> showTab2() R.id.tab3 -> showTab3() } } }
  7. // MainActivity.kt override fun onCreate(savedInstanceState: Bundle?) { // লུ pager.adapter

    = MainPagerAdapter(supportFragmentManager) bottomBar.setOnTabSelectListener { tabId -> when (tabId) { R.id.tab1 -> showTab1() R.id.tab2 -> showTab2() R.id.tab3 -> showTab3() } } } λϒબ୒࣌ʹɺදࣔ಺༰Λ੾Γସ͑
  8. // MainActivity.kt private fun showTab1() { // Clear back stack

    (1..supportFragmentManager.backStackEntryCount).forEach { supportFragmentManager.popBackStack() } // Change page without animation pager.setCurrentItem(0, false) }
  9. class MainPagerAdapter(fm: FragmentManager): FragmentPagerAdapter(fm) { var currentFragment: Fragment? = null

    override fun getCount(): Int {...} override fun getItem(position: Int): Fragment {...} override fun setPrimaryItem(container: ViewGroup?, position: Int, obj: Any?) { if (currentFragment != obj) { currentFragment = obj as Fragment } super.setPrimaryItem(container, position, obj) } }
  10. class MainPagerAdapter(fm: FragmentManager): FragmentPagerAdapter(fm) { var currentFragment: Fragment? = null

    override fun getCount(): Int {...} override fun getItem(position: Int): Fragment {...} override fun setPrimaryItem(container: ViewGroup?, position: Int, obj: Any?) { if (currentFragment != obj) { currentFragment = obj as Fragment } super.setPrimaryItem(container, position, obj) } } ݱࡏͷFragmentΛอ࣋
  11. // MainActivity.kt
 override fun onCreate(savedInstanceState: Bundle?) { // ... //

    ViewPager pager.adapter = MainPagerAdapter(supportFragmentManager) // bottomBar.setOnTabSelectListener { tabId -> dispatchUnselect() when (tabId) { R.id.tab1 -> showTab1() R.id.tab2 -> showTab2() R.id.tab3 -> showTab3() } dispatchSelect() } }
  12. // MainActivity.kt override fun onCreate(savedInstanceState: Bundle?) { // ... //

    ViewPager pager.adapter = MainPagerAdapter(supportFragmentManager) // bottomBar.setOnTabSelectListener { tabId -> dispatchUnselect() when (tabId) { R.id.tab1 -> showTab1() R.id.tab2 -> showTab2() R.id.tab3 -> showTab3() } dispatchSelect() } } Fragment੾ସͷલޙͰ
 ΠϕϯτΛൃՐ
  13. private fun dispatchSelect() { val mainAdapter = pager.adapter as? MainPagerAdapter

    val fragment = mainAdapter?.currentFragment as? RootFragment fragment?.onTabSelected() } private fun dispatchUnselect() { val mainAdapter = pager.adapter as? MainPagerAdapter val fragment = mainAdapter?.currentFragment as? RootFragment fragment?.onTabUnselected() }