创建一个像Instagram底部导航栏这样的backstack

时间:2019-02-19 02:46:55

标签: java android android-fragments kotlin back-stack

我想知道如何创建类似instagram backstack的东西。

我有一个应用程序,该应用程序的底部导航栏打开了3个不同的片段(select t.*, (select sum(t2.amount) from transactions as t2 where t2.tdA = t.tdA and -- not sure if this is necessary (t2.date < t.date or t2.date = t.date and t2.id <= t.id ) ) as running_amount from transactions as t; MapFragmentHostFragment),在ProfileFragment内,我创建了一个当用户单击按钮时,一个名为HostFragment的新按钮:

Host2Fragment

一些示例:

注意:Tab1 / MapFragment是主片段

  • 用户在 Tab1 Tab2 Tab3 [MapFragment][HostFragment][ProfileFragment] . . . [Host2Fragment] 上,按返回可退回到Host2Fragment
    • 再次按回去 Tab1
  • 用户在HostFragment上,点击 Tab2 返回Host2Fragment

本质上,按回去应该将所有图层剥离回原始片段(Tab1),并同时更新底部导航栏。我还想保留片段的状态,以便当我单击它时,它应该仍处于相同状态

我到目前为止所拥有的:

MainActivity

HostFragment

HostFragment

class MainActivity : AppCompatActivity(){
    private val mapFragment: Fragment = MapFragment()
    private val hostFragment: Fragment = HostFragment()
    private val profileFragment: Fragment = ProfileFragment()
    private val fm = supportFragmentManager
    private var activeFragment = mapFragment

    lateinit var toolbar: ActionBar
    private val mOnNavigationItemSelectedListener =
        BottomNavigationView.OnNavigationItemSelectedListener { item ->

            when (item.itemId) {
            R.id.navigation_map -> {
                fm.beginTransaction().hide(activeFragment).show(mapFragment).commit()
                activeFragment = mapFragment
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_host -> {
                fm.beginTransaction().hide(activeFragment).show(hostFragment).commit()
                activeFragment = hostFragment
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_profile -> {
                fm.beginTransaction().hide(activeFragment).show(profileFragment).commit()
                activeFragment = profileFragment
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fm.beginTransaction().add(R.id.container, profileFragment, "profileFragment").hide(profileFragment).commit()
        fm.beginTransaction().add(R.id.container, hostFragment, "hostFragment").hide(hostFragment).commit()
        fm.beginTransaction().add(R.id.container, mapFragment, "mapFragment").commit()

        toolbar = supportActionBar!!
        val bottomNavigation: BottomNavigationView = findViewById(R.id.navigationView)
        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

        //the home Fragment
        bottomNavigation.selectedItemId = R.id.navigation_map
    }
}

0 个答案:

没有答案