Android realizes the switching effect of loading state view

The details of Android loading status view switching are as follows

1. Introduction to Android interface switching state

How to switch interface status? Some interfaces want to customize their states? How to add a click event? The following is to solve these problems! Error loading data in content interface loading data no data after loading no network

2. Change the thinking and extract several statuses of separated class management

Previous practice:

Directly include these interfaces into the main interface, and then dynamically switch interfaces. Later, it was found that this processing is not easy to reuse in other projects, and the display and hiding of these states in the activity are disorderly. Subclasses are used to inherit the characteristics of the parent class and write the switching state in the parent class. However, what should be done if some interfaces do not inherit the parent class

Current practice:

To completely separate view state switching from activity, you must encapsulate these state views into a management class, and then expose several methods to realize the switching between views. Different views can be needed in different projects, so consider designing the management class into builder mode to freely add the required state view

3. Analysis on the advantages of the state switching tool

You can freely switch five states: content, empty data, abnormal error, loading and network error. The parent baseactivity directly exposes the states in the five states to facilitate the unified management of state switching by subclasses

When the status is failed to load data, click to refresh the data; When the status is no network, click to set the network

If some pages want to customize the state layout, they can also be implemented freely. It is very simple:

4. Steps on how to implement

1) . first look at the state manager class [builder builder mode]

Loadinglayoutresid and contentlayoutresid represent XML files waiting to load and display content. Viewstub should be used for several abnormal states, because loading and content view always need to be loaded and displayed during interface state switching, but the other three will be loaded and displayed only when there are no data or network exceptions, so loading them with viewstub can improve performance.

2) . there are about 5 states. How to manage these States? Added to the collection, SparseArray in Android saves more memory than HashMap and has better performance under some conditions, mainly because it avoids automatic boxing of keys (int to integer type). It stores data through two arrays, one for key and the other for value. In order to optimize performance, It also compresses the data to represent the data of sparse array, so as to save memory space

3) . when a layout is displayed, the method called is as follows

In the method, different codes are executed through ID judgment. First, judge whether the ViewStub is empty. If it is empty, it means that no View is added to false. Instead of empty, it loads View and adds it to the collection, then calls the showHideViewById method to show hidden View. RetryLoad method adds the event to the retry button.

4) . then hide the layout by ID

Find the view to be displayed through ID, display it and hide other views. If the display and hide listening event is not empty, call its display and hide methods respectively

5) Finally, look at the reload method

5. Introduction to use method

1) . add code directly to the activity

2) . override the following methods in the parent class, and the child class can inherit directly

3) For more detailed introduction, you can directly refer to demo https://github.com/yangchong211/YCStateLayout

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>