Here's a bit of code I use to manage Compatibility Library Fragments in Android. Hopefully this will help someone else out.
fragmentById()
fragmentById() is doing a few odd things. <T extends Fragment> T is saying that we don't care about type-safety beyond the assurance that type T extends the Fragment class. It's worth noting that if we dropped <T extends Fragment> in favor of <T> T, then we'd lose all type safety. Here's how fragmentById() is used:The main benefits of this method are that it gets rid of a cast and it isolates the compatibility library bits off to the side (e.g. the calls to getSupportFragmentManager()).
showFragmentIds() / hideFragmentIds() / setFragmentsVisibility()
The Enum argument is an attempt at readibility, a boolean wouldn't be readable and would have the downside of limiting my ability to, say, put a 3rd visibility mode in to rip out a fragment completely. Here's what that method looks like with a boolean parameter:</rant>. The only other thing to note about setFragmentsVisibility() is the varargs parameter int... fragmentIds. This approach makes the calling code nice and simple, e.g.:As always, I welcome suggestions for accomplishing this kind of thing in a more elegant way :)
One thing to note:
The above solution works fine for trivial Fragment cases, but for more interesting things (such as specifying enter and exit animations), your best bet is to stick with the built-in fragment support.
Wednesday, November 23, 2011
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment