Android: Combined with remote control to solve Android Problems

Source: Internet
Author: User

Android: Combined with remote control to solve Android Problems
Problem description people who have developed Android have encountered the following problems: As requirements change, some portal interfaces usually encounter issues such as UI increase, decrease, content changes, and jump interface changes. The code must be manually modified every time a change occurs, and the portal interface usually has the "little red dot" logic like "unread information reminder". Once the UI changes, the "little red dot" logic also needs to be re-computed. If different RD nodes maintain the code, the coupling is very high and the error probability is very high. This article takes the self-stock-selected personal page card as an example (as shown in the interface), and provides a set of solutions to dynamically update the UI and better solve the logic of unread alerts. The old Scheme (Phase out) usually solves the problem of dynamic UI changes through remote control. Taking "Asset Management" as an example, the old solution will write all the items in XML, such: three items: "Hong Kong stock trading", "fund trading", and "Boutique Financial Management. Then, parse the items to be hidden Based on the JSON passed in the background. Clicking different items will jump to different activities (as shown in). This part of the jump operation is also written to the code. This solves some of the problems, but if the demand adds items, such as the "Shanghai and Shenzhen trading" and "US stock trading", the existing code needs to be modified. For the unread indicator (little red dot) function, the function is to show a little red dot on the UI to remind users. For example, the dynamic profile picture reminder of the stockholders, the "NEW" reminder of asset management, and the NEW version reminder set by the system. The old scheme is to define an int (32-bit), with each bit representing an item on the UI. For example, the number of dynamic friends is 1st bits, and the number of unread reminders is 2nd bits... the idea of "little red dot" is which item has unread information, then the one corresponding to the int type is set to 1, otherwise it is set to 0. Once an item has an unread reminder change, the bit corresponding to the int type is changed and asynchronously written to SharedPreference. The observer mode is used to notify the UI for updates, as shown in: the biggest drawback of the above practice is that it does not implement the "open-close" principle. In the face of expansion, if you add an item, You have to modify the existing code. You need to add a flag in the int type. The observer mode also needs to register a new item. So next I will introduce another solution to better solve this problem. The new solution data abstraction first abstracts the data and groups the UI, as shown in: organizes the data in a tree structure according to the combination mode, shows the overall/partial hierarchy, as shown in. The advantage of doing so is that individual objects and object combinations can be processed in a consistent manner. Blue indicates the node, while green indicates the leaf node. The class diagram of the combination mode is as follows: data abstraction for the UI. Both the ListItem list items and the items in the GridView Item use the PersonalItem object, as shown below: For PersonalItem, some meaningless methods are not implemented, such as add (), remove (), getChild (), etc. Calling them will throw an UnsupportedOperationException () exception. Perfect solution to the problem of unread alerts (little red dot). For the calculation of little red dot, the PersonalGroup class uses the combination + iterator mode. The Code is as follows: here the iterator is used to traverse all PersonalComponent components. PersonalItem or PersonalGroup may be encountered during traversal. Since they are all PersonalComponent and the getUnreadIndicator () method is implemented, we only need to call getUnreadIndicator. As shown in, PersonalGroup loads PersonalComponent, which may be PersonalItem or PersonalGroup. The advantage of the combination mode is to ignore the specific type-all acquired are PersonalComponent, and then use polymorphism to call the getUnredIndicatorCount () method of a specific class. If it is a PersonalGroup, continue to call this method (like this method, it will start another traversal); if it is a PersonalItem, it indicates that it is traversed to the end of the tree structure (that is, the leaf node ), the following processing is performed: If getUnreadIndicator is set to true, the PersonalComponent needs to display the red dot. Therefore, the preceding combination + iteration method can be used to perform a call at the root node recursively. As shown in, when the leaf node "A-share competition" has an unread reminder, its parent groups also has an unread reminder, which is always counted to the root node. GetUnredIndicatorCount () is a method for each item to determine whether to display a small red dot. This decouples the local environment from the overall environment. As a whole, it is necessary to calculate the small red dot. As for how to calculate it, It is delegated to a specific class for implementation. That is, the separation of "what to do" and "how to do" in the object-oriented model. RD can be freed from it, without having to focus on the overall implementation, you just need to focus on your own implementation. For example, if you need to add "US stock transaction" to "Asset Management", RD only needs to add the "US stock transaction" content. In the next section, we will explain that this part of content has also been replaced by remote control. We remotely control the passed Date to compare it with the Date stored locally. If it is a new Date value, if this item is "NEW", the corresponding red dot needs to be displayed. The remote control dynamically updates the UI. When the remote control changes (an active request is sent once every five minutes), The JSON string returned by the remote control interface is parsed to generate a list of PersonalItem objects. Each item corresponds to an item on the UI. Note that a URL is also included, which is the URL to jump to by clicking the UI control. Taking "Asset Management" as an example, it contains sub-items such as "Shanghai and Shenzhen Stock Exchange" and "fund transaction. When you click any subitem, the same Activity-WebviewActivity is started, which contains a WebView control. Because the jump URLs of each subitem are different, this WebView loads different URLs, which completes the jump to different interfaces. Then, put the PersonalItem in the Groups according to the tree structure described above. As shown in: the Model stores the data structure to be displayed. This data is parsed by Parser to generate the UI content. As shown in the process: the Parser module is a recursive function that recursively parses the Model. Load the resolved List Item, Grid Group, and GridView Item to their XML files, and dynamically Add the UI component to the program. The onClick event indicates that the callback has been written when the PersonalItem is defined. For example, "Asset Management" belongs to the Grid Group, and its subitem "Shanghai and Shenzhen Stock Exchange" and "fund transaction" belong to the GridView Item. The onClick method has been defined in the above "Build PersonalItem Objects" step, and The onClick method is called to jump to WebViewActivity. This Activity loads URLs of different GridView items, in this way, you can click different items to jump to different pages. Note: For the ListItem element, that is, the list item (not the GridView element), the remote update policy is not implemented. Because their jump logic is to jump to their respective activities, it is fixed, and their text descriptions, icons, and whether to hide do not need to be updated in the background. Therefore, in the actual project, only the content of the GridView is remotely controlled to dynamically update the UI mechanism. In addition, some pitfalls have also been encountered in the process of dynamically updating the UI through remote control. For example, when the update is remotely controlled, the user exits the App, and the system just destroys the Activity. When executing the inflateUI of the preceding Parser module, you must determine whether the current context is empty. If the context is empty, exit directly. Conclusion and data this article abstracts the UI data, constructs the data using the combination mode, maps the data to the UI using recursion, and processes click events. Data sources can be controlled remotely for dynamic updates, and RD is freed from them. In addition, the combination + iterator perfectly solves the problem of little red dot and follows the "open-closed" principle to separate "what to do" from "how to do.

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.