![]() If here I We want to imitate the effect similar to the return stack, press the Back key to return to the previous fragment, how to achieve it? It’s actually very simple. In the previous section, we successfully implemented the function of dynamically adding Android Fragments to the activity, but if you try it, it will be posted Now, after adding a fragment by clicking the button, the program will exit directly by pressing the Back key. ![]() Simulate the return stack in the fragment: Then click the button, the effect is shown in Figure7. Re-run the program and you can see the same boundary as before. This completes the function of dynamically adding Android Fragments to the activity. ![]() Commit the transaction and call the commit() method to complete.You need to pass in the id of the container and the fragments to be added Instances. Adding Android Fragments to the container is generally implemented using the replace() method.To start a transaction, start it by calling the beginTransaction() method.Get the FragmentManager, which can be obtained by directly calling the getFragmentManager() method in the activity.Create a fragment instance to be added.Combining the code, it can be seen that dynamically adding Android Fragments is divided into 5 steps. We continue to improve on the basis of the code in the previous section, and create another_right_fragment.xml, the code is as follows Shown:Īs you can see, first we register a click event for the button in the left fragment, and then dynamically add the fragment The logic is placed in the click event. Add Android Fragments dynamically according to the specific situation, you can The program interface can be customized more diversified. In the previous section, you have learned how to add Android Fragments to the layout file, but the real power of Android Fragments The point is that it can be dynamically added to the activity while the program is running. But this example is too simple It’s hard to have any practical role in a real project, so let’s take a look at the more advanced skills. figure6Īs we expected, the two Android Fragments equally divided the layout of the entire event. So the simplest fragment example has been written, now run the program, the effect is shown in Figure6. Note one Be sure to add the package name of the class. Now start the tablet simulator, the effect is shown in Figure 5Īs you can see, we used the tag to add Android Fragments to the layout, and most of the attributes you specify It is familiar, but here you need to explicitly specify the name of the fragment class to be added through the android:name attribute. Since there seems to be a bug in the tablet simulator of the 4.0 system, here I will create a new tablet simulator of the figure2 system, such as As shown in Figure 4. You already know, broken Tablets are usually used in tablet development, so the first thing we have to do is to create a new tablet simulation Device. Having introduced so many abstract things, it is time to learn the specific usage of fragments. Therefore, a better design solution is to put the news headline list interface and the news detailed content interface in two pieces respectively Then, the two fragments are introduced in the same activity so that the screen space can be fully utilized, as shown in the figure As shown in figure3. The title of the news is generally not too long, which will result in a lot of blank areas on the interface, as shown in Figure2. However, if you design the same on the tablet, the news headline list will be stretched to fill the entire tablet. If it is designed for a mobile phone, we can put the news headline list in In one activity, put the details of the news in another activity, as shown in Figure1. When one of the headlines is clicked, open another interface to display the detailed content of the news. So how to use the fragments to make full use of the space of the tablet screen? Imagine we are developing a News application, one of the interfaces uses ListView to display a set of news headlines. ![]() You can even understand the fragments as a mini activity, although these mini Activities may be as big as normal activities. Although fragmentation should be a brand new concept for you to read, I believe you should have no effort to learn because it is so similar to activities, it can also contain layout, It also has its own life cycle.
0 Comments
Leave a Reply. |