Android App中使用ListFragment的实例教程
ListFragment继承于Fragment。因此它具有Fragment的特性,能够作为activity中的一部分,目的也是为了使页面设计更加灵活。
相比Fragment,ListFragment的内容是以列表(list)的形式显示的。ListFragment的布局默认包含一个ListView。因此,在ListFragment对应的布局文件中,必须指定一个android:id为“@android:id/list”的ListView控件!
ListFragment基础使用
下面介绍在Activity中显示ListFragment的步骤。
1.Activity对应的代码
publicclassFragmentTestextendsActivity{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
2.Activity对应的布局
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <fragment android:name="com.skw.fragmenttest.MyListFragment" android:id="@+id/myfragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
说明:该Activity的布局中只包行了一个Fragment。下面看看MyListFragment的内容。
3.MyListFragment的内容
publicclassMyListFragmentextendsListFragment{
privatestaticfinalStringTAG="##MyListFragment##";
privateListViewselfList;
String[]cities={
"Shenzhen",
"Beijing",
"Shanghai",
"Guangzhou",
"Wuhan",
"Tianjing",
"Changsha",
"Xi'an",
"Chongqing",
"Guilin",
};
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
BundlesavedInstanceState){
Log.d(TAG,"onCreateView");
returninflater.inflate(R.layout.list_fragment,container,false);
}
@Override
publicvoidonCreate(BundlesavedInstanceState){
Log.d(TAG,"onCreate");
super.onCreate(savedInstanceState);
//设置ListFragment默认的ListView,即@id/android:list
this.setListAdapter(newArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,cities));
}
publicvoidonListItemClick(ListViewparent,Viewv,
intposition,longid){
Log.d(TAG,"onListItemClick");
Toast.makeText(getActivity(),"Youhaveselected"+cities[position],
Toast.LENGTH_SHORT).show();
}
}
说明:MyListFragment是自定义的ListFragment。它使用了list_fragment.xml作为布局,并通过android.R.layout.simple_list_item_1显示ListView中的每一项。
4.list_fragment.xml的内容
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--ListFragment对应的android:id值固定为"@id/android:list"--> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false" /> </LinearLayout>
"Activity的布局以及代码"和前面一样,这里就不再重复说明。
5.MyListFragment的内容
publicclassMyListFragmentextendsListFragment{
privatestaticfinalStringTAG="##MyListFragment##";
privateListViewselfList;
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
BundlesavedInstanceState){
Log.d(TAG,"onCreateView");
returninflater.inflate(R.layout.list_fragment,container,false);
}
@Override
publicvoidonCreate(BundlesavedInstanceState){
finalString[]from=newString[]{"title","info"};
finalint[]to=newint[]{R.id.text1,R.id.text2};
Log.d(TAG,"onCreate");
super.onCreate(savedInstanceState);
//建立SimpleAdapter,将from和to对应起来
SimpleAdapteradapter=newSimpleAdapter(
this.getActivity(),getSimpleData(),
R.layout.item,from,to);
this.setListAdapter(adapter);
}
publicvoidonListItemClick(ListViewparent,Viewv,
intposition,longid){
Log.d(TAG,"onListItemClick");
Toast.makeText(getActivity(),
"Youhaveselected"+position,
Toast.LENGTH_SHORT).show();
}
privateList<Map<String,Object>>getSimpleData(){
List<Map<String,Object>>list=newArrayList<Map<String,Object>>();
Map<String,Object>map=newHashMap<String,Object>();
map.put("title","Ferriswheel");
map.put("info","SuzhouFerriswheel");
list.add(map);
map=newHashMap<String,Object>();
map.put("title","Flower");
map.put("info","Roser");
list.add(map);
map=newHashMap<String,Object>();
map.put("title","Disk");
map.put("info","SongDisk");
list.add(map);
returnlist;
}
}
说明:MyListFragment使用了R.layout.list_fragment作为布局,并且对于ListView中的每一项都使用了R.layout.item作为布局。
6.list_fragment.xml的内容
<!--ListFragment对应的android:id值固定为"@id/android:list"-->
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
/>
7.item.xml的内容
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextViewandroid:id="@+id/text1" android:textSize="12sp" android:textStyle="bold" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextViewandroid:id="@+id/text2" android:textSize="24sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>