让我添加更多上下文:
我使用ViewPager2运行底视图导航。对于底部导航视图的所有4个选项卡/片段,我都有一个选项菜单,该菜单在每个片段中分别动态创建。
现在,当我们浏览该应用程序时,它的行为正确,因为每个选项菜单仅针对其各自的片段显示。
问题是:仅在启动应用程序时,所有4个片段中的所有选项菜单才会显示在启动目标片段的选项卡上。但是,一旦我们向后滑动和向后滑动,应用程序栏上只会显示开始目标选项菜单。其他3个片段/选项卡也是如此。
理论:我认为这与onCreateOptionsMenu
有关,当同时创建了四个片段并共享一个应用栏时,也会调用const val F1_PAGE_INDEX = 0
const val F2_PAGE_INDEX = 1
const val F3_PAGE_INDEX = 2
const val F4_PAGE_INDEX = 3
class PagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
/**
* Mapping of the ViewPager page indexes to their respective Fragments
*/
private val tabFragmentsCreators: Map<Int, () -> Fragment> = mapOf(
F1_PAGE_INDEX to { FirstFragment() },
F2_PAGE_INDEX to { SecondFragment() },
F3_PAGE_INDEX to { ThirdFragment() },
F4_PAGE_INDEX to { FourthFragment() }
)
override fun getItemCount() = tabFragmentsCreators.size
override fun createFragment(position: Int): Fragment {
return tabFragmentsCreators[position]?.invoke() ?: throw IndexOutOfBoundsException()
}
}
。
有人熟悉这类问题吗?这是我的ViewPager的PagerAdapters代码:
class HomeViewPagerFragment(): Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentViewPagerBinding.inflate(inflater, container, false)
val viewPager = binding.viewPager
viewPager.isUserInputEnabled = false
viewPager.adapter = PagerAdapter(this)
//Save states of four fragments
viewPager.offscreenPageLimit = 4
(activity as AppCompatActivity).setSupportActionBar(binding.toolbar)
val bottomNavigation = binding.bottomNavView
bottomNavigation.setOnNavigationItemSelectedListener(
BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.fragment1_destination -> viewPager.currentItem = F1_PAGE_INDEX
R.id.fragment2_destination -> viewPager.currentItem = F2_PAGE_INDEX
R.id.fragment3_destination -> viewPager.currentItem = F3_PAGE_INDEX
R.id.fragment4_destination -> viewPager.currentItem = F4_PAGE_INDEX
}
true
})
return binding.root
}
}
这也是我的Home View Pager Fragment,我在其中创建我的底部导航并影响到主要片段:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.amplifyframework:amplify-tools-gradle-plugin:1.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'com.amplifyframework.amplifytools'
开始目的地(第一个片段)是应用程序启动时显示所有片段菜单的地方。
任何帮助都将受到赞赏!谢谢!
答案 0 :(得分:1)
我认为您需要做的是,仅在可见该片段时才使OptionsMenu
可见。
尝试:将其放在每个片段中
override fun onResume(){
super.onResume()
setHasOptionsMenu(isVisible())
}
仅当该片段可见时,这将使选项菜单可见。如果仅onCreateView
不起作用,则可以将其隐藏在onPause
或onResume
中。
答案 1 :(得分:1)
问题:经过数小时的努力,我们找到了解决方案。当我们将屏幕外页面限制设置为4时,便直接创建了该问题。这导致所有片段同时创建,因此,由于我们指示{{ 1}}在每个片段的setHasOptionsMenu(true)
中。
解决方案:只需将片段onCreate
中的options菜单设置为true,仅当我们以这种方式滑动到相应片段时才能调用:
onResume