【转】FragmentTest学习笔记1
原文网址:http://blog.csdn.net/hishentan/article/details/20734489
源码部分:
BookContent.java
- package com.example.bookdetailfragment;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.content.Context;
- public class BookContent {
- //inner class
- public static class Book{
- public Integer id;
- public String title;
- public String desc;
- public Book(Integer id,String title,String desc){
- this.id = id;
- this.title = title;
- this.desc = desc;
- }
- @Override
- public String toString(){
- return this.title;
- }
- }
- //recorde included book object
- public static List<Book> ITEMS = new ArrayList<Book>();
- public static Map<Integer,Book> ITEM_MAP = new HashMap<Integer,Book>();
- static{
- addItem(new Book(1,"Java","Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE, JavaEE, JavaME)的总称。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。"));
- addItem(new Book(2,"Math","数学是源自于人类早期的生产活动,早期古希腊、古巴比伦、古埃及、古印度及中国古代都对数学有所研究。数学是研究数量、结构、变化以及空间模型等概念的一门学科。透过抽象化和逻辑推理的运用,由计数、计算、量度和对物体形状及运动的观察中产生。数学的基本要素是:逻辑和直观、分析和推理、共性和个性。"));
- addItem(new Book(3,"English","英语(English )属于印欧语系中下的西日耳曼语支,由古代从欧洲大陆移民大不列颠岛的盎格鲁、撒克逊和朱特部落的日耳曼人所说的语言演变而来,并通过英国的殖民活动传播到世界各地。由于在历史上曾和多种民族语言接触,它的词汇从一元变为多元,语法从“多屈折”变为“少屈折”,语音也发生了规律性的变化。"));
- }
- //add book object to list and map respectively
- public static void addItem(Book book){
- ITEMS.add(book);
- ITEM_MAP.put(book.id,book);
- }
- }
BookDetailFragment.java
- package com.example.bookdetailfragment;
- import android.os.Bundle;
- import android.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class BookDetailFragment extends Fragment {
- public static final String ITEM_ID = "item_id";
- BookContent.Book book;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if(getArguments().containsKey(ITEM_ID)){
- //--如果启动该Fragment时包含了ITEM_ID参数
- book = BookContent.ITEM_MAP.get(getArguments().getInt(ITEM_ID));
- }
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState){
- //加载布局文件
- View rootView = inflater.inflate(R.layout.activity_book_detail_fragment,
- container, false);
- if(book!=null){
- //让id为book_title book_desc的文本框分别显示book.title book_desc
- ((TextView)rootView.findViewById(R.id.book_title)).setText(book.title);
- ((TextView)rootView.findViewById(R.id.book_desc)).setText(book.desc);
- }
- return rootView;
- }
- //
- }
BookListFragment.java
- package com.example.bookdetailfragment;
- import android.app.Activity;
- import android.app.ListFragment;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- //extends ListFragMent
- public class BookListFragment extends ListFragment {
- //定义一个回调接口 该Fragment对象所在Activity需要实现该接口
- public interface Callbacks{
- public void onItemSelected(Integer id);
- }
- private Callbacks mCallbacks;
- @Override
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- //为该ListFragment设置Adapter
- ArrayAdapter<BookContent.Book> adapter = new ArrayAdapter<BookContent.Book>(
- getActivity(),android.R.layout.simple_list_item_activated_1,
- android.R.id.text1,BookContent.ITEMS);//左边的Fragment 此处只显示标题
- //BookContent.Book类的toString()方法返回的是this.title
- setListAdapter(adapter);
- }
- @Override
- //Called when --a fragment is first attached to its activity--.
- public void onAttach(Activity activity){
- super.onAttach(activity);
- //如果该Acitivity没有实现Callbacks接口
- if(!(activity instanceof Callbacks)){
- try {
- throw new Exception("BookListFragement所在的Activity" +
- "必须实现Callbacks接口");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //把该Activity当成Callbacks对象
- mCallbacks = (Callbacks) activity;
- }
- @Override
- //Called when the fragment is no longer attached to its activity.
- public void onDetach(){
- super.onDetach();
- mCallbacks = null;
- }
- @Override
- //当用户单击某列表项时回调该方法
- public void onListItemClick(ListView l, View v, int position, long id){
- super.onListItemClick(l, v, position, id);
- //激发mCallbacks的onItemSelected方法
- mCallbacks.onItemSelected(BookContent.ITEMS.get(position).id);
- }
- //设置选择模式*** where to call ??
- /*
- public void setActivateOnItemClick(boolean activateOnItemClick){
- getListView().setChoiceMode(activateOnItemClick?ListView.CHOICE_MODE_SINGLE:
- ListView.CHOICE_MODE_NONE);
- }*/
- }
SelectBookActivity.java
- package com.example.bookdetailfragment;
- import android.app.Activity;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- //
- public class SelectBookActivity extends Activity implements BookListFragment.Callbacks {
- @Override
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- //add book_twopane layout with fragment label
- setContentView(R.layout.activity_book_twopane);
- //
- }
- //implement the method from interface BookListFragment.Callbacks
- public void onItemSelected(Integer id){
- //deliver parameters to BookListFragment
- Bundle arguments = new Bundle();
- arguments.putInt(BookDetailFragment.ITEM_ID, id);
- //Create BookDetailFragment
- BookDetailFragment bookdetailfragment = new BookDetailFragment();
- bookdetailfragment.setArguments(arguments);//<---向Fragment传入参数
- //cget fragmentmanager
- FragmentManager fragmentManager = getFragmentManager();
- //begin transaction
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- fragmentTransaction.replace(R.id.book_detail_container, bookdetailfragment);
- //commit transaction
- fragmentTransaction.commit();
- }
- }
activity_book_detail_fragment.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".BookDetailFragment" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <TextView
- android:id="@+id/book_title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:padding="16dp"
- android:textSize="20sp"
- android:background="#0f0"
- />
- <TextView
- android:id="@+id/book_desc"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:padding="16dp"
- android:textSize="15sp"
- />
- </LinearLayout>
- </RelativeLayout>
activity_book_twopane.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 水平排列 使用中等分隔条 -->
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:showDividers="middle"
- android:baselineAligned="false"
- >
- <!-- 添加一个Fragment -->
- <fragment
- android:name="com.example.bookdetailfragment.BookListFragment"
- android:id="@+id/book_list"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- <!-- 添加一个FrameLayout容器 -->
- <FrameLayout
- android:id="@+id/book_detail_container"
- android:layout_width="0dp"
- android:layout_height="fill_parent"
- android:layout_weight="3"
- >
- </FrameLayout>
- </LinearLayout>
AndroidManifest.xml
android:name="com.example.bookdetailfragment.SelectBookActivity"
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.bookdetailfragment"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="11"
- android:targetSdkVersion="18" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.bookdetailfragment.SelectBookActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
运行结果:
Stay hungry, stay foolish!