Listframgent is a fragment containing ListView that displays a range of information through a data source (array or cursor). Listfragment is very useful, like RSS, may display a list on the left, the right to display the selected list of the corresponding content.
You can create a Listfragment object by inheriting listfragment. The following shows how to use Listfragment.
1. Create a project: Listfragmentexample.
2. Code in the Main.xml.
<?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:o" rientation= "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. Under Re/layout, create a new file: Fragment1.xml.
?
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:drawselectorontop= false "/>
</LinearLayout>
4, under the package path to create a new class: Fragment1.java.
public class Fragment1 extends Listfragment {string[] presidents = {"Dwight D. Eisenhower",
"John F. Kennedy", "Lyndon 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 (savedins
Tancestate); 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 (getactiv
ity (), "You have selected" + presidents[position], toast.length_short). Show (); }
}