Android Recyclerview Click, long-press event: Based on the Onitemtouchlistener +gesturedetector standard Implementation (ii), encapsulation is extracted into a generic tool class

Source: Internet
Author: User
Tags gety

??

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

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.