In order to reuse the fragment UI components, we should build each fragment into fully self-contained, modular components that define their own layout and behavior. Once these modular fragment have been defined, they can be associated with activity, allowing them to combine with the logic of application to achieve a global, composite UI.
Often interactions between fragment may be required, such as changing fragment content based on user events. The interaction between all fragment needs to be through their associated activity, and the two fragment should not interact directly with each other.
Define an interface
- In order for fragment to interact with the activity, an interface can be defined in the fragment class and implemented in the activity. Fragment gets the implementation of the interface in the Onattach () method of their life cycle, and then invokes the interface's methods to interact with the activity.
Here is an example of fragment interacting with the activity:
Explanation: This fragment mainly writes a ListView and then adds a click event, we can get the position of the clicked item, we pass this position to accommodate the activity of fragment.
Public classFraglistextendsfragment{Privateview view; PrivateListView ListView; PrivateArrayadapter<string>adapter; PrivateOnparentlistener Listener; //the instantiation of the interface must be@Override Public voidOnattach (activity activity) {Listener=(Onparentlistener) activity; Super. Onattach (activity); } @Override @Nullable PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancest ATE) {View= Inflater.from (Getactivity ()). Inflate (R.layout.frag_list,NULL); Initview (); returnview; } voidInitview () {ListView=(ListView) View.findviewbyid (r.id.fralist); Adapter=NewArrayadapter<string>(Getactivity (), Android. R.layout.simple_list_item_1,adddatas ()); Listview.setadapter (adapter); Listview.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {listener.getposition (position); } }); } //show the position that the activity is passing. Public voidGetparentcontent (intposition) {Toast.maketext (Getactivity (), string.valueof (position),0). Show (); } //Source Data PrivateList Adddatas () {list<String> list =NewArraylist<string>(); for(inti = 0;i<10;i++) {List.add ("Item" +i); } returnlist; } //Defining Interfaces Public Interfaceonparentlistener{voidGetPosition (intposition); } }Implementing interfaces
In order to receive callback events, the host activity must implement the interfaces defined in fragment.
As an example, the following activity implements the interface in the example above.
Activity: Inherit fragment Inside interface, and override method, through this interface we can get click Position
Public classFragmentlistactivityextendsFragmentactivityImplementsonparentlistener{//show the position that fragment sent over. PrivateTextView TextView; //accept the position that fragment sent over. Private intT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_fragment_list); Initview (); Addfragment (); } voidInitview () {TextView=(TextView) Findviewbyid (R.id.text); }; //This is an interaction with fragment, calls the Fragment method, and displays the incoming T voidtofragment () {Findviewbyid (r.id.fragbtn). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {fraglist fraglist=(fraglist) Getsupportfragmentmanager (). Findfragmentbyid (R.id.frag); if(Fraglist! =NULL) {fraglist.getparentcontent (t); } } }); } //Get Fragment voidaddfragment () {Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.frag,Newfraglist ()). commit (); } @Override Public voidGetPosition (intposition) {T=position; Textview.settext (string.valueof (position)); }}
The interaction between fragment and Activiy