[英]When I show a fragment the objects below still respond to clicks
I have some fragments that are part of a view pager. 我有一些片段是视图寻呼机的一部分。 Those work fine.
那些工作正常。 However, on top of the fragments in the view pager, I want to put fragments on top of them (ie nested or child fragments).
但是,在视图分页器中的片段之上,我想将片段放在它们之上(即嵌套片段或子片段)。 So, in OnCreateView of the parent fragment, I go ahead and declare my child fragment but then hide it right away.
因此,在父片段的OnCreateView中,我继续声明我的子片段,然后立即将其隐藏。
optionsFragment = new TrackOptions();
var trans = Activity.SupportFragmentManager.BeginTransaction();
trans.Add(trackOptionsContainer.Id, optionsFragment, "Track Options");
trans.Hide(optionsFragment);
trans.Commit();
Then, in the parent fragment, when I want to show this child fragment, I simply call to show it and when I want it to go away, I hide it again. 然后,在父片段中,当我想要显示该子片段时,我只是调用以显示它,而当我希望它消失时,我再次将其隐藏。
At first, I noticed that the child was transparent and I could see through it to the parent. 起初,我注意到孩子是透明的,我可以透过父母看到它。 If I then set a background, that problem went away.
如果再设定背景,该问题就消失了。
However, now I noticed that while I am viewing the child and clicking on screen, the parent will still respond to events (click events) for buttons that aren't visible because they are on the parent, which is covered by showing the child. 但是,现在我注意到,当我查看孩子并单击屏幕时,父级仍会响应事件(单击事件),因为按钮在父级上不可见,因此显示了孩子。
I suspect this is because I need to explicitly also hide the parent when I am showing the child. 我怀疑这是因为当我向孩子展示时,我还需要明确隐藏父母。 Is this correct?
这个对吗? If so, here are two follow up questions that I don't understand.
如果是这样,这是我不理解的两个后续问题。 1. I don't want to re-instantiate the parent each time I want to show it.
1.我不想每次都想给父母重新实例化。 I just want the one instance that was created when the app launched.
我只想要应用启动时创建的一个实例。 If I have to explicitly hide it, how do I show it again while using the same instance of the fragment class?
如果必须显式隐藏它,如何在使用片段类的相同实例时再次显示它? 2. I have read so many different options to get an event when a fragment is shown and I have tried most of them (OnHiddenChanged, OnResume, UserVisibleHint) but none of them seem to work well with this child fragment setup.
2.我已经阅读了很多不同的选项,以便在显示片段时获得事件,并且我尝试了其中的大多数选项(OnHiddenChanged,OnResume,UserVisibleHint),但是这些选项似乎都不适合此子片段设置。 What even do I use when my child fragment is shown?
显示我的孩子片段时,我什至还要使用什么?
Thanks! 谢谢!
Mike 麦克风
Just set your fragment as clickable, thus it will not allow clicks to pass through. 只需将您的片段设置为可点击,就不会允许点击通过。 Don't worry, as the background of the fragment is not a clickable background, you will not see any clicks.
不用担心,因为片段的背景不是可点击的背景,所以您不会看到任何点击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.