我想知道如何创建类似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;
,MapFragment
,HostFragment
),在ProfileFragment
内,我创建了一个当用户单击按钮时,一个名为HostFragment
的新按钮:
Host2Fragment
一些示例:
注意:Tab1 / MapFragment是主片段
Tab1 Tab2 Tab3
[MapFragment][HostFragment][ProfileFragment]
.
.
.
[Host2Fragment]
上,按返回可退回到Host2Fragment
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
}
}