Android选项菜单和选项卡布局在刷新时重复菜单并取消它们

发布于 2025-02-03 06:25:53 字数 1497 浏览 3 评论 0原文

问题是在刷卡布局时将重复菜单图标。如果滑动被取消,则重复(实际上是重复的,但是其他片段具有的另一个菜单布局或滑动的目标片段)停留在那里。但是,如果滑动成功,那么在滑动或取消时不会再出现。它仅在未初始化片段的情况下首次发生,

这是菜单项重复时的样子,然后取消 1

取消刷卡时

“其他选项卡 3

,当我单击“从计算机片段”中单击订单片段时),它将显示3个重复的刷新图标(菜单项),然后在刷新后暂时消失(当OnPagesCrollStateChanged中的状态在继续时等于0或在其他片段中闲置)(

在继续刷卡后(或当TablayOut中的每个片段)已经开始了),菜单来回刷动时(或在onPagesCrollStateChanged中的状态= 1时)

我使用sethasoptionsmenu() 我在同一菜单布局中

使用父母片段中的界面尝试了此此操作,但它不起作用(仅在成功地滑动到其他片段后,它才能使用菜单图标在刷卡布局时会消失,但是当其他片段在其他片段时行不通片段从未访问过)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
   

    setHasOptionsMenu(true)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, inflater)
    inflater.inflate(R.menu.refresh_menu, menu)
    HomeFragment.setHideRefresButtonOnPageScrolled(object: HomeFragment.HideRefreshButton {
        override fun onSwipe(state: Int) {
            if (state == 1) {
                menu.findItem(R.id.refresh).isVisible = false
                activity?.invalidateOptionsMenu()
            } else if (state == 0){
                menu.findItem(R.id.refresh).isVisible = true
                activity?.invalidateOptionsMenu()
            }
        }
    })
}

如何解决此问题?任何帮助都将受到赞赏

The problem is the menu icon is duplicated when swiping the tab layout. If the swipe is canceled then the duplicate (not duplicate actually, but the other menu layout that the other fragment has or the destination fragment of the swipe) stays there. But if the swipe is successful, then it wont appear again when swiping or cancelling. It only happens for the first time when the fragment has not been initialized

Here is what it looks like by the menu items duplicate when swiping then cancel1

When swiping is canceled2

When swiping is continued to other tabs3

AND, when I clicked the order fragment from the tab layout (from the machine fragment), it will show 3 duplicate refresh icon (menu items) momentarily and then disappear after the swipe (when state in onPageScrollStateChanged is equal to 0 or idle in other fragment) is settled

After the swiping is continued (or when each fragment in the tablayout is already initiated), the menu wont duplicate anymore when swiping back and forth (or when state = 1 in onPageScrollStateChanged)

I used setHasOptionsMenu() in every fragment that is in the tab layout and each of them inflate the same menu layout

I tried this using interface from the parent fragment but it doesn't work (it ONLY WORKS after successfully swiping to other fragment where the menu icon will disappear when swiping the tab layout, but it doesn't work when the other fragment has never been accessed)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
   

    setHasOptionsMenu(true)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, inflater)
    inflater.inflate(R.menu.refresh_menu, menu)
    HomeFragment.setHideRefresButtonOnPageScrolled(object: HomeFragment.HideRefreshButton {
        override fun onSwipe(state: Int) {
            if (state == 1) {
                menu.findItem(R.id.refresh).isVisible = false
                activity?.invalidateOptionsMenu()
            } else if (state == 0){
                menu.findItem(R.id.refresh).isVisible = true
                activity?.invalidateOptionsMenu()
            }
        }
    })
}

how do I fix this? any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2025-02-10 06:25:53

通过调用

菜单

来修复它

fixed it by calling

menu.clear

before inflating another menu on onCreateOptionsMenu in each fragment in tabs layout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文