如何通过单击刚打开的片段来防止打开新片段?
我使用RecyClerview创建了项目的网格。当用户单击该项目时,全屏片段将打开。它运行良好。但是,当用户单击任何全屏片段表面时,我看到了循环网格电网打开的新片段时的奇怪情况。事实证明,用户(按原样)点击片段下的按钮。如何避免这种情况?
这是密码的一部分,如何组织点击聆听:
SetGrid.adapter = TestAdapter{ position ->
when (position) {
0 -> {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fr_frame, OneFragment())
transaction.disallowAddToBackStack()
transaction.commit()
}
1 -> {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fr_frame, TwoFragment())
transaction.disallowAddToBackStack()
transaction.commit()
}
2 -> {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fr_frame, ThreerFragment())
transaction.disallowAddToBackStack()
transaction.commit()
}
}
I created the grid of the items using the RecyclerView. When a user clicks the item, the full screen fragment opens. It works well. But I see the weird situation when a user clicks on any full screen fragment surface the new fragment from the RecyclerView grid opens. It turns out that the user, as it were, clicks on the button under the fragment. How can this be avoided?
Here is a chunk of the code how the click listening is organized:
SetGrid.adapter = TestAdapter{ position ->
when (position) {
0 -> {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fr_frame, OneFragment())
transaction.disallowAddToBackStack()
transaction.commit()
}
1 -> {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fr_frame, TwoFragment())
transaction.disallowAddToBackStack()
transaction.commit()
}
2 -> {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fr_frame, ThreerFragment())
transaction.disallowAddToBackStack()
transaction.commit()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您所说的背景中的片段中,您的片段触发了接触式导航。您可以从后排堆栈中将其删除,因为我看到您使用了
disallaDdTtoBackStack()
。我怀疑它无法按预期工作,因此在片段的odCreate方法中更好地使用“popbackstack()
”。From what you said your fragment in the background is triggering the on-touch navigations. You can remove it from the back stack as I see you used
disallowAddToBackStack()
. I suspect it is not working as intended so its better to "popBackStack()
" in the oncreate method of the fragment.