Android uses fragment to imitate the example code of wechat interface

What is fragment

Since the introduction of the concept of fragments in Android 3.0, it can be translated into fragments and fragments according to the translation of word sea. Its purpose is to solve the dynamic and flexible UI design with different screen resolution. Large screen such as tablet, small screen such as mobile phone, tablet computer design makes it have more space to put more UI components, and the extra space to store UI will produce more interaction, thus giving birth to fragments.

The design of fragments does not require you to personally manage the complex changes of view hierarchy. By dispersing the layout of the activity into the frame, you can modify the appearance of the activity at runtime, and save some changes in the back stack managed by the activity. When a clip specifies its own layout, it can be configured in different combinations with other clips, and the layout configuration can be modified for different screen sizes in the activity (the small screen may display one clip at a time, while the large screen can display two or more).

Fragments must be written as reusable modules. Because a fragment has its own layout, its own event response, and its own life cycle and behavior, you can include different instances of the same fragment in multiple activities. This is especially important to make your interface give users a perfect experience under different screen sizes.

Fragment advantages

Fragment enables you to separate an activity into multiple reusable components, each with its own lifecycle and UI.

Fragment can easily create dynamic and flexible UI design, which can be adapted to different screen sizes. From mobile phones to tablets.

Fragment is a separate module that is tightly bound to activity. It can dynamically remove, join, exchange, etc. during operation.

Fragment provides a new way to unify your UI on different Android devices.

Fragment solves the problem that the switching between activities is not smooth and light.

Fragment replaces tabacity for navigation, with better performance.

Fragment adds a nested fragment usage method in version 4.2. To generate a better interface effect.

It is more convenient for fragment to update local content. In order to achieve this, multiple layouts should be put into one activity. Now, multiple fragments can be used instead. Fragments can be loaded only when necessary to improve performance.

The returned result can be received from startactivityforresult, but the view cannot.

The picture shows the effect of the example. When you click the button below, the upper part will automatically switch to the corresponding content. The technique used here is fragment.

You must have known something about fragment. Even if you don't know, baidu also has a detailed introduction. Here we will focus on the implementation process.

First of all, take one part of the "home page":

The upper part is the fragment, and the lower part is the relatively fixed button area. That is, when the button is clicked, only the upper part of the content is switched. Therefore, each fragment has its own XML layout file. As shown in the figure, the XML file of the "home page" fragment is composed of a textview. After completing the fragment XML file, you need to define a corresponding Java class to find it. For example, the corresponding class of the home page is homefragment.java. Note that this class needs to inherit fragment, and each class that inherits fragment needs to override its oncreateview method. The specific code is:

When this step is completed, the content of each fragment has been completed. The next step is to bind each fragment to a page and display it on it. Here I use a menufunction. XML

The code is:

In this layout, the LinearLayout above is used to display the fragment content, and the button below is used.

Then, in the corresponding Java class of menufunction.xml, find the defined fragment and display it. The main idea is: ① get a manager ② start a transaction ③ replace the fragment content ④ submit. Note that the fourth step here is easy to be forgotten.

The code is:

Among them, because there are many and similar click events involved, I used a special writing method, that is, setonclicklistener (this), used this for the parameter, and redefined a click method. Note: to write this, you must inherit an interface of clicklistener. Finally, submit on OK.

The effect is:

This is to use fragment to simulate wechat interface.

The above is the example code of Android using fragment to imitate wechat interface introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>