The Android control refreshableview implements drop-down refresh

Requirements: customize a ViewGroup to realize the function of drop-down refresh. After pulling down for a certain distance (the interface displayed during the pull-down can customize any complex interface), release the finger to recall the refresh function. After the user processes the refreshed content, he can call the method oncompleterefresh() to notify that the refresh is completed, and then return to the normal state. The effects are as follows:

Source code: refreshableview( https://github.com/wangjiegulu/RefreshableView )

analysis:

Our purpose is that no matter what control is, as long as there is a layer of label in the XML, all the controls where the sub labels under this label are supported and can be pulled down and refreshed. Therefore, we can use ViewGroup to implement it. Here I choose to inherit LinearLayout. Of course, other (FrameLayout, etc.) are the same.

Because a refreshed view needs to be displayed according to the finger sliding, a child view (called refreshheaderview) needs to be added here and placed at the top. The advantage of using LinearLayout is that it can be set to vertical, so it can be directly "this. Addview (refreshheaderview, 0);". Then, the height of the refreshheaderview should be changed dynamically according to the sliding distance of the finger. At the same time, check whether it has reached the height that can be refreshed. If so, update the current refresh status. When the finger is released, refresh or return to the normal state according to the previously moved refresh state.

The refreshableview code is as follows:

Of which:

Originrefreshheight, indicating the actual height of the header to refresh the view.

Refresharrivedstateheight, which indicates the drop-down distance to refresh

Refreshingheight, which indicates the height displayed during refresh

Refreshnormalheight, which indicates the height displayed by refreshheaderview in normal state

The main core code should be in the ontouchevent method. First, briefly analyze the main code: in action_ When down, record the Y coordinate of the current finger falling, and then act_ When moving, calculate the sliding distance, and judge that if the sliding distance is greater than refresharrivedstateheight, the update is in the refreshable state, otherwise the update is not in the refreshable state. Then act_ In up, if the refreshable state has been reached, the current state will be updated to the refreshing state, and the method of callback state change will be used.

If there are Scrollview and other scrollable controls in it, how should we deal with the events in it?

Just https://github.com/wangjiegulu/RefreshableView take as an example

The XML layout is as follows:

As mentioned above, the outermost is a refreshableview, and then inside is a nestscrollview. Inside nestscrollview is a textview. Because there are many words in textview, nestscrollview is used to realize scrolling (nestscrollview is extended from Scrollview, which will be discussed below). The logic at this time should be that when nestscrollview is at the top and the finger slides down, the touch event should be handed over to refreshableview for processing; When the finger slides up, that is, the Scrollview scrolls down, you need to give the touch event to the refreshableview for processing.

The following code is analyzed:

Interceptallmoveevents of refreshableview indicates whether refreshableview is required to block all move events (that is, refreshableview handles all move events by itself). If there are no controls such as Scrollview that need to handle move events in the automatic control, it can be set to true; If there are controls such as Scrollview, it needs to be set to false. In this case, refreshableview will pass the move event to the subclass for processing. Obviously, in the current example, you need to set interceptallmoveevents to false. You can see the XML file above to set the method. You can use the attribute RV: interceptallmoveevents = "false".

In the onintercepttouchevent() method, we return disallowintercept, which changes dynamically according to the call of requestdisallowintercepttouchevent() method, so that we can switch the processing object of touch event.

When the finger falls, first call the requestdisallowintercepttouchevent() method to ensure that the current event can be passed to the child control normally, that is, the current Scrollview. Then the finger will start moving in action_ In move, first calculate the current sliding distance.

If it is an effective drag down event, the event needs to be processed in refreshableview, so it needs to be intercepted and not passed to child controls, that is, interception is not allowed to be set to false; If the event is not dragged down effectively, the event is passed to the child control for processing, so it does not need to be intercepted and passed to the child control, that is, interception is not allowed to be set to true.

How to judge whether it is effective? According to downy, if downy is the original initial value, float.max_ Value, indicating that the move event was handled by the child control at the beginning of down, not by the refreshableview, indicating that it is an invalid drag down event for the refreshableview; If downy is not the original initial value, float.max_ Value indicates that the move event is already handled by refreshableview when down, so it is valid.

Then, calculate the height of the refreshheaderview, and transform the height of the refreshheaderview according to the sliding difference.

If the current state is refreshing, the move event is directly invalid.

Otherwise, judge whether the current height has reached the refreshable state or not, and update the status value.

When up, you should first ensure that the event is passed down. And reset downy. Then, according to the current status, if the refresh status is reached, the refresh starts, and the status is being refreshed when the current quota status is updated; If the refresh state is not reached, the execution animation returns to the normal state; If you are in the refresh state, you can also execute the animation to return to the refresh height.

Then analyze nestscrollview:

As shown above, you also need to override the onintercepttouchevent () method, which needs to pass all events except the move event, so that the innermost textview has onclick and other events.

In the ontouchevent method, in action_ When going down, first record the Y coordinate of the down, and then ensure that the events of the parent (i.e. refreshableview) can be passed, so you need to call the getparent(). Requestdisallowinterceptetouchevent() method. Because the drop-down refresh can only occur when the Scrollview scroll bar is at the top, in move, if the current state is at the top, you need to let the parent control (refreshableview) intercept it, and then directly return false to pass the current event to the ontouchevent method in refreshableview for processing. If it is not at the top, the processing of calling the parent control (refreshableview) is masked and handled directly by itself. Finally, ensure that events can be passed to Scrollview when up.

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