Open source China Oschina Android Client Source Analysis (3) can drag the ScrollView

Source: Internet
Author: User

The view layout of the Oschina Client slide menu uses a scrollview that can be dragged, and the class file is Customerscrollview.

1 We need to analyze why we use ScrollView? Used in fact it is easy to understand to avoid its internal sub-view layout is large, on smaller devices can not be fully displayed.

2 realize the drag-and-drop effect, just from the user experience point of view, next we detailed analysis of its custom ScrollView.

2.1 The target of the drag is the layout view of the menu within the ScrollView, so the onfinishinflate () function within the Customerscrollview is first passed through Getchildat (0) To get the view of the menu layout, which is the goal of the first step is to get the object to be dragged. onfinishinflate () load the view, the view here refers to the source code <include layout= "@layout/fragment_navigation_drawer_items"/ > load the layout file.

2.2 The drag process is actually a "press-move-lift" process, so to rewrite ontouchevent (motionevent ev), where the move is actually the menu view according to the direction and distance of movement, how to achieve this function? The most critical thing in the source code is this line.

Inner.layout (Inner.getleft (), YY, Inner.getright (), Inner.getbottom ()-DeltaY); This method four parameters are inner relative to their parent control ScrollView the coordinates of the original point. Do not know very well, you can specifically check the relevant knowledge of the coordinates.

2.3 When the finger is lifted and the Motionevent.action_up event occurs, the drag-and-drop view recovery is moved to its original position, and the movement is attached with an animation, because the movement is actually a position change, so the use of translateanimation, Because it is up and down, the starting and ending coordinates of X are both the starting and ending coordinates of the 0,y. As for why so write, I believe that after reading the blog should be understood. Then the problem comes, to automatically move back, then the timing of the trigger in the motionevent.action_up, the original position how to save, because the move requires the left upper and lower right four parameters, so in In Customerscrollview we see such a variable private rect normal = new rect ();

Normal.set (Inner.getleft (), Inner.gettop (), Inner.getright (), Inner.getbottom ()); Method records the initialization position of the menu view.

2.4 In the source code This function is also worth our analysis:

Whether you need to move

public Boolean isneedmove () {

int offset = Inner.getmeasuredheight ()-getheight ();

int scrolly = getscrolly ();

if (scrolly = = 0 | | scrolly = = offset) {

return true;

}

return false;

}

After careful examination found that scrolly = = 0 The condition is actually scrolled to the top of the time, while scrolly = = Offset is scrolled to the bottom of the time, two conditions satisfy one can achieve drag and drop effect. int offset = inner.getmeasuredheight ()-getheight (), which is equal to the height minus the actual height you can see is the part of the body that you don't see.

2.5 Why is the isneedanimation () function needed in the source code? Because Inner.layout (Normal.left, Normal.top, Normal.right, Normal.bottom) are also used to restore to the original location, the normal must first have four parameter values. this normal is only valued if it satisfies the conditions in 2.4.

2.6 Why do you want to put this normal.setempty () after the drag-and-drop has returned to its original position? What is the intention of it? Think carefully, found that the normal set of the upper left and right bottom four values, is satisfied with 2.4 of the two conditions will have a specific value. So there are two different kinds of rect for this normal. Top left and right bottom four values (0,0, the actual menu width 240DP, the actual measurement height of the menu) and scroll to the bottom of the upper left and right bottom four values respectively (0, negative " The actual height of the menu minus the height of the screen ", the actual menu width 240dp, screen height ), so need to be emptied.





Open source China Oschina Android Client Source Analysis (3) can drag the ScrollView

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.