Android Slide and click Conflict Resolution

Source: Internet
Author: User
Tags gety


Reason

Recyclerview item enables sliding listening, allowing the user to swipe left and right to disappear and allow the user to click on the item. You need to overload the Ontouch and OnClick methods, but the time will be conflicting, onclick will consume events, resulting in the event will not be passed into the Ontouch. The solution is to move the onclick logic to Ontouch.

Judgment conditions

Reference 1 uses the time and move distance of two variables to ensure that the user really wants to click on the item. Satisfying this condition will invoke the logic in the onclick, otherwise the sliding logic will be invoked. Time control in the 1s, the idea is in the Action_down to remember a press button time, in the action_up to determine whether action_up to action_down the time required less than 1s The distance is measured from the Viewconfiguration class and is the data that Andrew recommends.

Specific code

private static final int max_click_duration = 1000;

private int mslop;

Private long pressstarttime;
private float Pressedx;
private float pressedy;

...

Viewconfiguration VC = Viewconfiguration.get (Builder.mRecyclerView.getContext ());
Mslop = Vc.getscaledtouchslop ();

...

Case Motionevent.action_down: {
Pressstarttime = System.currenttimemillis ();
Pressedx = Motionevent.getx ();
Pressedy = Motionevent.gety ();

...

Case MOTIONEVENT.ACTION_UP: {
Long pressduration = System.currenttimemillis ()-pressstarttime;
if (Pressduration < max_click_duration && distance (Pressedx, Pressedy, Motionevent.getx (), MOT Ionevent.gety ()) < Mslop) {
Mitemclickcallback.onclick (Mrecyclerview.getchildposition (Mdownview));
return true;
}

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.