Detailed explanation of fragment loading mode and data communication in Android

1、 Loading mode

1. Static loading

1.1 loading steps

(1) Create fragment: create a custom fragment class, inherit from the fragment class, and bind the custom fragment class to the fragment view (convert layout to view)

View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)

The inflator is used to bind the layout file of the fragment, convert the layout into a view object and return it; The container is the parent container of the UI of the fragment. The return value is the UI displayed by fragment. If it is not displayed, it returns null.

inflate(int resource,ViewGroup root,boolean attachToRoot)

Resource is the layout file to be loaded for fragment; Root is the parent ViewGroup that loads the fragment, that is, the container passed in oncreateview; Attachtoroot is whether to return the parent ViewGroup.

(2) Using fragment: to introduce fragment in the parent view, the name attribute and a unique identifier must be specified for static loading. The identifier can be ID or tag

(3) Listening event: if listening events are set in the class corresponding to the parent view, you can directly access the sub components in the fragment; If it is set in the fragment class, the child component (view. Findviewbyid (ID)) in the fragment must be accessed through the view object returned by inflate().

1.2 simple examples

Myfragment view:

Myfragment class:

Parent view referencing fragment:

Set event listening for the class corresponding to the parent view:

2. Dynamic loading

2.1 loading steps

(1) Get transaction manager: operations such as adding, removing and replacing fragments are transactions. You need to obtain the transaction manager through the following code to dynamically operate the fragment.

(2) Create fragment object: create the fragment to be loaded, and then dynamically load it through methods such as add or replace.

2.2 simple examples

Layout:

Java:

2、 Data communication

3. The activity passes data to the fragment

3.1 activity transfers data to dynamically loaded fragments

(1) Get the fragment object in the activity;

(2) Create a bundle object and pass in data;

(3) Pass the bundle object to the fragment object;

(4) Get the bundle object in the fragment and unpack to get the data.

Example: there is only one button with ID of send in the activity and only one textview in myfragment. There is no layout code here.

Activity:

Fragment:

3.2 activity transfers data to statically loaded fragments

(1) Create a data object as a container in the fragment, and create getters and setters;

(2) Get the fragmentmanager in the activity;

(3) Obtain the fragment object through the findfragmentbyid or findfragmentbytag method of the transaction manager;

(4) Call the setter method of the container through the obtained fragment object to pass the value.

Example: the only difference between the layout here and the dynamically loaded layout is that the send button is placed in the fragment, and the others are the same.

Fragment:

Activity:

4. Fragment transfers data to activity

(1) Write a callback interface in the fragment;

(2) The callback interface is implemented in the activity, and the implemented function is used to pass values;

(3) Rewrite onattach in fragment and create an interface object in it to get the passed activity (my understanding is that this interface is actually equivalent to a parent class of the passed activity, which uses polymorphism);

(4) Use the obtained interface object to transfer values.

Fragment:

Activity:

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>