开发者

Android FragmentTransaction, ActionBar Tabs, and Multiple Fragments

开发者 https://www.devze.com 2023-03-18 07:43 出处:网络
So, I am building a tablet app with the compatibility library and have run into an oddity I can\'t seem to figure out.All in one activity, I have 2 tabs (Tab A and Tab B), and 3 Fragments (Fragments A

So, I am building a tablet app with the compatibility library and have run into an oddity I can't seem to figure out. All in one activity, I have 2 tabs (Tab A and Tab B), and 3 Fragments (Fragments A1, A2, and B). The ActionBar.TabListener associated with Tab A handles the adding and removing of Fragments A1 and A2, and the Ac开发者_如何转开发tionBar.TabListener associated with Tab B handles the adding and removing of Tab B. So far so good.

The strange behavior is exhibited when I launch the activity (so Tab A is selected and Fragments A1 and A2 are displayed from left to right, correctly) click on Tab B (so Fragments A1 and A2 are removed and Fragment B is shown, still correctly) and then click back on Tab A! Now, Fragments A1 and A2 are showing, but in the reversed order: A2 and then A1!

Has anyone experienced this oddity? If I select Tab B and then Tab A again, they reverse again to be in the correct order, and the cycle continues. According to this article, "If you're adding multiple fragments to the same container, then the order in which you add them determines the order they appear in the view hierarchy", which strangely doesn't seem to be the case now does it.

Any ideas? Thanks in advance!


According to this article, "If you're adding multiple fragments to the same container, then the order in which you add them determines the order they appear in the view hierarchy", which strangely doesn't seem to be the case now does it.

Personally, I wouldn't count on that.

For example, let's assume that you are using a horizontal LinearLayout. Your current code presumably is putting both fragments in the LinearLayout. The way I approach it is to have two FrameLayouts already in the LinearLayout, and to put each fragment in one of the FrameLayouts.

0

精彩评论

暂无评论...
验证码 换一张
取 消