【Android Training - 03】使用Fragments建立動態的UI [ Lesson 4 - Fragment之間的通信 ]

爲了重用fragment的UI組件,你必須爲每個fragment建立自己的container,模塊化自己的layout與行爲。一旦你定義了那些可重用的fragment,你可以使用activity與他們建立聯繫,對那些UI組件做組合動作等。

通常,你也會想要fragment-s之間能夠交流。例如,基於用戶事件來改變內容。所有的Fragment-to-Fragment之間的交互都是基於activity進行操作的。兩個fragment之間沒有辦法直接交互。

Define an Interface [定義接口]

爲了允許fragment與activity進行交互,你可以在fragment中定義一個interface,然後在activity中去implement它。Fragment在它的onAttach()方法裏面捕獲接口的實現,然後call接口的方法來與activity進行交互。

下面是一個fragment與activity進行交互的例子:

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }
    
    ...
}

那麼現在,fragment可以通過onArticleSelected() 傳遞messages給activity。這個方法去使用OnHeadlineSelectedListener接口的mCallback

例如:下面在fragment中的方法會在用戶點擊listitem的時候被call到。Fragment使用這個callback接口來傳遞事件給父activity。

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Send the event to the host activity
        mCallback.onArticleSelected(position);
    }

Implement the Interface [實現接口]

爲了接受到fragment返回事件的callback,包含那個fragment的activity必須實現定義在fragment中的接口。

例如:下面的activity實現了上面例子中的接口。

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...
    
    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

Deliver a Message to a Fragment [傳遞msg給fragment]

宿主activity可以通過findFragmentById()捕獲到fragment實例,然後call fragment的public方法來傳遞msg給fragment。

如下演示了使用另外一個fragment來顯示從上面例子的callback中獲取到的內容。

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article

        ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);
        } else {
            // Otherwise, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);
        
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }
}
學習自:https://developer.android.com/training/basics/fragments/communicating.html,歡迎一起交流!

轉載請註明出自:http://blog.csdn.net/kesenhoo,謝謝!








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章