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.