Android Program Development: (13) special fragments-13.1 ListFragment

Source: Internet
Author: User

ListFramgent is a Fragment containing ListView. It can display a series of information through data sources (arrays or cursors. ListFragment is very useful, just like RSS. It may display a list on the left and the content corresponding to the selected list on the right.

You can create a ListFragment object by inheriting the ListFragment object. The following describes how to use ListFragment.

1. Create a project: ListFragmentExample.

2. Code in main. xml.

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "horizontal">
 
<Fragment
Android: name = "net. manoel. ListFragmentExample. Fragment1"
Android: id = "@ + id/fragment1"
Android: layout_weight = "0.5"
Android: layout_width = "0dp"
Android: layout_height = "200dp"/>
 
<Fragment
Android: name = "net. manoel. ListFragmentExample. Fragment1"
Android: id = "@ + id/fragment2"
Android: layout_weight = "0.5"
Android: layout_width = "0dp"
Android: layout_height = "300dp"/>
 
</LinearLayout>
3. Create a new file fragment1.xml under re/layout.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

<ListView
Android: id = "@ id/android: list"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: layout_weight = "1"
Android: drawselectid Top = "false"/>

</LinearLayout>
4. Create a class Fragment1.java under the package path.
[Java]
Public class Fragment1 extends ListFragment {
String [] presidents = {
"Dwight D. Eisenhower ",
"John F. Kennedy ",
"Lyw. B. Johnson ",
"Richard Nixon ",
"Gerald Ford ",
"Jimmy Carter ",
"Ronald Reagan ",
"George h.w. Bush ",
"Bill Clinton ",
"George W. Bush ",
"Barack Obama"
};
 
@ Override
Public View onCreateView (LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState ){
Return inflater. inflate (R. layout. fragment1, container, false );
}
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetListAdapter (new ArrayAdapter <String> (getActivity (),
Android. R. layout. simple_list_item_1, presidents ));
}

Public void onListItemClick (ListView parent, View v,
Int position, long id)
{
Toast. makeText (getActivity (),
"You have selected" + presidents [position],
Toast. LENGTH_SHORT). show ();
}
 
}
5. Press F11 to debug the simulator. Two Information lists are displayed.


6. Click a row to bring up a message.

 

Because the android: layout_height attribute of ListFragment is set in main. xml respectively, the heights of the two lists are different.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.