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;
}