The following tutorial is a stripped down fragment-based interface. If you've been thinking about giving the fragment API a try in your apps, this tutorial is for you. You can follow along or download the project and import it directly into Eclipse.
1. Create a new Android project using Eclipse. To natively support fragments, you must target Android 3.0 (Honeycomb) or greater. Be sure to change the name of the startup activity to Main.java.
2. For the demo, we will use two layouts: One to represent our main container which holds the two fragments, and a second one to define our detail fragment. The list fragment we will define in code by extending the handy ListFragment class.
3. We are ready to move on to the source folder. Because fragments act almost like activities, the code in Main.java does nothing more than load the topmost layout.
4. Our DetailFrag.java implementation is nearly as simple; it inflates a view and exposes a setText method.
5. Last is the ListFrag.java file. We create a list view, implement the on click, and make use of the fragment manager to reference one fragment from another. In the onListItemClick() method, notice the check for whether the target fragment exists in this particular layout -- that allows a developer to implement a separate layout for smaller screen devices like phones.
We are ready to execute the demo.
0 comments:
Post a Comment