Android App中使用ListFragment的实例教程

2019-12-10 18:02:20丽君

说明: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的内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <TextView android:id="@+id/text1"
  android:textSize="12sp"
  android:textStyle="bold"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

 <TextView android:id="@+id/text2"
  android:textSize="24sp"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

</LinearLayout>

ListFragment实例
应用实例说明:建立一个activity,包括2个ListFragment。第1个ListFragment采用中ListView每一行的内容通过android自带的android.R.layout.simple_list_item_1布局来显示;第2个ListFragment每一行的内容通过自定义的layout文件来显示,每一行显示两个文本。

activity对应的layout文件代码:

<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
 xmlns:tools="http://www.easck.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >

 <fragment 
  android:name="com.skywang.app.ListFragmentImpl"
  android:id="@+id/fragment1" 
  android:layout_weight="1"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

 <fragment 
  android:name="com.skywang.app.ListFragmentSelf"
  android:id="@+id/fragment2" 
  android:layout_weight="1"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

</LinearLayout>