??
Android Recyclerview Click, long-press event: Based on the Onitemtouchlistener +gesturedetector standard Implementation (ii), encapsulation is extracted into a generic tool class
I wrote the appendix in article 2, which describes how Android can implement click-and-hold events based on the Onitemtouchlistener +gesturedetector, since today Recyclerview is so prevalent in Android development, And Recyclerview Click events are so common, if you write a bunch of event listeners like in Appendix 2 to the Business logic code, how many times do you write the code? So I took the listener of adding a click event to Recyclerview and encapsulated it into a common toolbar class recyclerviewutil.
When adding a click event to a Recyclerview, just like the ListView event, simply write the following code:
The first step is to initialize the Recyclerviewutil, pass in the current Recyclerview object and the context pointer this:context recyclerviewutil util=new recyclerviewutil ( This,mrecyclerview); The second step, the direct set Util.setonitemclicklistener (new Recyclerviewutil.onitemclicklistener () { @Override public void Onitemclick (int position, view view) { //toast.maketext (Getapplicationcontext (), position+ "click", Toast.length_short). Show (); } });
Long-press events are similar:
First initialize the Recyclerviewutil, pass in the current Recyclerview object and the context pointer this:context recyclerviewutil util=new recyclerviewutil (this , Mrecyclerview); Util.setonitemlongclicklistener (New Recyclerviewutil.onitemlongclicklistener () { @Override public Void Onitemlongclick (int position, view view) { //toast.maketext (Getapplicationcontext (), position+ "Long Press", Toast.length _short). Show (); } });
Encapsulated, well-extracted universal Recyclerviewutil.java all code:
Package Zhangphil.app;import Android.content.context;import Android.support.v7.widget.recyclerview;import Android.view.gesturedetector;import android.view.motionevent;import android.view.view;/** * Created by Phil on 2016/11 /19. */public class Recyclerviewutil {private Recyclerview mrecyclerview = null; Private Gesturedetector mgesturedetector = null; Private Recyclerview.simpleonitemtouchlistener Msimpleonitemtouchlistener; Private Onitemclicklistener monitemclicklistener = null; Private Onitemlongclicklistener monitemlongclicklistener = null; Private context context; Public Recyclerviewutil (context context, Recyclerview Recyclerview) {this.context = context; This.mrecyclerview = Recyclerview; Mgesturedetector = new Gesturedetector (context, new Gesturedetector.simpleongesturelistener () {//Long press Event @Override public void onlongpress (Motionevent e) {super.onlongpress (e); if (monitemloNgclicklistener! = null) {View Childview = Mrecyclerview.findchildviewunder (E.getx (), e.gety ()); if (Childview! = null) {int position = Mrecyclerview.getchildlayoutposition (childvi EW); Monitemlongclicklistener.onitemlongclick (position, childview); }}}//Click event @Override Public boolean onsingletapup (motionevent e) {if (Monitemclicklistener! = null) {View Childview = Mrecyclerview.findchildviewun Der (E.getx (), e.gety ()); if (Childview! = null) {int position = mrecyclerview.getchildlayoutposition (Childview); Monitemclicklistener.onitemclick (position, childview); return true; }} return Super.onsingletapup (e); } }); MsimpleonitemtouchListener = new Recyclerview.simpleonitemtouchlistener () {@Override public boolean Onintercepttouche Vent (Recyclerview RV, motionevent e) {if (Mgesturedetector.ontouchevent (e)) {return TR Ue } return false; } }; Mrecyclerview.addonitemtouchlistener (Msimpleonitemtouchlistener); } public void Setonitemclicklistener (Onitemclicklistener l) {monitemclicklistener = l; } public void Setonitemlongclicklistener (Onitemlongclicklistener l) {monitemlongclicklistener = l; }//Long press event interface public interface Onitemlongclicklistener {public void Onitemlongclick (int position, view view); }//Click event Interface public interface Onitemclicklistener {public void Onitemclick (int position, view view); }}
Use the time to copy the Recyclerviewutil.java directly into your project code project.
Appendix:
1, "Recyclerview Click event" Link: http://blog.csdn.net/zhangphil/article/details/46771865
2, "Android Recyclerview click, long by Event standard implementation: Based on Onitemtouchlistener + gesturedetector": http://blog.csdn.net/zhangphil/ article/details/53230157
Android Recyclerview Click, long-press event: Based on the Onitemtouchlistener +gesturedetector standard Implementation (ii), encapsulation is extracted into a generic tool class