亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android之listfragment的使用例子

 更新時(shí)間:2017年01月27日 10:05:14   作者:yuboona  
這篇文章主要介紹了Android之listfragment的使用例子,簡(jiǎn)單的介紹了fragment,還有一個(gè)ListFragment實(shí)例,有興趣的可以了解一下。

1、fragment簡(jiǎn)介

我對(duì)fragment的理解是基于activity的,對(duì)于大多數(shù)的基本開(kāi)始發(fā)時(shí),我們最先遇到的就是用activity來(lái)開(kāi)發(fā)。

簡(jiǎn)單的例子,新建一個(gè)最基本的Android空白界面,我們得到的是一個(gè)可以顯示一個(gè)空白界面的app。一個(gè)activity對(duì)應(yīng)著一個(gè)layout。

但是fragment則是基于activity,突破了已經(jīng)固定好的layout的限制,在原有的layout中,把布局元素作為容器,動(dòng)態(tài)容納新的layout。

這樣就等于在一個(gè)activity中可以擁有多個(gè)界面。

2、ListFragment實(shí)例講解

最終效果

最終效果如上圖所示

2.1、首先我們先談一下,準(zhǔn)備工作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>

這里的線性布局,包含了三個(gè)部分(1)layout_title(2)fragment_container(3)layout_bottom

其中(2)fragment_container就是用來(lái)動(dòng)態(tài)加載listfragment的地方。

2.2、第二點(diǎn)我們看一下被動(dòng)態(tài)加載到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可以看出,為了動(dòng)態(tài)加載一個(gè)listfragment,我們?yōu)槠渚帉懥艘粋€(gè)擁有ListView組件的xml,這一點(diǎn)是必須的。

2.3、第三點(diǎn),我們看一看到底是如何在activity中用什么方式動(dòng)態(tài)的加載listfragment

我們看一下MainActivity.Java的關(guān)鍵部分

  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();
//*********************************************

我特殊標(biāo)記的地方就是用來(lái)動(dòng)態(tài)加載的代碼。

為了加載fragment,我們要編寫一個(gè)fragment類,這個(gè)類的對(duì)象我們可以看到在add函數(shù)中被用到,也是在這個(gè)地方,將fragmen加載。

使用fragmentManager的add函數(shù)來(lái)加載,它有三個(gè)參數(shù)(1)fragment被加載的位置(R.id.fragment_container)(2)一個(gè)fragment對(duì)象,這個(gè)對(duì)象的編寫也很重要,等會(huì)講到。(3)為動(dòng)態(tài)加載的fragment起一個(gè)名字,這一項(xiàng),隨便起。

2.4、第四步,fragment對(duì)象的類的編寫

上文中第二步的fragment_order.xml就是被這個(gè)類來(lái)使用,實(shí)例化,正是因?yàn)橛辛诉@個(gè)類才能夠?qū)ragment實(shí)例化,于是才能被動(dòng)態(tài)加載。

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 };
//用來(lái)初始化listfragmnet每一條項(xiàng)目的資源

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

    return inflater.inflate(R.layout.fragment_order, container, false);
//這里用inflate函數(shù),在初始化創(chuàng)建view時(shí)返回fragment_order.xml實(shí)例
  }


//下面的部分則是用于將每一條項(xiàng)目的資源放入到listview的每一個(gè)條目中去
  @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的用法,因?yàn)檫@很重要,如果數(shù)據(jù)不能和layout綁定,那么就會(huì)不能運(yùn)行成功。

使用simpleAdapter是很重要的。為什么要使用simpleAdapter的原因很簡(jiǎn)單,綁定數(shù)據(jù)和layout的工作不可能完全由程序自動(dòng)完成,數(shù)據(jù)和layout的對(duì)應(yīng)關(guān)系需要自己來(lái)定,adapter就是為了把對(duì)應(yīng)的數(shù)據(jù)綁到對(duì)應(yīng)的layout上

simpleAdapter算是Adapter中比較簡(jiǎn)單好用的一個(gè)

listitem中用了Map<string,object>的數(shù)據(jù)格式,代表了每一行內(nèi)容其中的數(shù)據(jù)。

list則是一連串的Map<string,object>

我們看simpleAdapter的參數(shù),總共5個(gè):(1)得到當(dāng)前的activity(2)已經(jīng)將數(shù)據(jù)存好了的list(3)又是一個(gè)xml,這個(gè)xml是用來(lái)作為listview的一條項(xiàng)目的layout,這樣一個(gè)項(xiàng)目的外觀才會(huì)被確定(4)這個(gè)數(shù)組指明了在Map<string,object>中,數(shù)據(jù)的名稱代號(hào)是什么,這樣adapter在取list的每個(gè)條目的數(shù)據(jù)時(shí),才有參照。這個(gè)參數(shù)同時(shí)和下一個(gè)參數(shù)有很大關(guān)系(5)這個(gè)參數(shù)是layout中的id,和上一個(gè)參數(shù)對(duì)應(yīng)著。由上一個(gè)參數(shù)的數(shù)據(jù)的名稱作為指導(dǎo),將每一行的數(shù)據(jù)可以對(duì)應(yīng)到相應(yīng)的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>

最后祝大家新年快樂(lè),雞年大吉吧?。?!

相關(guān)文章

最新評(píng)論