Android之listfragment的使用例子
1、fragment簡介
我對fragment的理解是基于activity的,對于大多數(shù)的基本開始發(fā)時,我們最先遇到的就是用activity來開發(fā)。
簡單的例子,新建一個最基本的Android空白界面,我們得到的是一個可以顯示一個空白界面的app。一個activity對應著一個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的對應關(guān)系需要自己來定,adapter就是為了把對應的數(shù)據(jù)綁到對應的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ù)對應著。由上一個參數(shù)的數(shù)據(jù)的名稱作為指導,將每一行的數(shù)據(jù)可以對應到相應的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>
最后祝大家新年快樂,雞年大吉吧!??!
相關(guān)文章
Android實現(xiàn)類似execel的表格 能回顯并能修改表格內(nèi)容的方法
今天小編就為大家分享一篇Android實現(xiàn)類似execel的表格 能回顯并能修改表格內(nèi)容的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Android中Handler實現(xiàn)倒計時的兩種方式
本篇文章主要介紹了Android中Handler實現(xiàn)倒計時的兩種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07Android無障礙監(jiān)聽通知的實戰(zhàn)過程
開發(fā)微動手勢的時候,做了一個通知觸發(fā)的功能,就是在收到某個預設(shè)的通知的時候,自動觸發(fā)某個動作,因此需要監(jiān)聽通知消息,這篇文章主要給大家介紹了關(guān)于Android無障礙監(jiān)聽通知的相關(guān)資料,需要的朋友可以參考下2022-07-07Android中AlertDialog 點擊按鈕后不關(guān)閉對話框的功能
本篇文章主要介紹了Android中AlertDialog 點擊按鈕后不關(guān)閉對話框的功能,非常具有實用價值,需要的朋友可以參考下2017-04-04Android斷點續(xù)傳下載器JarvisDownloader的示例
本篇文章主要介紹了Android斷點續(xù)傳下載器JarvisDownloader的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05