Android之listfragment的使用例子

2022-10-19,

这篇文章主要介绍了Android之listfragment的使用例子,简单的介绍了fragment,还有一个ListFragment实例,有兴趣的可以了解一下。

1、fragment简介

我对fragment的理解是基于activity的,对于大多数的基本开始发时,我们最先遇到的就是用activity来开发。

简单的例子,新建一个最基本的Android空白界面,我们得到的是一个可以显示一个空白界面的app。一个activity对应着一个layout。

但是fragment则是基于activity,突破了已经固定好的layout的限制,在原有的layout中,把布局元素作为容器,动态容纳新的layout。

这样就等于在一个activity中可以拥有多个界面。

2、ListFragment实例讲解

最终效果

最终效果如上图所示

2.1、首先我们先谈一下,准备工作activity_main的布局:activity_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:orientation="vertical">

  <include android:id="@+id/layout_bar" layout="@layout/layout_title"/>

  <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >
    </FrameLayout>


  <include layout="@layout/layout_bottom"/>

</LinearLayout>

这里的线性布局,包含了三个部分(1)layout_title(2)fragment_container(3)layout_bottom

其中(2)fragment_container就是用来动态加载listfragment的地方。

2.2、第二点我们看一下被动态加载到fragment_container中的布局:文件fragment_order.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">


  <ListView
    android:id="@+id/android:list"
    android:scrollbars="none"
    android:dividerHeight="0dp"
    android:divider="#00000000"
    android:listSelector="#00000000"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

分析以上的xml可以看出,为了动态加载一个listfragment,我们为其编写了一个拥有ListView组件的xml,这一点是必须的。

2.3、第三点,我们看一看到底是如何在activity中用什么方式动态的加载listfragment

我们看一下MainActivity.Java的关键部分

  private FragmentManager manager;
@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
//*********************************************
    manager = getFragmentManager();
manager.beginTransaction().add(R.id.fragment_container, homefragment, "article").commit();
//*********************************************

我特殊标记的地方就是用来动态加载的代码。

为了加载fragment,我们要编写一个fragment类,这个类的对象我们可以看到在add函数中被用到,也是在这个地方,将fragmen加载。

使用fragmentManager的add函数来加载,它有三个参数(1)fragment被加载的位置(R.id.fragment_container)(2)一个fragment对象,这个对象的编写也很重要,等会讲到。(3)为动态加载的fragment起一个名字,这一项,随便起。

2.4、第四步,fragment对象的类的编写

上文中第二步的fragment_order.xml就是被这个类来使用,实例化,正是因为有了这个类才能够将fragment实例化,于是才能被动态加载。

public class Fragment_order extends ListFragment
{
  private MainActivity parentActivity;
  private String[] values = new String[] { "快餐店", "烤食店", "烧鱼店", "甜食店", "蔬菜店",
      "融合菜店","面条店" };
  private int[] images = new int[] { R.drawable.fastfood,
      R.drawable.roastfood, R.drawable.fishfood,
      R.drawable.sweetfood, R.drawable.vegetables,
      R.drawable.multifood,R.drawable.noodles };
//用来初始化listfragmnet每一条项目的资源

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_order, container, false);
//这里用inflate函数,在初始化创建view时返回fragment_order.xml实例
  }


//下面的部分则是用于将每一条项目的资源放入到listview的每一个条目中去
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < values.length; i++) {
      Map<String, Object> listItem = new HashMap<String, Object>();
      listItem.put("values", values[i]);
      listItem.put("images", images[i]);
      list.add(listItem);
    }

    SimpleAdapter adapter = new SimpleAdapter(getActivity(),list,
        R.layout.list_item, new String[] { "values", "images" },
        new int[] { R.id.storeName, R.id.storePic });
    setListAdapter(adapter);

  }

主要想讲一讲simpleAdapter的用法,因为这很重要,如果数据不能和layout绑定,那么就会不能运行成功。

使用simpleAdapter是很重要的。为什么要使用simpleAdapter的原因很简单,绑定数据和layout的工作不可能完全由程序自动完成,数据和layout的对应关系需要自己来定,adapter就是为了把对应的数据绑到对应的layout上

simpleAdapter算是Adapter中比较简单好用的一个

listitem中用了Map<string,object>的数据格式,代表了每一行内容其中的数据。

list则是一连串的Map<string,object>

我们看simpleAdapter的参数,总共5个:(1)得到当前的activity(2)已经将数据存好了的list(3)又是一个xml,这个xml是用来作为listview的一条项目的layout,这样一个项目的外观才会被确定(4)这个数组指明了在Map<string,object>中,数据的名称代号是什么,这样adapter在取list的每个条目的数据时,才有参照。这个参数同时和下一个参数有很大关系(5)这个参数是layout中的id,和上一个参数对应着。由上一个参数的数据的名称作为指导,将每一行的数据可以对应到相应的ID。

2.5、最后把listview的每一行条目的layout代码写一下:list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  xmlns:app="http://schemas.android.com/apk/res-auto">

  <LinearLayout
    android:id="@+id/contactitem_layout"
    style="@style/MMListItem"
    android:layout_height="65.0dip"

    android:paddingLeft="12dip"
    android:background="@drawable/border"
    android:padding="2dp"
    android:weightSum="1">
      <RelativeLayout

      android:id="@+id/avatar_container"
      android:layout_width="match_parent"
      android:layout_marginTop="4dp"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
  >
      <ImageView
        android:id="@+id/storePic"
        android:layout_width="50.0dip"
        android:layout_height="50.0dip"
        android:src="@drawable/head" />
      <TextView
        android:id="@+id/storeName"
        style="@style/MMFontTitleInList"
        android:layout_toRightOf="@+id/storePic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:text="No data" />
      </RelativeLayout>

  </LinearLayout>

</LinearLayout>

最后祝大家新年快乐,鸡年大吉吧!!!

您可能感兴趣的文章:

  • Android App中使用ListFragment的实例教程

《Android之listfragment的使用例子.doc》

下载本文的Word格式文档,以方便收藏与打印。