天天看點

Fragment與Activity之間的通信

前言

君子博學而日參省乎已,則知明而行無過矣。

要顯示Fragment,必須将Fragment添加到Activity中。添加到Activity中有如下兩種方式。

  • 在布局檔案中使用<fragment../>元素添加Fragment,<fragment../>元素的android:name屬性指定Fragment的實作類。
  • 在java代碼中通過FragmentTransaction對象的add()方法來添加Fragment。

将Fragment添加到Activity之後,Fragment必須與Activity互動資訊,這就需要Fragment能擷取它所在的Activity,Activity也能擷取它所包含的任意的Fragment。

  • Fragment擷取它所在的Activtiy:調用Fragment的getActivity()方法即可傳回它所在的Activity。
  • Activity擷取它包含的Fragment:調用Activity關聯的FragmentManager的findFragmentById(int id)或findFragmentByTag(String tag)方法即可擷取指定的Fragment。

除此之外,Fragment與Activity可能還需要互相傳遞資料,可按如下方式進行。

  • Activity向Fragment傳遞資料:在Activity中建立Bundle資料包,并調用Fragment的setArguments(Bundle bundle)方法即可将Bundle資料包傳給Fragment。
  • Fragment向Activity傳遞資料或Activity需要在Fragment運作中進行實時通信:在Fragment中定義一個内部回調接口,再讓包含該Fragment的Activity實作該回調接口,這樣Fragment即可調用該回調方法将資料傳給Activity。

代碼示例

下面是一個顯示圖書詳情的應用程式,将會在不同的手機螢幕下,顯示出不同的效果,在小屏手機下,點選目前activity的清單項,将會跳轉到另一個Activtiy,在平闆上,清單将會在左邊顯示,内容将會在右邊顯示。

BookContent.java
public class BookContent {

    //定義一個内部類,作為系統的業務對象
    public static class Book
    {
        public Integer id;
        public String title;
        public String desc;
        public Book(Integer id, String title, String desc) {
            super();
            this.id = id;
            this.title = title;
            this.desc = desc;
        }
        @Override
        public String toString() {
            return title;
        }
    }

    //使用List集合記錄系統所包含的Book對象
    public static List<Book> ITEMS = new ArrayList<Book>();
    //使用Map集合記錄系統所包含的Book對象
    public static Map<Integer, Book> ITEM_MAP = new HashMap<Integer, Book>();

    static
    {
        //使用靜态初始化代碼。将Book對象添加到List集合、Map集合中
        addItem(new Book(1,"西遊記","西遊記是中國四大名著之一"));
        addItem(new Book(2,"水浒傳","水浒傳是中國四大名著之一"));
        addItem(new Book(3,"三國演義","三國演義是中國四大名著之一"));
        addItem(new Book(4,"紅樓夢","紅樓夢是中國四大名著之一"));
    }

    private static void addItem(Book book)
    {
        ITEMS.add(book);
        ITEM_MAP.put(book.id, book);
    }
}
           
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:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:divider="?android:attr/dividerHorizontal"
    android:showDividers="middle"
    >
    <!-- 添加一個Fragment -->
    <fragment
        android:name="com.zdf.fragmentdemo1.BookListFragment"
        android:id="@+id/book_list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />
    <!-- 添加一個FrameLayout -->
    <FrameLayout
        android:id="@+id/book_detail_container"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="match_parent"
        />
</LinearLayout>
           
activity_book_list.xml,小螢幕手機清單項的布局
<?xml version="1.0" encoding="utf-8"?>
<!-- 直接使用BookListFragment作為界面元件 -->
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.zdf.fragmentdemo1.BookListFragment"
    android:id="@+id/book_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    />
           
/res/values-large/refs.xml 引用檔案,根據螢幕大小使用不同的布局。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 定義activity_book_list實際引用@layout/activity_book_twopane資源 -->
    <item type="layout" name="activity_book_list">@layout/activity_book_twopane</item>
</resources>
           
activity_book_detail.xml,小螢幕手機的第二個fragment的activity布局。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/book_detail_container1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
           
fragment_book_detail.xml,小螢幕手機嵌入第二個Activity 的Fragment
<?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="vertical" >
    <TextView
        style="?android:attr/textAppearanceLarge"
        android:id="@+id/book_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        />
    <TextView
        style="?android:attr/textAppearanceMedium"
        android:id="@+id/book_desc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        />
</LinearLayout>
           
BookListActivity.java
public class BookListActivity extends Activity implements BookListFragment.Callbacks{


