How to adjust the drop-down refresh distance of swiperefreshlayout under Android programming

Source: Internet
Author: User

Swiperefreshlayout's drop-down refresh distance is shorter, and there is no API to set the drop-down distance, but look at Swiperefreshlayout source, you will find an internal variable mdistancetotriggersync , this variable determines the drop-down distance that triggers the refresh. The code below shows how to assign a value to the variable in the source:

Final Displaymetrics metrics =(int) math.min ((View) getParent ()). GetHeight () * Max_swipe_distance_ FACTOR, refresh_trigger_distance * metrics.density);

The above uses the parent view height and some constant calculations to determine the trigger distance. Max_swipe_distance_factor (0.6) and refresh_trigger_distance (120) are private constants that we cannot modify directly. However, we can recalculate the value of mdistancetotriggersync by means of reflection, the code is as follows:

Viewtreeobserver vto =swipelayout.getviewtreeobserver (); Vto.addongloballayoutlistener (NewViewtreeobserver.ongloballayoutlistener () { Public voidongloballayout () {FinalDisplaymetrics metrics =getresources (). Getdisplaymetrics (); Float Mdistancetotriggersync= Math.min ((View) swipelayout.getparent ()). GetHeight () * 0.6f, 500 *metrics.density); Try{Field field= Swiperefreshlayout.class. Getdeclaredfield ("Mdistancetotriggersync"); Field.setaccessible (true);        Field.setfloat (Swipelayout, Mdistancetotriggersync); } Catch(Exception e) {e.printstacktrace (); } viewtreeobserver Obs=Swipelayout.getviewtreeobserver (); Obs.removeongloballayoutlistener ( This); }});

Compare the code, just change the value of refresh_trigger_distance (120) to 500, you will find that the drop-down refresh distance significantly increased, another code download: Swiperefreshlayout.rar

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.