1 ListFragement介绍
ListFragment继承于Fragment。因此它具有Fragment的特性,能够作为activity中的一部分,目的也是为了使页面设计更加灵活。
相比Fragment,ListFragment的内容是以列表(list)的形式显示的。
1.1 ListFragment布局
ListFragment的布局默认包含一个list view。因此,在ListFragment对应的布局文件中,必须指定一个 android:id 为 “@android:id/list” 的ListView控件! 若用户向修改list view的,可以在onCreateView(LayoutInflater, ViewGroup, Bundle)中进行修改。当然,用户也可以在ListFragment的布局中包含其它的控件。
下面是ListFragment对应的一个layout示例:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="8dp" android:paddingRight="8dp"> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" android:drawSelectorOnTop="false"/> <TextView android:id="@id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" android:text="No data"/> </LinearLayout>
ListView中每一行的显示内容,是通过设置适配器来实现的。我们既可以自定义,也可以采用系统默认的layout。后面的应用实例中,会分别列举2种情况下的显示。
1.2 绑定数据
ListFragment绑定ListView的数据,必须通过ListFragment.setListAdapter()接口来绑定数据,而不是使用ListView.setAdapter() 或其它方法!
2 ListFragment应用实例
应用实例说明:建立一个activity,包括2个ListFragment。第1个ListFragment采用中ListView每一行的内容通过android自带的android.R.layout.simple_list_item_1布局来显示;第2个ListFragment每一行的内容通过自定义的layout文件来显示,每一行显示两个文本。
activity对应的layout文件代码:
<LinearLayout xmlns: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.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>
说明:
(01) 该layout布局包含两个fragment。
activity的代码:
package com.skywang.app; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.view.Menu; public class ListFragmentTest extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_fragment_test); } }
说明:
(01) 在 onCreateView()中,调用list_fragment_impl作为该ListFragment的布局文件。
(02) 在 onCreate()中,通过setListAdapter() 设置android.R.layout.simple_list_item_1为ListView每一行的布局文件,设置cities为其中数据的每一项内容。
ListFragmentImpl.java的代码:
package com.skywang.app; import android.app.ListFragment; import android.widget.ListView; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.util.Log; import android.widget.Toast; import android.widget.SimpleAdapter; import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; public class ListFragmentImpl extends ListFragment{ private static final String TAG = "ListFragmentImpl"; private ListView selfList; String[] cities = { "Shenzhen", "Beijing", "Shanghai", "Guangzhou", "Wuhan", "Tianjing", "Changsha", "Xi'an", "Chongqing", "Guilin", }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView"); return inflater.inflate(R.layout.list_fragment_impl, container, false); } @Override public void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate"); super.onCreate(savedInstanceState); // 设置ListFragment默认的ListView,即@id/android:list this.setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, cities)); } public void onListItemClick(ListView parent, View v, int position, long id) { Log.d(TAG, "onListItemClick"); Toast.makeText(getActivity(), "You have selected " + cities[position], Toast.LENGTH_SHORT).show(); } }
list_fragment_impl.xml的内容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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>
ListFragmentSelf.java的代码:
package com.skywang.app; import android.app.ListFragment; import android.widget.ListView; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.util.Log; import android.widget.Toast; import android.widget.SimpleAdapter; import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; public class ListFragmentSelf extends ListFragment{ private static final String TAG = "ListFragmentImpl"; private ListView selfList; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView"); return inflater.inflate(R.layout.list_fragment_self, container, false); } @Override public void onCreate(Bundle savedInstanceState) { final String[] from = new String[] {"title", "info"}; final int[] to = new int[] {R.id.text1, R.id.text2}; Log.d(TAG, "onCreate"); super.onCreate(savedInstanceState); // 建立SimpleAdapter,将from和to对应起来 SimpleAdapter adapter = new SimpleAdapter( this.getActivity(), getSimpleData(), R.layout.two_textview, from, to); this.setListAdapter(adapter); } public void onListItemClick(ListView parent, View v, int position, long id) { Log.d(TAG, "onListItemClick"); Toast.makeText(getActivity(), "You have selected " + position, Toast.LENGTH_SHORT).show(); } private List<Map<String, Object>> getSimpleData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "Ferris wheel"); map.put("info", "Suzhou Ferris wheel"); list.add(map); map = new HashMap<String, Object>(); map.put("title", "Flower"); map.put("info", "Roser"); list.add(map); map = new HashMap<String, Object>(); map.put("title", "Disk"); map.put("info", "Song Disk"); list.add(map); return list; } }
说明:
(01) 在 onCreateView()中,调用list_fragment_self作为该ListFragment的布局文件。
(02) 在 onCreate()中,通过setListAdapter() 设置R.layout.two_textview为ListView每一行的布局文件。
list_fragment_self.xml的内容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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>
two_textview.xml的内容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.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>
点击下载:源代码
效果图:
相关推荐
在Android开发中,Fragment是一个非常重要的组件,它允许我们在一个Activity中分割用户界面,实现更加灵活的布局管理。`ListFragment`是Fragment的一个子类,专门用于展示列表数据,简化了在Fragment中集成ListView...
在Android应用程序开发中,ListFragment是Android SDK提供的一种特殊类型的Fragment,它的主要功能是展示一个列表视图(ListView)。ListFragment简化了在Activity中集成列表展示的流程,因为它内置了对ListView的...
在Android开发中,`ListFragment`是一个非常实用的组件,它是`Fragment`的子类,专为在界面上显示一个可滚动的列表而设计。本文将深入探讨如何自定义`ListFragment`以满足特定需求,并结合给定的标签“源码”和...
在Android开发中,`ListFragment`是Android Support Library或AndroidX库中的一种组件,它用于在界面中展示列表数据。`ListFragment`是`Fragment`的子类,它集成了`ListView`的功能,使得开发者可以更方便地在应用中...
在Android应用开发中,Fragment是UI组件的重要组成部分,它允许我们构建模块化的界面,尤其在大屏幕设备上,如平板电脑。本实例将探讨如何在Fragment中使用ListFragment,实现ListView的下拉刷新、上拉加载以及横向...
简单的例子,新建一个最基本的Android空白界面,我们得到的是一个可以显示一个空白界面的app。一个activity对应着一个layout。 但是fragment则是基于activity,突破了已经固定好的layout的限制,在原有的layout中...
在Android应用开发中,Fragment是Android SDK提供的一种用于在Activity中实现界面模块化的重要组件。Fragment可以看作是Activity的一部分,允许我们构建可重用、可组合的UI块。本篇将深入探讨`DialogFragment`和`...
**ListFragment:Android开发中的列表视图组件** 在Android应用开发中,`ListFragment`是Android SDK提供的一种特殊类型的Fragment,专门用于展示列表数据。它继承自`Fragment`类,并集成了`ListView`的功能,使得...
在Android开发中,`ListFragment`是Android Support Library或AndroidX库的一部分,它是一个轻量级的组件,用于在活动中展示列表数据。`ListFragment`是`Fragment`的一个子类,专为显示`ListView`而设计,简化了在...
上一篇文章介绍了ListFragment,其中的ListView并没有自定义适配器,实际上在实际开发中常会用到自定义适配器,是实现更复杂的列表数据展示。所以这篇文章增加了自定义适配器,来进行ListView数据的展示
在Android应用开发中,`ListFragment` 是一个非常实用的组件,它继承自 `Fragment` 类,主要用于在界面上展示列表数据。在这个场景中,我们看到的项目是关于使用 `ListFragment` 来显示莎士比亚的作品。让我们深入...
在新项目中,应该使用AndroidX版本的`ListFragment`,它在`androidx.fragment.app`包下,命名保持不变。 **6. 拓展使用** `ListFragment`还可以与其他组件结合使用,例如`ViewPager`,实现更复杂的布局。同时,也...
在Android开发中,我们经常需要实现一种布局设计,即在一个界面上,左侧显示标题列表,右侧展示与之对应的内容。这种设计模式常用于新闻应用、电子书应用等,让用户能够方便地浏览和查看详细信息。标题"左边...
在Android开发中,ListView是一种常用的组件,用于展示大量的列表数据。"长按listview的条目,点击删除"这一主题涉及到ListView的触摸事件处理和数据操作。以下将详细讲解相关知识点。 1. **ListView长按事件处理**...
本教程将详细介绍如何基于ListFragment框架来实现这一功能,这对于初学者来说是一个很好的实践项目,能够帮助巩固和提升Android开发的基本技能。 首先,我们需要理解ListFragment的概念。ListFragment是Android ...
ListFragment是Android SDK中的一个类,用于在Fragment中展示ListView。集成下拉刷新功能后,用户在列表顶部下拉即可刷新列表内容,非常适合用来展示动态更新的数据,如新闻列表或好友动态。 六、LauncherActivity ...
在Android开发中,Fragment是构建用户界面的一个关键组件,它允许开发者在单个Activity中构建模块化的、可重用的UI部分。Fragment的设计理念是为了更好地支持大屏幕设备,如平板电脑,但也同样适用于手机。在本文中...
在Android开发中,Fragment是一个非常重要的组件,尤其在构建复杂且动态的用户界面时。Xamarin.Android是一个强大的工具,它允许开发者使用C#语言来构建原生的Android应用。在这个"Xamarin Android Fragment例子"中...