In this section, you will learn
1. Define the interface
2. Implement the interface
3. Pass the message to fragment
In order to reuse the fragment UI component, you should make the fragment self-contained and modular in the design by defining each FRAGEMNT own layout and behavior. Once you have defined these reusable fragment, you can associate them with activity and connect them through the program's logical code to achieve a holistic combination of UI.
You will often want a fragment to communicate with another fragment, such as changing the content in fragment based on user events. All fragment quality single-volume communications are linked through activity. Two fragment should not communicate directly between the two.
Defining interfaces
In order to allow fragment to communicate with its activity, you should define an interface in the fragment class and implement it in the activity. Fragment gets the concrete implementation object of the interface in the Onattach () callback function. Later, fragment can invoke the methods in the interface to implement communication with the activity.
Here is an example of a fragment activity communication:
The activtiy used to store the fragment must implement this interface public interface Onheadlineselectedlistener {public void onarticleselected (int position); } @Override public void Onattach (activity activity) { Super.onattach (activity); This is to ensure that the activity container implements the interface used for callbacks. If not, it throws an exception. try { mcallback = (onheadlineselectedlistener) activity; } catch (ClassCastException e) { throw new ClassCastException (activity.tostring () + "must implement Onheadlineselectedlistener"); } }
Now fragment can implement the call by using the OnHeadlineSelectedListener接口的mCallback实例
onArticleSelected()方法(或其他在接口中的方法)将消息传递给Activity。
For example, the following method in fragment will be called when the user clicks on a list item. This fragment uses the callback interface to pass the time to the parent activtity.
@Override public void Onlistitemclick (ListView l, View v, int position, long ID) { //Send time to activity host Mcallba ck.onarticleselected (position); }
Implementing interfaces
In order to receive an event callback passed from fragment, the host activity must implement an interface defined in the Fragment class.
For example, the following activity implements the interface in the previous example.
public static class Mainactivity extends Activity implements headlinesfragment.onheadlineselectedlistener{ ... public void onarticleselected (int position) {/ /user Selected header header in Headlinesfragment //Do some business Operation }}
Passing a message to fragment
宿主Activity可以通过调用findFragmentById()方法获取Fragment实例,然后直接调用Fragment的公有方法,将消息传递给Fragment。
例如,想象一下,之前讲过的Activity可能包含有其他的fragment,而这些fragment通过上面的回调函数返回的接口动态地显示一些内容。这种情况下,Activity可以将回调函数中接收到信息传递给需要的fragment。
public static class Mainactivity extends Activity implements headlinesfragment.onheadlineselectedlistener{... public void onarticleselected (int position) {//the user selects the header header in Headlinesfragment//makes some necessary business operations Articlef Ragment Articlefrag = (articlefragment) Getsupportfragmentmanager (). Findfragmentbyid (R.id.article_fragment) ; if (Articlefrag! = null) {//If article Frag is not empty, then we are showing the layout of two fragmnet simultaneously ...//calling the party in Articlefragment method to update its contents articlefrag.updatearticleview (position); } else {//Otherwise, we are in a layout that contains only one fragment and need to exchange fragment ...//create fragment and give him an argument about the selected article Artic Lefragment newfragment = new Articlefragment (); Bundle args = new bundle (); Args.putint (Articlefragment.arg_position, POSITION); Newfragment.setarguments (args); Fragmenttransaction transaction = Getsupportfragmentmanager (). BeginTransaction (); Use this f.Ragment replaces anything in the Fragment_container//and adds a transaction to the back stack so that the user can fallback to the previous state transaction.replace (R.ID.FRAGM Ent_container, newfragment); Transaction.addtobackstack (NULL); Commit Transaction Transaction.commit (); } }}
Communication between #########################################################[android][fragment][and]fragment (2014-01-07 15:02:45)
reproduced
Tags: fragment it |
Category: Android |
In this section, you will learn
1. Define the interface
2. Implement the interface
3. Pass the message to fragment
In order to reuse the fragment UI component, you should make the fragment self-contained and modular in the design by defining each FRAGEMNT own layout and behavior. Once you have defined these reusable fragment, you can associate them with activity and connect them through the program's logical code to achieve a holistic combination of UI.
You will often want a fragment to communicate with another fragment, such as changing the content in fragment based on user events. All fragment quality single-volume communications are linked through activity. Two fragment should not communicate directly between the two.
Defining interfaces
In order to allow fragment to communicate with its activity, you should define an interface in the fragment class and implement it in the activity. Fragment gets the concrete implementation object of the interface in the Onattach () callback function. Later, fragment can invoke the methods in the interface to implement communication with the activity.
Here is an example of a fragment activity communication:
The activtiy used to store the fragment must implement this interface public interface Onheadlineselectedlistener {public void onarticleselected (int position); } @Override public void Onattach (activity activity) { Super.onattach (activity); This is to ensure that the activity container implements the interface used for callbacks. If not, it throws an exception. try { mcallback = (onheadlineselectedlistener) activity; } catch (ClassCastException e) { throw new ClassCastException (activity.tostring () + "must implement Onheadlineselectedlistener"); } }
Now fragment can implement the call by using the OnHeadlineSelectedListener接口的mCallback实例
onArticleSelected()方法(或其他在接口中的方法)将消息传递给Activity。
For example, the following method in fragment will be called when the user clicks on a list item. This fragment uses the callback interface to pass the time to the parent activtity.
@Override public void Onlistitemclick (ListView l, View v, int position, long ID) { //Send time to activity host Mcallba ck.onarticleselected (position); }
Implementing interfaces
In order to receive an event callback passed from fragment, the host activity must implement an interface defined in the Fragment class.
For example, the following activity implements the interface in the previous example.
public static class Mainactivity extends Activity implements headlinesfragment.onheadlineselectedlistener{ ... public void onarticleselected (int position) {/ /user Selected header header in Headlinesfragment //Do some business Operation }}
Passing a message to fragment
宿主Activity可以通过调用findFragmentById()方法获取Fragment实例,然后直接调用Fragment的公有方法,将消息传递给Fragment。
For example, imagine that the activity previously described might contain other fragment, and these fragment dynamically display some content through the interface returned by the callback function above. In this case, the activity can pass the information received in the callback function to the required fragment.
public static class Mainactivity extends Activity implements headlinesfragment.onheadlineselectedlistener{... public void onarticleselected (int position) {//the user selects the header header in Headlinesfragment//makes some necessary business operations Articlef Ragment Articlefrag = (articlefragment) Getsupportfragmentmanager (). Findfragmentbyid (R.id.article_fragment) ; if (Articlefrag! = null) {//If article Frag is not empty, then we are showing the layout of two fragmnet simultaneously ...//calling the party in Articlefragment method to update its contents articlefrag.updatearticleview (position); } else {//Otherwise, we are in a layout that contains only one fragment and need to exchange fragment ...//create fragment and give him an argument about the selected article Artic Lefragment newfragment = new Articlefragment (); Bundle args = new bundle (); Args.putint (Articlefragment.arg_position, POSITION); Newfragment.setarguments (args); Fragmenttransaction transaction = Getsupportfragmentmanager (). BeginTransaction (); Use this f.Ragment replaces anything in the Fragment_container//and adds a transaction to the back stack so that the user can fallback to the previous state transaction.replace (R.ID.FRAGM Ent_container, newfragment); Transaction.addtobackstack (NULL); Commit Transaction Transaction.commit (); } }}
Share:
2
Like
0
Gift Pen
Http://www.cnblogs.com/kissazi2/p/3440257.html
Http://blog.sina.com.cn/s/blog_75992b660101os3k.html
Fragment data transfer, communication