国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android之listfragment的使用例子

2019-12-12 03:51:33
字體:
供稿:網(wǎng)友

1、fragment簡介

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

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

但是fragment則是基于activity,突破了已經(jīng)固定好的layout的限制,在原有的layout中,把布局元素作為容器,動態(tài)容納新的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就是用來動態(tài)加載listfragment的地方。

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

2.3、第三點,我們看一看到底是如何在activity中用什么方式動態(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();//*********************************************

我特殊標記的地方就是用來動態(tài)加載的代碼。

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

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

2.4、第四步,fragment對象的類的編寫

上文中第二步的fragment_order.xml就是被這個類來使用,實例化,正是因為有了這個類才能夠?qū)ragment實例化,于是才能被動態(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 };//用來初始化listfragmnet每一條項目的資源  @Override  public View onCreateView(LayoutInflater inflater, ViewGroup container,               Bundle savedInstanceState) {    return inflater.inflate(R.layout.fragment_order, container, false);//這里用inflate函數(shù),在初始化創(chuàng)建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的用法,因為這很重要,如果數(shù)據(jù)不能和layout綁定,那么就會不能運行成功。

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

simpleAdapter算是Adapter中比較簡單好用的一個

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

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

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

最后祝大家新年快樂,雞年大吉吧!!!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 麻阳| 湖南省| 河曲县| 彭州市| 右玉县| 榆林市| 南昌市| 榆树市| 开原市| 富顺县| 西充县| 岢岚县| 台北县| 朝阳市| 西畴县| 湖北省| 广德县| 乌兰察布市| 开化县| 广河县| 利辛县| 吴忠市| 长泰县| 尚义县| 灵丘县| 乐业县| 蓬安县| 罗山县| 南漳县| 自治县| 嘉义县| 陇西县| 阳城县| 崇信县| 曲靖市| 绍兴县| 隆回县| 仁怀市| 涟水县| 夹江县| 德江县|