Examples of listfragment for Android
1. Introduction to fragment
My understanding of fragment is based on activity. For most basic development, the first thing we encounter is to develop with activity.
For a simple example, create a basic Android blank interface, and we get an app that can display a blank interface. An activity corresponds to a layout.
However, fragment is based on activity and breaks through the limitation of the fixed layout. In the original layout, the layout elements are used as containers to dynamically accommodate the new layout.
This is equivalent to having multiple interfaces in one activity.
2. Listfragment example explanation
Final effect
The final effect is shown in the figure above
2.1. First, let's talk about the preparation activity_ Layout of Main: activity_ main.xml
The linear layout here consists of three parts (1) layout_ title(2)fragment_ container(3)layout_ bottom
Where (2) fragment_ Container is where listfragment is loaded dynamically.
2.2. Second, let's take a look at the dynamic loading into the fragment_ Layout in container: file fragment_ order.xml
By analyzing the above XML, we can see that in order to dynamically load a listfragment, we have written an XML with listview component, which is necessary.
2.3. Third, let's take a look at how to dynamically load listfragment in the activity
Let's look at the key parts of mainactivity. Java
The place I specially marked is the code used for dynamic loading.
In order to load the fragment, we need to write a fragment class. We can see that the object of this class is used in the add function. It is also here to load the fragment.
Use the add function of the fragmentmanager to load. It has three parameters: (1) the location where the fragment is loaded (r.id.fragment_container) (2) a fragment object. The writing of this object is also very important, which will be discussed later. (3) Give a name to the dynamically loaded fragment. This item is optional.
2.4. Step 4: write the class of fragment object
Fragment in step 2 above_ Order.xml is used and instantiated by this class. It is precisely because of this class that fragments can be instantiated and loaded dynamically.
I mainly want to talk about the usage of simpleadapter, because it is very important. If the data cannot be bound to layout, it will not run successfully.
It is important to use simpleadapter. The reason why simpleadapter is used is very simple. Binding data and layout cannot be completed automatically by the program. The corresponding relationship between data and layout needs to be determined by yourself. Adapter is to bind the corresponding data to the corresponding layout
Simpleadapter is a relatively simple and easy-to-use one of the adapters
The listitem uses the data format of map < string, Object >, which represents the data in each line.
List is a series of map < string, Object >
Let's look at the parameters of simpleadapter. There are five in total: (1) get the current activity; (2) list that has saved the data; (3) another XML, which is used as the layout of an item in listview, so that the appearance of an item can be determined; (4) this array indicates the name code of the data in map < string, Object >, In this way, the adapter has a reference only when it fetches the data of each entry in the list. This parameter is also closely related to the next parameter. (5) this parameter is the ID in the layout, which corresponds to the previous parameter. Guided by the name of the data of the previous parameter, the data of each row can be corresponding to the corresponding ID.
2.5. Finally, write the layout code of each line of listview: list_ item.xml
Finally, I wish you a happy new year and a happy year of chicken!!!