【转】FragmentTest学习笔记1

原文网址:http://blog.csdn.net/hishentan/article/details/20734489

源码部分:

BookContent.java

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package com.example.bookdetailfragment;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.content.Context;  
  9.   
  10. public class BookContent {  
  11.       
  12.     //inner class   
  13.     public static class Book{  
  14.         public Integer id;  
  15.         public String title;  
  16.         public String desc;  
  17.         public Book(Integer id,String title,String desc){  
  18.             this.id = id;  
  19.             this.title = title;  
  20.             this.desc = desc;  
  21.         }  
  22.           
  23.         @Override  
  24.         public String toString(){  
  25.             return this.title;  
  26.         }  
  27.     }  
  28.       
  29.     //recorde included book object  
  30.     public static List<Book> ITEMS = new ArrayList<Book>();  
  31.     public static Map<Integer,Book> ITEM_MAP = new HashMap<Integer,Book>();  
  32.       
  33.     static{  
  34.         addItem(new Book(1,"Java","Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE, JavaEE, JavaME)的总称。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。"));  
  35.         addItem(new Book(2,"Math","数学是源自于人类早期的生产活动,早期古希腊、古巴比伦、古埃及、古印度及中国古代都对数学有所研究。数学是研究数量、结构、变化以及空间模型等概念的一门学科。透过抽象化和逻辑推理的运用,由计数、计算、量度和对物体形状及运动的观察中产生。数学的基本要素是:逻辑和直观、分析和推理、共性和个性。"));  
  36.         addItem(new Book(3,"English","英语(English )属于印欧语系中下的西日耳曼语支,由古代从欧洲大陆移民大不列颠岛的盎格鲁、撒克逊和朱特部落的日耳曼人所说的语言演变而来,并通过英国的殖民活动传播到世界各地。由于在历史上曾和多种民族语言接触,它的词汇从一元变为多元,语法从“多屈折”变为“少屈折”,语音也发生了规律性的变化。"));  
  37.           
  38.     }  
  39.       
  40.     //add book object to list and map  respectively  
  41.     public static void addItem(Book book){  
  42.         ITEMS.add(book);  
  43.         ITEM_MAP.put(book.id,book);  
  44.     }  
  45. }  

BookDetailFragment.java
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package com.example.bookdetailfragment;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6.   
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.TextView;  
  10.   
  11. public class BookDetailFragment extends Fragment {  
  12.     public static final String ITEM_ID = "item_id";  
  13.     BookContent.Book book;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         if(getArguments().containsKey(ITEM_ID)){  
  18.             //--如果启动该Fragment时包含了ITEM_ID参数  
  19.             book = BookContent.ITEM_MAP.get(getArguments().getInt(ITEM_ID));          
  20.         }  
  21.     }  
  22.     @Override  
  23.     public View  onCreateView(LayoutInflater inflater, ViewGroup container,  
  24.             Bundle savedInstanceState){  
  25.         //加载布局文件  
  26.         View rootView = inflater.inflate(R.layout.activity_book_detail_fragment,  
  27.                 container, false);  
  28.         if(book!=null){  
  29.             //让id为book_title book_desc的文本框分别显示book.title  book_desc  
  30.             ((TextView)rootView.findViewById(R.id.book_title)).setText(book.title);  
  31.             ((TextView)rootView.findViewById(R.id.book_desc)).setText(book.desc);  
  32.         }  
  33.         return rootView;  
  34.     }  
  35.     //   
  36. }  

BookListFragment.java
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package com.example.bookdetailfragment;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ListFragment;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.ListView;  
  9.   
  10. //extends ListFragMent  
  11. public class BookListFragment extends ListFragment {  
  12.     //定义一个回调接口  该Fragment对象所在Activity需要实现该接口  
  13.     public interface Callbacks{  
  14.         public void onItemSelected(Integer id);  
  15.     }  
  16.     private Callbacks mCallbacks;  
  17.        
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState){  
  20.         super.onCreate(savedInstanceState);  
  21.         //为该ListFragment设置Adapter  
  22.           
  23.         ArrayAdapter<BookContent.Book> adapter = new ArrayAdapter<BookContent.Book>(  
  24.                 getActivity(),android.R.layout.simple_list_item_activated_1,  
  25.                 android.R.id.text1,BookContent.ITEMS);//左边的Fragment  此处只显示标题  
  26.         //BookContent.Book类的toString()方法返回的是this.title  
  27.           
  28.         setListAdapter(adapter);  
  29.     }  
  30.       
  31.     @Override  
  32.     //Called when --a fragment is first attached to its activity--.  
  33.     public void onAttach(Activity activity){  
  34.         super.onAttach(activity);  
  35.         //如果该Acitivity没有实现Callbacks接口  
  36.         if(!(activity instanceof Callbacks)){  
  37.             try {  
  38.                 throw new Exception("BookListFragement所在的Activity" +  
  39.                         "必须实现Callbacks接口");  
  40.             } catch (Exception e) {  
  41.                 // TODO Auto-generated catch block  
  42.                 e.printStackTrace();  
  43.             }  
  44.         }  
  45.         //把该Activity当成Callbacks对象  
  46.         mCallbacks = (Callbacks) activity;  
  47.     }  
  48.       
  49.     @Override  
  50.     //Called when the fragment is no longer attached to its activity.  
  51.     public void onDetach(){  
  52.         super.onDetach();  
  53.         mCallbacks = null;  
  54.     }  
  55.       
  56.     @Override  
  57.     //当用户单击某列表项时回调该方法  
  58.     public void onListItemClick(ListView l, View v, int position, long id){  
  59.         super.onListItemClick(l, v, position, id);  
  60.         //激发mCallbacks的onItemSelected方法  
  61.         mCallbacks.onItemSelected(BookContent.ITEMS.get(position).id);  
  62.     }  
  63.       
  64.     //设置选择模式***  where to call  ??  
  65.     /* 
  66.     public void setActivateOnItemClick(boolean activateOnItemClick){ 
  67.         getListView().setChoiceMode(activateOnItemClick?ListView.CHOICE_MODE_SINGLE: 
  68.             ListView.CHOICE_MODE_NONE); 
  69.          
  70.     }*/  
  71. }  

