Android, use the easiest way to achieve pull-up refresh pull-down load more

Source: Internet
Author: User
Tags gety

<!--Description: Pull-up refresh, pull-down load more is now the most popular gesture operation, but for beginners, it is difficult to achieve,Many tutorials on the internet are too complex, for beginners can not play a guiding role, hereby write this article, to help the novice Android to understand this, and the most important point: This article only helps you understand, not want you to become a code Porter! Don't be frightened by so much code, many of which are comments, A closer look at the notes is a great help to your understanding. Author:booker ldate:2014-05-16-->, prepared: To achieve this function, the most basic need two things, one is Ontouchlistener, One is Onscrolllistener, you have to understand these two things. 1,ontouchlistener: Used to monitor touch events, when a finger touches the screen, and when sliding, it is necessary to use the listener to handle the corresponding operation 2, Onscrolllistener: A scrolling event used to listen to a ListView (not necessarily a ListView, all controls that support that listener), when scrolling to the first or last line requires judgment, which will have three parameters, followed by the word two, let's begin: Start by customizing the two listener and implementing the corresponding interface, and set two variables in the class that can access the public variable to determine the current scrolling value and state: 1,class TestClass extends activity{// Set two variables in the class that can access the public variable to determine whether the current scroll value and state private Boolean isfirstrow;//roll to the first row of private Boolean islastrow, or whether to roll to the last row ...} Next write two custom classes to implement the related interface://Scrolling Listener class 2.1,class Myscrolllistener implements onscrolllistener{/** when scrolling is triggered by the method * firstvisiableitem--the first item subscript that can be seen in the current list, which is the total number of item currently being scrolled to the front of the *visiableitemcount--currently visible * totalitemcount--Total Item quantity */@Overridepublic void onscroll (Abslistview arg0, int firstvisiableitem, int Visiableitemcount, int totalitemcount) {//To determine whether the current scroll to the first line (based on the explanation of the pre-facing parameters I believe you can understand) if (firstvisiableitem==0&& totalitemcount>0) {isfirstrow=true;} else{//is not the first line of Isfirstrow=false;}  Decide whether to scroll to the last line if (firstvisiableitem + visiableitemcount = = Totalitemcount && totalitemcount > 0) {islastrow = true;} Else{islastrow=false;}} /** the state of the change that is triggered when the rolling state changes *scrollstate--, in total: scroll_state_idle (scrolling, and the finger has left the screen), scroll_state_fling (when the finger is scrolling quickly), Scroll_state_touch_scroll (finger scrolling, but finger still on screen) */@Overridepublic void onscrollstatechanged (Abslistview arg0, int Scrollstate) {//The use of this method depends on the situation, there is no need to use the}}//touch listening class 2.2,class Mytouchlistener implements ontouchlistener{/** Where motionevent is an important parameter, it provides all the parameters and methods of the touch event */@Overridepublic Boolean OnTouch (View V, motionevent event) {/* Here are a few things to do: 1, When a down event occurs, the event's y-coordinate 2 is recorded, and when the move event occurs, the y-coordinate of the movement is judged to be able to reach the pull-down or pull-up event 3, and when the Up event occurs, the corresponding event is triggered if the pull-up or pull-down event is reached/*// You need to set the following variables//To get the display related content displaymetrics DM =getresources (). Getdisplaymetrics ();//Gets the DPI value so that the moving pixel is calculated more accurately float density= dm.densitydpi/100;//gets the layout parameters and uses it to achieve the visual drag effect linearlayout.layoutparams LP = new Linearlayout.layoutparams ( LinearLayout.LayoutParams.WRAP_CONTENT, lineArLayout.LayoutParams.WRAP_CONTENT);///Set a variable to record the original y-coordinate in private float or_y=0f;//event call getaction () to get what event is currently triggered, Here we're going to use the Down,move,up these three events switch (event.getaction ()) {case motionevent.action_down://takes place the original y-coordinate of the down event record, the GetY () method can be overloaded, Used to judge multi-touch, this does not need to judge Or_y=event.gety (); Break;case motionevent.action_move://When the gesture moves, get the current y-coordinate and compare it to the previous coordinates to determine whether it is up or down/ Set the variable to get the moving value, the upper bracket is positive, pull down negative int y_dis= (int) (Or_y-event.gety ()),//Determine whether the drag condition is reached if (isfirstrow&&y_dis<0) {// In the first line and continues to pull//get the absolute value of the moving distance and calculate the relative distance of its movement, float distance=math.abs (Y_DIS/DENSITY/2); lp.topmargin= (int) distance;// Creates a pull-down effect where the ListView is the corresponding control (not necessarily a ListView) Listview.setlayoutparams (LP);} else if (islastrow&&y_dis>0) {//is in the last line and continues to pull online//the same as float distance=math.abs (Y_DIS/DENSITY/2); lp.bottommargin= (int) distance;listview.setlayoutparams (LP);} Break;case motionevent.action_up://When an up event occurs, the event is triggered if the condition is reached, which triggers the corresponding event if (Isfirstrow) {lp.topmargin=0;// Call the interface method that gets the refresh data getnewest ();} else if (islastrow) {lp.bottommargin=0;//calls the interface method that gets more data Getmore ();} LISTVIEW.SETLAYOUTPARAMS (LP); Break;}}} Finally in LiSet the corresponding listener in Stview: 3,oncreate () {//Get control listview= (ListView) R.findviewbyid (r.id.xxx);// Set the corresponding listener to Listview.setontouchlistener (new Mytouchlistener ()); Listview.setonscrolllistener (new Myscrolllistener ());} Done~and ENJOY

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.