    //定義一個旗标,用于辨別該應用是否支援大螢幕
    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //指定加載R。layout。activtiy_book_list對應的界面布局
        //但實際上應用會根據螢幕分辨率加載不同的界面布局檔案
        setContentView(R.layout.activity_book_list);
        //如果加載的界面布局檔案包含ID為book_detail_container的元件
        if(findViewById(R.id.book_detail_container) != null)
        {
            mTwoPane = true;
            ((BookListFragment)getFragmentManager()
                    .findFragmentById(R.id.book_list))
                    .setActivateOnItemClick(true);
        }

    }

    @Override
    public void onItemSelected(Integer id) {
        if(mTwoPane)
        {
            //建立Bundle,準備向Fragment傳入參數
            Bundle arguments = new Bundle();
            arguments.putInt(BookDetailFragment.ITEM_ID, id);
            //建立BookDetailFragment對象
            BookDetailFragment fragment = new BookDetailFragment();
            //向Fragment傳入參數
            fragment.setArguments(arguments);
            //使用fragment替換book_detail_container容器目前顯示的Fragment
            getFragmentManager().beginTransaction()
            .replace(R.id.book_detail_container, fragment).commit();
        }
        else
        {
            //建立啟動BookDetailActivity的Intent
            Intent detailIntent = new Intent(this, BookDetailActivity.class);
            //設定傳給BookDetailActivity的參數
            detailIntent.putExtra(BookDetailFragment.ITEM_ID, id);
            //啟動Activity
            startActivity(detailIntent);
        }
    }
}
           
BookListFragment.java
public class BookListFragment extends ListFragment {

        private Callbacks mCallbacks;
        //該Fragment将通過該接口與它所在的Activity互動
        public interface Callbacks
        {
            public void onItemSelected(Integer id);
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //為該ListFragment設定Adapter
            setListAdapter(new ArrayAdapter<BookContent.Book>(getActivity(),
                    android.R.layout.simple_list_item_activated_1,
                    android.R.id.text1,BookContent.ITEMS));
        }

        //當該Fragment被添加、顯示到Activity時,回調該方法。
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            //如果Activity沒有實作Callbacks接口,抛出異常
            if(!(activity instanceof Callbacks))
            {
                throw new IllegalStateException("BookListFragment所在的Activity必須實作Callbacks接口");
            }
            //把該Activity當成Callbacks對象
            mCallbacks = (Callbacks) activity;
            Log.v("ATTACH", "1");
        }

        //當Fragment從它所屬的Activtiy中被删除時回調該方法

        @Override
        public void onDetach() {
            super.onDetach();
            //将mCallbacks賦為null
            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);
            Log.v("onListItemClick", "2");
        }

        public void setActivateOnItemClick(boolean activateOnItemClick)
        {
            getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
        }
}

           
BookDetailActivity.java
public class BookDetailActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //指定加載/res/layout目錄下的activity_book_detail.xml布局檔案
        //該界面布局檔案内隻定義了一個名為book_detail_container的FrameLayout
        setContentView(R.layout.activity_book_detail);
        //将ActionBar上的應用圖示轉換成可點選的按鈕
        getActionBar().setDisplayHomeAsUpEnabled(true);

        if(savedInstanceState == null)
        {
            //建立BookDetailFragment對象
            BookDetailFragment fragment = new BookDetailFragment();
            //建立Bundle對象
            Bundle arguments = new Bundle();
            arguments.putInt(BookDetailFragment.ITEM_ID, getIntent().getIntExtra(BookDetailFragment.ITEM_ID, 0));
            //向Fragment傳入參數
            fragment.setArguments(arguments);
            getFragmentManager().beginTransaction().add(R.id.book_detail_container1, fragment).commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == android.R.id.home)
        {
            //建立啟動BookListActivity的Intent
            Intent intent = new Intent(this, BookListActivity.class);
            //添加額外的Flag,将Activity棧中處于FirstActivity之上的Activity彈出
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

           
BookDetailFragment.java
public class BookDetailFragment extends Fragment
{
    public static final String ITEM_ID = "item_id";
    //儲存該Fragment顯示的Book對象

    BookContent.Book book;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //如果啟動該Fragment時包含ITEM_ID參數
        if(getArguments().containsKey(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.fragment_book_detail, container, false);
        if(book != null)
        {
            //讓book_title文本框顯示book對象的title屬性
            ((TextView)rootView.findViewById(R.id.book_title)).setText(book.title);
            //讓book_desc文本框顯示book對象的desc屬性
            ((TextView)rootView.findViewById(R.id.book_desc)).setText(book.desc);

        }
        return rootView;
    }
}
           

效果

小螢幕手機
Fragment與Activity之間的通信

Screenshot_20171115-162550.png

點選清單時,跳轉到另一個activity加載相應的fragment。

Fragment與Activity之間的通信

Screenshot_20171115-162715.png

大螢幕手機
Fragment與Activity之間的通信

Screenshot_20171115-163704.png

繼續閱讀