Android ViewPager2 FragmentStateAdapter显示每个片段中的菜单 [英] Android ViewPager2 FragmentStateAdapter shows Menu from every Fragment

查看:133
本文介绍了Android ViewPager2 FragmentStateAdapter显示每个片段中的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有底部导航视图的活动,该活动具有关联的3个片段.其中之一是带有ViewPager2的Fragment,该片段使用FragmentStateAdapter.在Adapter的createfragment(int position)方法内部,我返回了另一个具有选项菜单的Fragment的几个实例,在它的onCreate()内部,我调用了setHasOptionsMenu(true).问题在于,在适配器初始化时,所有片段中的菜单都是可见的,而不仅仅是当前可见片段中的菜单.第一次滑动后,菜单似乎失效了,一切都应按原样进行.如果我通知适配器有关已添加另一个片段的更改,也会出现此问题,然后为新添加的片段显示另一个菜单项.我看到了一个类似的问题,但是答案是在片段的onResume()方法内调用setHasOptionsMenu(),但这似乎并不是解决此问题的正确方法.在评论中,另一个用户建议将ViewPager2升级到另一个版本,但是我使用的是最新版本.有人可以告诉我如何处理吗?

i have an activity with bottom navigation view with 3 Fragments associated. One of them is a Fragment with a ViewPager2 that uses a FragmentStateAdapter. Inside the createfragment(int position) method of the Adapter I return a couple Instances of another Fragment which has an Options Menu and inside it's onCreate() i call setHasOptionsMenu(true). The Problem is that on initialization of the adapter, the menu from all fragments are visible not just the menu of the currently visible fragment. After the first swipe it seems that the menu gets invalidated and everything is as it should be. The problem also occurs if i notify the adapter about changes that another fragment is added, then another menu item appears for the newly added fragment. I saw a similar question but the answer was to call setHasOptionsMenu() inside the onResume() method of the fragment but that doesn't seem to be the right behavior to handle this. In a comment another user suggested to upgrade ViewPager2 to another version but I am using the latest version. Can anybody tell me how to handle this?

推荐答案

我加入StackOverflow只是为了回答这个问题,因为我也在这个确切的问题上浪费了时间.

I joined StackOverflow just to reply to this question as I've also wasted time on this exact issue.

此修复程序是将ViewPager2更新到最新的alpha01版本.

The fix is to update to the latest alpha01 version of ViewPager2.

implementation "androidx.viewpager2:viewpager2:1.1.0-alpha01"

请参阅: https://developer.android.com/jetpack/androidx/releases/viewpager2#1.1.0-alpha01

修复了将片段添加到FragmentManager时具有初始片段菜单可见性的FragmentStateAdapter问题.(I9d2ff,b/144442240)

Fixed FragmentStateAdapter issue with initial fragment menu visibility when adding a fragment to the FragmentManager. (I9d2ff, b/144442240)

这篇关于Android ViewPager2 FragmentStateAdapter显示每个片段中的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