The previous article added to the process knowledge of event distribution in Android, which we went on to analyze the pinnedheaderexpandablelistview implementation of the teacher's "pull down".
What is the role of the Ongiveuptoucheventlistener interface in Stickylayout?
Public interface Ongiveuptoucheventlistener {public boolean giveuptouchevent (Motionevent event); }
The following methods are also provided for setting up the listener in Stickylayout:
public void Setongiveuptoucheventlistener (Ongiveuptoucheventlistener l) { mgiveuptoucheventlistener = l; }
This approach is actually a hook method that defines an abstract method (not specifically implemented) Giveuptouchevent in Ongiveuptoucheventlistener. The concrete logic is then implemented by Mainactivity inheriting the Ongiveuptoucheventlistener interface.
@Override Public Boolean giveuptouchevent (Motionevent event) { if ( Expandablelistview.getfirstvisibleposition () = = 0) { View view = Expandablelistview.getchildat (0); if (view! = null && view.gettop () >= 0) { return true; } } return false; }
The logic in this method: Take the first visible item in the Expandablelistview, and if it is the first in its child view, then the first part of the header should be slid (let it unfold).
What is the difference between true and false returned here? Look down
@Override public boolean onintercepttouchevent (Motionevent event) {int intercepted = 0; int x = (int) event.getx (); int y = (int) event.gety (); Switch (event.getaction ()) {case Motionevent.action_down: {mlastxintercept = x; Mlastyintercept = y; MLASTX = x; Mlasty = y; intercepted = 0; Break } case motionevent.action_move: {int deltax = x-mlastxintercept; int deltay = y-mlastyintercept; if (Mstatus = = status_expanded && deltay <=-mtouchslop) {intercepted = 1; } else if (Mgiveuptoucheventlistener! = null) {if (mgiveuptoucheventlistener.giveuptouchevent) & & DeltaY >= mtouchslop) {intercepted = 1; }} break; } case MOTIONEVENT.ACTION_UP: {intercepted = 0; Mlastxintercept = mlastyintercept = 0; Break } Default:break; } log.d (TAG, "intercepted=" + intercepted); return intercepted! = 0; }There are a few lines of code in the Action_move of the event interception method in the Stickylayout class:
if (Mstatus = = status_expanded && deltay <=-mtouchslop) { intercepted = 1; } else if (Mgiveuptoucheven Tlistener! = null) { if (mgiveuptoucheventlistener.giveuptouchevent (event) && DeltaY >= mtouchslop) { intercepted = 1; } }
Now you should understand, hehe. Status_expanded is a status value, meaning that the header section is now expanded, and if the header part is closed, the return value of the giveuptouchevent is determined. If Giveuptouchevent returns True, the list is all pulled down and the header section should be expanded. If you return false, you should decline the list instead of expanding the header section.
Second, the realization of Pinnedheaderexpandablelistview to Onscrolllister
@Override public void onscrollstatechanged (abslistview view, int scrollstate) { if (Mheaderview! = null && Amp Scrollstate = = scroll_state_idle) { int firstvisiblepos = Getfirstvisibleposition (); if (Firstvisiblepos = = 0) { mheaderview.layout (0, 0, mheaderwidth, mheaderheight);} } if (Mscrolllistener! = null) { mscrolllistener.onscrollstatechanged (view, scrollstate);} } @Override public void Onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int Totalitemcount) { if (Totalitemcount > 0) { refreshheader (); } if (Mscrolllistener! = null) { mscrolllistener.onscroll (view, Firstvisibleitem, VisibleItemCount, Totalitemcount ); } }
Onscrolllistener is a scrolling event for a ListView.
In onscrollstatechanged (abslistview view, int scrollstate), there are three states of Scrollstate, respectively:
1. Scroll_state_fling: Start scrolling
2. Scroll_state_touch_scroll: Scrolling
3, Scroll_state_idle: Has stopped
The Onscroll () method always calls back when the list is scrolled, knowing that scrolling stops to stop the callback, and also callback once when clicked. And onscrollstatechanged means the callback in the above three states, the callback sequence is as follows:
- 1th time: scrollstate = scroll_state_touch_scroll (1) scrolling
- < Span margin:= "0px=" "padding:=" "border:=" "none=" "color:=" "black=" "background-color:=" "inherit=" "style=" Word-wrap:break-word; padding:0px; margin:0px; " > 2nd time: Scrollstate = scroll _state_fling (2) Fingers do the throwing action (finger off the screen, a hard slide)
- < Span margin:= "0px=" "padding:=" "border:=" "none=" "color:=" "black=" "background-color:=" "inherit=" "style=" Word-wrap:break-word; padding:0px; margin:0px; " > 3rd time: Scrollstate = scroll _state_idle (0) Stop scrolling
- The header of the list is laid out (drawn) again after scrolling stops in the Onscrollstatechanged method above. The main purpose of the Onscroll method is to invoke the previously mentioned Refreshheader () method to callback and refresh the list header.
- Iv. How to handle the following list head up the list above?
-
protected void Refreshheader () {if (Mheaderview = = null) {return; } int firstvisiblepos = Getfirstvisibleposition (); int pos = Firstvisiblepos + 1; int firstvisiblegrouppos = Getpackedpositiongroup (getexpandablelistposition (Firstvisiblepos)); int group = Getpackedpositiongroup (Getexpandablelistposition (POS)); if (group = = Firstvisiblegrouppos + 1) {View view = Getchildat (1); if (View.gettop () <= mheaderheight) {int delta = mheaderheight-view.gettop (); Mheaderview.layout (0,-delta, Mheaderwidth, Mheaderheight-delta); }} else {mheaderview.layout (0, 0, mheaderwidth, mheaderheight); } if (Mheaderupdatelistener! = null) {Mheaderupdatelistener.updatepinnedheader (firstvisiblegrouppos); } }
You can see in the Refreshheader () method that the above is not more than a list header if it is to reset the position of the following list header to "standard position", so you feel a sense of overhead.
- Iii. How to expand the Shrink list
-
@Override public boolean dispatchtouchevent (motionevent ev) {int x = (int) ev.getx (); int y = (int) ev.gety (); LOG.D (TAG, "dispatchtouchevent"); int pos = pointtoposition (x, y); if (y >= mheaderview.gettop () && y <= mheaderview.getbottom ()) {if (ev.getaction () = = Motioneven T.action_down) {mactiondownhappened = true; } else if (ev.getaction () = = motionevent.action_up) {int groupposition = Getpackedpositiongroup (Getexpanda Blelistposition (POS)); if (groupposition! = invalid_position && mactiondownhappened) {if (isgroupexpanded (Grouppositi ON) {Collapsegroup (groupposition); } else {expandgroup (groupposition); } mactiondownhappened = false; }} return true; } return SUPEr.dispatchtouchevent (EV); }You can see the following code in the event distribution function in Pinnedheaderexpandablelistview:
-
Limit height (in list header position)
-
if (isgroupexpanded (groupposition)) { collapsegroup (groupposition); } else { expandgroup (groupposition ); }Expand or close if clicked.
First analysis here, almost finished ...