Example tutorial of using listfragment in Android App

Listfragment inherits from fragment. Therefore, it has the feature of fragment and can be used as a part of activity. The purpose is to make page design more flexible. Compared with fragment, the contents of listfragment are displayed in the form of list. The layout of listfragment contains a listview by default. Therefore, in the layout file corresponding to listfragment, you must specify a listview control whose Android: ID is "@ Android: ID / list"!

The following describes the steps to display listfragments in an activity.

1. Code corresponding to activity

2. Layout corresponding to activity

Note: only one fragment is included in the layout of this activity. Let's take a look at the content of mylistfragment.

3. Contents of mylistfragment

Note: mylistfragment is a custom listfragment. It uses a list_ Fragment.xml as the layout, and through android.r.layout.simple_ list_ item_ 1 displays each item in the listview.

4. list_ Content of fragment.xml

"Activity layout and code" is the same as before, and the description will not be repeated here.

5. Contents of mylistfragment

Note: mylistfragment uses r.layout.list_ Fragment is used as the layout, and r.layout.item is used as the layout for each item in the listview.

6. list_ Content of fragment.xml

<!-- The Android: ID value corresponding to listfragment is fixed as "@ ID / Android: List" - > < listview Android: id = "@ ID / Android: List" Android: Layout_ width="match_parent" android:layout_ height="match_parent" android:drawSelectorOnTop="false" />

7. Content of item.xml

Listfragment instance application example description: create an activity, including 2 listfragments. The first listfragment uses the contents of each line of the listview in Android. R.layout.simple_ list_ item_ 1) display according to the layout; The second listfragment displays the contents of each line through a custom layout file, and each line displays two text.

Layout file code corresponding to activity:

Note: (01) the layout contains two fragments. Activity Code:

Description: (01) in onCreateView (), call list_ fragment_ Impl as the layout file of the listfragment. (02) in oncreate(), set android.r.layout.simple through setlistadapter()_ list_ item_ 1 for the layout file of each row of listview, set cities as each item of data in it.

Code of listfragmentimpl.java:

list_ fragment_ Impl.xml content:

Code of listfragmentself.java:

package com.skywang.app;

explain:

(01) in onCreateView (), calling list_ fragment_ Self as the layout file of the listfragment. (02) in oncreate(), set r.layout.two through setlistadapter()_ Textview is the layout file for each row of the listview.

list_ fragment_ Content of self.xml:

two_ Contents of textview.xml:

design sketch:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>