SelectBookActivity.java
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package com.example.bookdetailfragment;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. //  
  8. public class SelectBookActivity extends Activity implements BookListFragment.Callbacks {  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState){  
  11.   
  12.         super.onCreate(savedInstanceState);  
  13.         //add book_twopane layout with fragment label  
  14.         setContentView(R.layout.activity_book_twopane);  
  15.         //  
  16.     }  
  17.       
  18.     //implement the method from interface BookListFragment.Callbacks  
  19.     public void onItemSelected(Integer id){  
  20.         //deliver parameters to BookListFragment  
  21.         Bundle arguments = new Bundle();  
  22.         arguments.putInt(BookDetailFragment.ITEM_ID, id);  
  23.         //Create BookDetailFragment  
  24.         BookDetailFragment bookdetailfragment = new  BookDetailFragment();  
  25.         bookdetailfragment.setArguments(arguments);//<---向Fragment传入参数  
  26.         //cget fragmentmanager  
  27.         FragmentManager fragmentManager = getFragmentManager();  
  28.         //begin transaction  
  29.         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();  
  30.         fragmentTransaction.replace(R.id.book_detail_container, bookdetailfragment);  
  31.         //commit transaction  
  32.         fragmentTransaction.commit();  
  33.     }  
  34.       
  35.       
  36.       
  37. }  

activity_book_detail_fragment.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".BookDetailFragment" >  
  10.   
  11.    <LinearLayout  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="fill_parent"  
  14.         android:orientation="vertical"  
  15.         >  
  16.         <TextView  
  17.                
  18.              android:id="@+id/book_title"  
  19.              android:layout_width="fill_parent"  
  20.              android:layout_height="wrap_content"  
  21.              android:padding="16dp"  
  22.              android:textSize="20sp"  
  23.              android:background="#0f0"  
  24.                
  25.             />  
  26.         <TextView  
  27.                
  28.              android:id="@+id/book_desc"  
  29.              android:layout_width="fill_parent"  
  30.              android:layout_height="wrap_content"  
  31.              android:padding="16dp"  
  32.              android:textSize="15sp"  
  33.             />  
  34.           
  35.     </LinearLayout>  
  36.   
  37. </RelativeLayout>  

activity_book_twopane.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 水平排列 使用中等分隔条 -->  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="horizontal"   
  7.     android:showDividers="middle"  
  8.     android:baselineAligned="false"  
  9.     >  
  10.       
  11.     <!-- 添加一个Fragment -->  
  12.     <fragment  
  13.         android:name="com.example.bookdetailfragment.BookListFragment"  
  14.         android:id="@+id/book_list"  
  15.         android:layout_width="0dp"  
  16.         android:layout_height="fill_parent"  
  17.         android:layout_weight="1"  
  18.         />  
  19.     <!-- 添加一个FrameLayout容器 -->  
  20.     <FrameLayout  
  21.         android:id="@+id/book_detail_container"  
  22.         android:layout_width="0dp"  
  23.         android:layout_height="fill_parent"  
  24.         android:layout_weight="3"  
  25.         >  
  26.           
  27.     </FrameLayout>  
  28. </LinearLayout>  

AndroidManifest.xml

 android:name="com.example.bookdetailfragment.SelectBookActivity"

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.bookdetailfragment"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="11"  
  9.         android:targetSdkVersion="18" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.bookdetailfragment.SelectBookActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </manifest>  

运行结果:

 
原文地址:https://www.cnblogs.com/wi100sh/p/4292720.html