我正在尝试在现有应用中实现Navigation with Jetpack's architecture components。
我有一个活动应用程序,其中主要片段( | ID | data |
|----|--------|
| 1 | hello |
| 1 | hear |
| 2 | roses |
| 2 | red |
| 3 | water |
| 4 | pie |
| 5 | love |
| 5 | chicken|
| 5 | pie |
)是项目列表。当前,当用户点击列表项时,ListFragment
将第二个片段添加到堆栈中。因此,当按下后退按钮fragmentTransaction.add(R.id.main, detailFragment)
时,DetailFragment
再次显示。
使用导航体系结构可以自动处理。而不是添加新的片段replaced,因此片段视图被破坏,按下返回键以重新创建视图时,将调用ListFragment
并调用onDestroyView()
。
我知道这是与LiveData和ViewModel一起使用的一种很好的模式,可以避免使用不必要的内存,但是在我的情况下,这很烦人,因为该列表具有复杂的布局并且夸大了时间和CPU消耗,也是因为我需要保存列表的滚动位置并再次滚动到用户离开片段的相同位置。有可能,但似乎应该存在更好的方法。
我试图将视图保存在片段的私有字段中,如果已经存在,请在onCreateView()
上重新使用它,但这似乎是一种反模式。
onCreateView()
还有其他更优雅的方法来避免重新布局吗?
答案 0 :(得分:6)
伊恩湖回答说,我们可以将视图存储在变量中,并且代替可以扩大新布局,只需返回在onCreateView()
渗漏可能显示为渗漏,但其误报。
答案 1 :(得分:2)
如果您关注的是来自 google 的高级示例,他们会使用扩展名。这是它的修改版本。在我的情况下,我必须在它们附加和分离时显示和隐藏片段:
/**
* Manages the various graphs needed for a [BottomNavigationView].
*
* This sample is a workaround until the Navigation Component supports multiple back stacks.
*/
fun BottomNavigationView.setupWithNavController(
navGraphIds: List<Int>,
fragmentManager: FragmentManager,
containerId: Int,
intent: Intent
): LiveData<NavController> {
// Map of tags
val graphIdToTagMap = SparseArray<String>()
// Result. Mutable live data with the selected controlled
val selectedNavController = MutableLiveData<NavController>()
var firstFragmentGraphId = 0
// First create a NavHostFragment for each NavGraph ID
navGraphIds.forEachIndexed { index, navGraphId ->
val fragmentTag = getFragmentTag(index)
// Find or create the Navigation host fragment
val navHostFragment = obtainNavHostFragment(
fragmentManager,
fragmentTag,
navGraphId,
containerId
)
// Obtain its id
val graphId = navHostFragment.navController.graph.id
if (index == 0) {
firstFragmentGraphId = graphId
}
// Save to the map
graphIdToTagMap[graphId] = fragmentTag
// Attach or detach nav host fragment depending on whether it's the selected item.
if (this.selectedItemId == graphId) {
// Update livedata with the selected graph
selectedNavController.value = navHostFragment.navController
attachNavHostFragment(fragmentManager, navHostFragment, index == 0, fragmentTag)
} else {
detachNavHostFragment(fragmentManager, navHostFragment)
}
}
// Now connect selecting an item with swapping Fragments
var selectedItemTag = graphIdToTagMap[this.selectedItemId]
val firstFragmentTag = graphIdToTagMap[firstFragmentGraphId]
var isOnFirstFragment = selectedItemTag == firstFragmentTag
// When a navigation item is selected
setOnNavigationItemSelectedListener { item ->
// Don't do anything if the state is state has already been saved.
if (fragmentManager.isStateSaved) {
false
} else {
val newlySelectedItemTag = graphIdToTagMap[item.itemId]
if (selectedItemTag != newlySelectedItemTag) {
// Pop everything above the first fragment (the "fixed start destination")
fragmentManager.popBackStack(
firstFragmentTag,
FragmentManager.POP_BACK_STACK_INCLUSIVE
)
val selectedFragment = fragmentManager.findFragmentByTag(newlySelectedItemTag)
as NavHostFragment
// Exclude the first fragment tag because it's always in the back stack.
if (firstFragmentTag != newlySelectedItemTag) {
// Commit a transaction that cleans the back stack and adds the first fragment
// to it, creating the fixed started destination.
if (!selectedFragment.isAdded) {
fragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.nav_default_enter_anim,
R.anim.nav_default_exit_anim,
R.anim.nav_default_pop_enter_anim,
R.anim.nav_default_pop_exit_anim
)
.add(selectedFragment, newlySelectedItemTag)
.setPrimaryNavigationFragment(selectedFragment)
.apply {
// Detach all other Fragments
graphIdToTagMap.forEach { _, fragmentTagIter ->
if (fragmentTagIter != newlySelectedItemTag) {
hide(fragmentManager.findFragmentByTag(firstFragmentTag)!!)
}
}
}
.addToBackStack(firstFragmentTag)
.setReorderingAllowed(true)
.commit()
} else {
fragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.nav_default_enter_anim,
R.anim.nav_default_exit_anim,
R.anim.nav_default_pop_enter_anim,
R.anim.nav_default_pop_exit_anim
)
.show(selectedFragment)
.setPrimaryNavigationFragment(selectedFragment)
.apply {
// Detach all other Fragments
graphIdToTagMap.forEach { _, fragmentTagIter ->
if (fragmentTagIter != newlySelectedItemTag) {
hide(fragmentManager.findFragmentByTag(firstFragmentTag)!!)
}
}
}
.addToBackStack(firstFragmentTag)
.setReorderingAllowed(true)
.commit()
}
}
selectedItemTag = newlySelectedItemTag
isOnFirstFragment = selectedItemTag == firstFragmentTag
selectedNavController.value = selectedFragment.navController
true
} else {
false
}
}
}
// Optional: on item reselected, pop back stack to the destination of the graph
setupItemReselected(graphIdToTagMap, fragmentManager)
// Handle deep link
setupDeepLinks(navGraphIds, fragmentManager, containerId, intent)
// Finally, ensure that we update our BottomNavigationView when the back stack changes
fragmentManager.addOnBackStackChangedListener {
if (!isOnFirstFragment && !fragmentManager.isOnBackStack(firstFragmentTag)) {
this.selectedItemId = firstFragmentGraphId
}
// Reset the graph if the currentDestination is not valid (happens when the back
// stack is popped after using the back button).
selectedNavController.value?.let { controller ->
if (controller.currentDestination == null) {
controller.navigate(controller.graph.id)
}
}
}
return selectedNavController
}
private fun BottomNavigationView.setupItemReselected(
graphIdToTagMap: SparseArray<String>,
fragmentManager: FragmentManager
) {
setOnNavigationItemReselectedListener { item ->
val newlySelectedItemTag = graphIdToTagMap[item.itemId]
val selectedFragment = fragmentManager.findFragmentByTag(newlySelectedItemTag)
as NavHostFragment
val navController = selectedFragment.navController
// Pop the back stack to the start destination of the current navController graph
navController.popBackStack(
navController.graph.startDestination, false
)
}
}
private fun BottomNavigationView.setupDeepLinks(
navGraphIds: List<Int>,
fragmentManager: FragmentManager,
containerId: Int,
intent: Intent
) {
navGraphIds.forEachIndexed { index, navGraphId ->
val fragmentTag = getFragmentTag(index)
// Find or create the Navigation host fragment
val navHostFragment = obtainNavHostFragment(
fragmentManager,
fragmentTag,
navGraphId,
containerId
)
// Handle Intent
if (navHostFragment.navController.handleDeepLink(intent)
&& selectedItemId != navHostFragment.navController.graph.id
) {
this.selectedItemId = navHostFragment.navController.graph.id
}
}
}
private fun detachNavHostFragment(
fragmentManager: FragmentManager,
navHostFragment: NavHostFragment
) {
fragmentManager.beginTransaction()
.hide(navHostFragment)
.commitNow()
}
private fun attachNavHostFragment(
fragmentManager: FragmentManager,
navHostFragment: NavHostFragment,
isPrimaryNavFragment: Boolean,
fragmentTag: String
) {
if (navHostFragment.isAdded) return
fragmentManager.beginTransaction()
.add(navHostFragment, fragmentTag)
.apply {
if (isPrimaryNavFragment) {
setPrimaryNavigationFragment(navHostFragment)
}
}
.commitNow()
}
private fun obtainNavHostFragment(
fragmentManager: FragmentManager,
fragmentTag: String,
navGraphId: Int,
containerId: Int
): NavHostFragment {
// If the Nav Host fragment exists, return it
val existingFragment = fragmentManager.findFragmentByTag(fragmentTag) as NavHostFragment?
existingFragment?.let { return it }
// Otherwise, create it and return it.
val navHostFragment = NavHostFragment.create(navGraphId)
fragmentManager.beginTransaction()
.add(containerId, navHostFragment, fragmentTag)
.commitNow()
return navHostFragment
}
private fun FragmentManager.isOnBackStack(backStackName: String): Boolean {
val backStackCount = backStackEntryCount
for (index in 0 until backStackCount) {
if (getBackStackEntryAt(index).name == backStackName) {
return true
}
}
return false
}
private fun getFragmentTag(index: Int) = "bottomNavigation#$index"
答案 2 :(得分:1)
尽管我认为NavigationAdvancedSample是更好的解决方案,但我也使用@ shahab-rauf的代码解决了此问题。因为我没有足够的时间将其应用到我的项目中。
abstract class AppFragment: Fragment() {
private var persistingView: View? = null
private fun persistingView(view: View): View {
val root = persistingView
if (root == null) {
persistingView = view
return view
} else {
(root.parent as? ViewGroup)?.removeView(root)
return root
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val p = if (persistingView == null) onCreatePersistentView(inflater, container, savedInstanceState) else persistingView // prevent inflating
if (p != null) {
return persistingView(p)
}
return super.onCreateView(inflater, container, savedInstanceState)
}
protected open fun onCreatePersistentView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return null
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (persistingView != null) {
onPersistentViewCreated(view, savedInstanceState)
}
}
protected open fun onPersistentViewCreated(view: View, savedInstanceState: Bundle?) {
logv("onPersistentViewCreated")
}
}
class DetailFragment : AppFragment() {
override fun onCreatePersistentView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// I used data-binding
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_program_detail, container, false)
binding.model = viewModel
binding.lifecycleOwner = this
return binding.root
}
override fun onPersistentViewCreated(view: View, savedInstanceState: Bundle?) {
super.onPersistentViewCreated(view, savedInstanceState)
// RecyclerView bind with adapter
binding.curriculumRecycler.adapter = adapter
binding.curriculumRecycler.apply {
layoutManager = LinearLayoutManager(context)
setHasFixedSize(true)
}
viewModel.curriculums.observe(viewLifecycleOwner, Observer {
adapter.applyItems(it ?: emptyList())
})
viewModel.refresh()
}
}
答案 3 :(得分:0)
我这样尝试过,它对我有用。
ViewModel
初始化navGraphViewModels
(在导航范围内运行)ViewModel
// fragment.kt
private val vm by navGraphViewModels<VM>(R.id.nav_graph) { vmFactory }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Restore state
vm.state?.let {
(recycler.layoutManager as GridLayoutManager).onRestoreInstanceState(it)
}
}
override fun onPause() {
super.onPause()
// Store state
vm.state = (recycler.layoutManager as GridLayoutManager).onSaveInstanceState()
}
// vm.kt
var state:Parcelable? = null
答案 4 :(得分:0)
您可以通过以下实现对片段进行永久查看
BaseFragment
open class BaseFragment : Fragment(){
var hasInitializedRootView = false
private var rootView: View? = null
fun getPersistentView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?, layout: Int): View? {
if (rootView == null) {
// Inflate the layout for this fragment
rootView = inflater?.inflate(layout,container,false)
} else {
// Do not inflate the layout again.
// The returned View of onCreateView will be added into the fragment.
// However it is not allowed to be added twice even if the parent is same.
// So we must remove rootView from the existing parent view group
// (it will be added back).
(rootView?.getParent() as? ViewGroup)?.removeView(rootView)
}
return rootView
}
}
MainFragment
class MainFragment : BaseFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return getPersistentView(inflater, container, savedInstanceState, R.layout.content_main)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (!hasInitializedRootView) {
hasInitializedRootView = true
setListeners()
loadViews()
}
}
}
答案 5 :(得分:0)
如果您仍无法解决导航组件的问题,请使用通用功能:
get_text()
当您按下按钮时,它将很容易将视图保存到后退!
答案 6 :(得分:0)
这与@Shahab Rauf 建议的答案相同,只是额外的事情是包含数据绑定并仅在 BaseFragment 而不是 Child Fragment 中实现 onCreateView。并在 BaseFragment 的 onViewCreated() 中初始化 navController。
基础片段
abstract class BaseFragment<T : ViewDataBinding, VM : BaseViewModel<UiState>> : Fragment() {
protected lateinit var binding: T
var hasInitializedRootView = false
private var rootView: View? = null
protected abstract val mViewModel: ViewModel
protected lateinit var navController: NavController
fun getPersistentView(
inflater: LayoutInflater?,
container: ViewGroup?,
savedInstanceState: Bundle?,
layout: Int
): View? {
if (rootView == null) {
binding = DataBindingUtil.inflate(inflater!!, getFragmentView(), container, false)
//setting the viewmodel
binding.setVariable(BR.mViewModel, mViewModel)
// Inflate the layout for this fragment
rootView = binding.root
} else {
// Do not inflate the layout again.
// The returned View of onCreateView will be added into the fragment.
// However it is not allowed to be added twice even if the parent is same.
// So we must remove rootView from the existing parent view group
// (it will be added back).
(rootView?.getParent() as? ViewGroup)?.removeView(rootView)
}
return rootView
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? = getPersistentView(inflater, container, savedInstanceState, getFragmentView())
//this method is used to get the fragment layout file
abstract fun getFragmentView(): Int
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
}
}
HomeFragment(任何扩展 BaseFragment 的 Fragment)
class HomeFragment : BaseFragment<HomeFragmentBinding, HomeViewModel>(),
RecycleViewClickListener {
override val mViewModel by viewModel<HomeViewModel>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (!hasInitializedRootView) {hasInitializedRootView = true
setListeners()
loadViews()
--------
}
答案 7 :(得分:0)
您好,问题在最新版本 2.4.0-alpha01 中得到修复,现在官方支持多个 backstack 导航
查看链接: https://developer.android.com/jetpack/androidx/releases/navigation#version_240_2
答案 8 :(得分:0)
这将有助于加快片段创建速度,并且当您使用数据绑定时,视图模型数据仍将保存在视图中,以防被按下。
就这样做:
lateinit var binding: FragmentConnectBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
if (this::binding.isInitialized) {
binding
} else {
binding = FragmentConnectBinding.inflate(inflater, container, false)
binding.viewModel = viewModel
binding.model = connectModel
binding.lifecycleOwner = viewLifecycleOwner
viewModel.buildAllProfiles()
// do what ever you need to do in first creation
}
setupObservers()
return binding.root
}
答案 9 :(得分:0)
如果您使用的是 DataBinding,则仅更改以下编码。这是 Shahab Rauf 的可编辑答案
BaseFragment
open class BaseFragment<T : ViewDataBinding> : Fragment() {
var hasInitializedRootView = false
private var rootView: View? = null
lateinit var binding: T
fun getPersistentView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
layout: Int
): View? {
if (rootView == null) {
binding = DataBindingUtil.inflate(inflater, layout, container, false)
rootView = binding.root
} else {
(rootView?.parent as? ViewGroup)?.removeView(rootView)
}
return rootView
}
}
主片段
class MainFragment: BaseFragment<FragmentProfileBinding>() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return getPersistentView(inflater, container, savedInstanceState, R.layout.fragment_profile)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (!hasInitializedRootView) {
hasInitializedRootView = true
setListeners()
loadViews()
}
}
private fun setListeners(){
binding.btnShare.setOnClickListener { }
}
private fun loadViews(){}
}