开发者

Android Fragment Navigation

I have a screen with 4 fragments inside it:frag1,frag2,frag3,frag4.In frag1,there is a listview displaying some items.So frag1 is 开发者_运维问答a listfragment. Now on the click of listitem i want to replace frag3 with a new listfragment frag5. Remember frag3 is there in the screen layout file but the new frag5 is not there in the layout.How to do this?


You should not put frag3 in the layout file. Instead you have to use FrameLayout in your layout file which acts as a fragment container and add the fragment to that container dynamically.


As already said - do not put fragments in your XML layout files directly. Use empty container (like FrameLayout) and replace its content. My approach is to tag each fragment thats ends in certain container with the same tag (so tag is unique per container and since I got one fragment per container, there's no conflict). Then you simply replace fragment with that tag with new one fragment when you need to do that. And since it's safe to replace tagged fragment even if you in fact got no fragment in container yet, you do not need to bother with checking yourself.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