Android difference common touch method and scroll

Source: Internet
Author: User

Today want to achieve this function, but just use the existing ontouchevent and gesturedetector feel to do some tangled, the original seems to have tried, the final process a bit messy, not good maintenance, then use the Android programmer the biggest advantage-source code.     First think of the ListView support both click and Support drag, to see the source, the first to find the breakthrough is:    android.view.viewtreeobserver.ontouchmodechangelistener    Only one method is defined;    public void Ontouchmodechanged (Boolean isintouchmode);    Then I looked at viewtreeobserver and found that there was no relevant content except add, It then finds the parent class Android.widget.AbsListView of the ListView, which implements the interface. This is more complicated is not introduced in detail, combined with ontouchevent and ontouchmodechanged can see its implementation mechanism, but the general reading did not find a good solution to the problem, as soon as possible, and want to resolve, At this time think of the gesture recognition of the class android.view.GestureDetector, in fact, see its ontouchevent to be more relaxed, with the implementation of the previous mechanism are very similar, but relatively simple, but it is not possible to find a direct solution to the problem, but understand the Gesturedetecto The event distribution mechanism and situation of R, Ongesturelistener will be much clearer. After the use of the most basic implementation mechanism to simulate the processing of two events, here said that the simulation is actually not really completely differentiated, about this aspect is more complex, still not clear, should look into the ListView will have a concrete implementation. OK, directly paste the code, the above several classes can help solve this problem, but also can be in-depth study, the specific time and did not use the handler, if you want to respond to more methods when this mechanism is very useful, easy to expand. The following direct display of the code, but also relatively simple, it is not too much to explain, here is understood as sliding 20 like Socai think it is a sliding event to simulate, but this time it looks more clear, just record it.  java code         //--------------------------------------------------------------------------- //touch screen and swipe (only to distinguish between sliding and touch screen, if you need to reconsider logic processing when adding double-tap time)  //---------------------------- -----------------------------------------------   private Gesturedetector Gesturedetector;        //gesture recognition    //indicates no gesture event  private final int touch_mode_rest =-1;   //indicates touching the screen  private final int touch_mode_down = 0;  //indicates that a scroll has occurred, but it is still necessary to confirm whether it is a sliding event  private final int touch_mode_scrollcheck = 1;     //is currently sliding state  private final int touch_mode_scroll = 2;    private int touchmode;        //current touch status, using Ondown, Onscroll, and actionup to differentiate between sliding and touchscreen  private int tempoffsetx = 0;  //defined as scroll before you need to store offsets    Java code:
    1. @Override
    2. public boolean ontouchevent (Motionevent event) {
    3. if (Event.getaction () ==motionevent.action_up) {
    4. try {
    5. Return Handleactionup (event);
    6. } catch (Inaccuratescrollactionhandleexception e) {
    7. TODO auto-generated Catch block
    8. LOG.E ("Digitbusscreen.ontouchevent", "Use handleactionup wrong place");
    9. return false;
    10. }
    11. }
    12. Return Gesturedetector.ontouchevent (event);
    13. }
Java code:
  1. @Override
  2. public boolean Ondown (Motionevent arg0) {
  3. TODO auto-generated Method Stub
  4. Touchmode = Touch_mode_down;
  5. return true;
  6. }
  7. @Override
  8. public boolean onscroll (Motionevent E1, motionevent E2, float Distancex,
  9. Float Distancey) {
  10. TODO auto-generated Method Stub
  11. Switch (touchmode) {
  12. Case Touch_mode_down:
  13. Case Touch_mode_scrollcheck:
  14. if (Math.Abs (Tempoffsetx+distancex) <20) {
  15. Tempoffsetx + = Distancex;
  16. Touchmode = Touch_mode_scrollcheck;
  17. }else{
  18. Touchmode = Touch_mode_scroll;
  19. Tempoffsetx = 0;
  20. }
  21. Break
  22. Case Touch_mode_scroll:
  23. if (distancex!=0 && (offsetx-distancex<=0)
  24. && (offsetx-distancex>=-(offsetx+ (Actuallengthbetweenstation+positions.getstationxlength ()) * Numberofstation)) {
  25. for (Sprite sprite:sprites) {
  26. if (Sprite instanceof Scrollsprite)
  27. ((Scrollsprite) sprite). Scrollchangeoffset (-distancex, 0);
  28. }
  29. OffsetX + =-distancex;
  30. }
  31. Break
  32. }
  33. return true;
  34. }
  35. /**
  36. * Handling Action_up's Motionevent
  37. * @param Event
  38. * @return
  39. * @throws inaccuratescrollactionhandleexception
  40. */
  41. Private Boolean Handleactionup (Motionevent event) throws inaccuratescrollactionhandleexception{
  42. if (Event.getaction ()!=motionevent.action_up)
  43. throw new Inaccuratescrollactionhandleexception ();
  44. Switch (touchmode) {
  45. Case Touch_mode_scrollcheck:
  46. Tempoffsetx = 0;
  47. Case Touch_mode_down:
  48. Super.ontouchevent (event);
  49. Break
  50. }
  51. Touchmode = Touch_mode_rest;
  52. return true;
  53. }

Android difference common touch method and scroll

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.