Fragment a solution to the problem of multi-layered nested method invocation _android

Source: Internet
Author: User

The production and introduction of fragment

Android runs on a variety of devices, with small-screen phones, oversized screens and even TVs. For the screen size gap, in many cases, is the first mobile phone to develop a set of apps, and then copy, modify the layout to adapt to the plate God horse super big screen. Can't it be that an app can adapt to both the phone and the tablet at the same time, of course, must have AH. Fragment's appearance is to solve such a problem. You can think of fragment as an integral part of an activity's interface, and even the interface of the activity can be composed entirely of different fragment, and the more handsome fragment has its own lifecycle and the events that receive and process the user, This eliminates the need to write a bunch of code for the event handling of the control in the activity. More importantly, you can dynamically add, replace, and remove a fragment.

The life cycle of fragment

Fragment must be dependent and activity, so the life cycle of activity will directly affect the life cycle of fragment. This picture of the official website illustrates the relationship between the two life cycles:

Below is a description of how to resolve a method call under fragment multi-layered nesting.

There may be an activity in the same

Object 1:a Fragment

Object 2:b fragment a ListView or a viewadapter

Interaction: An interface in B fragment after obtaining the information (possibly network information, etc.), passing it to a fragment or a fragment after handling the event and then notifying the interface under B fragment to make a change.

Programme 1

Calling methods through a middleman activity

In b fragment, get a fragment object to invoke the method in a fragment

Disadvantage: When there are many layers, the acquisition of such intermediaries becomes a multilayered middleman, which requires many times to handle intermediaries

For example, when you want to invoke a method in the subclass object of the pager class parent class in the Viewpager list in B fragment

1, the subclass object needs to override the method

2, you need to define the parent class for this method

3, you need to find the subclass object in the list in B fragment and call the method

4, you need to find the Bfragment object in a Fragment to call the method

This shows that when the nested way more complex, this method needs to deal with a more hierarchical relationship ... Therefore, the adoption of option 2

Programme 2

Interface mode

Defining an interface in a fragment

This shows how the case calls B fragment in Afragment by defining the interface:

public class A_fragment extends Basefragment { 
private onswitchpaperlistener onswitchpaperlistener; 
Define interface and interface methods public 
interface onswitchpaperlistener{ 
void switchpaper (int i); 
} 
Provide a way to set up the listener externally. Public 
void Setonswichtpaerlistener (Onswitchpaperlistener listener) { 
This.onswitchpaperlistener=listener ; 
} 
@Override public 
void Intievent () { 
//This has processed a lot of logic and information before it needs to pass the message through Afragment to B. 
if ( Onswitchpaperlistener!=null) { 
onswitchpaperlistener.switchpaper (position); 
} else { 
} 
} 
}

and bfragment only need to implement the interface and rewrite the interface method can be called by a fragment

In a number of nameless nested classes in B fragment:

Mainactivity.getafragment (). Setonswichtpaerlistener (New Slidingleft_fragment.onswitchpaperlistener () { 
@ Override public 
void switchpaper (int i) { 
basecenterpaper currentpaper= basecenterpaperlist.get (i); 
Tv_title.settext (Lists.get (i). GetTitle ()); 
Remove 
fl.removeallviews () first; 
Currentpaper.initdata (); 
Fl.addview (Currentpaper.getroot ()); 
} 

The above is a small set to introduce the fragment multilayer nesting method to call the problem solution, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.