Android Recyclerview Click, long by Event standard implementation: Based on Onitemtouchlistener + Gesturedetector

Source: Internet
Author: User

??

Android Recyclerview Click, long-press event: Based on Onitemtouchlistener + Gesturedetector standard implementation

Android Recyclerview has most of the features of the ListView, but Android Recyclerview does not implement a Click event like a ListView, a standard implementation of long-press events, the way I described in Appendix 1, A typical Recyclerview item Click event is implemented, The general idea is to monitor the click events by adding View.onclicklistener events to the Recyclerview Viewholder, which works well, but is less standard. Now gives a more standard Android official implementation.
Turning over the official documents of Android Recyclerview, this document does not provide a Onitem click event like the ListView, but notice there is a addonitemtouchlistener, according to the official document description, Addonitemtouchlistener is the lead entry for the Click event left in the official Android document, suggesting that the developer should implement the required logic code from the event listener interface. But if you pass the new object directly to the past, you need to resolve the complex processing of Android Touch event. Fortunately, Android official gave the Recyclerview.simpleonitemtouchlistener, as the name implies, is a simplified onitemtouchlistener. Although with Simpleonitemtouchlistener, this is not finished, because Simpleonitemtouchlistener is only the entrance of the event interception processing, the real thing to do is the protagonist: Gesturedetector. The Recyclerview-click, long-press event is finally implemented by Gesturedetector the hosted clicks, long-press events inside the block.

Full code:

Package Zhangphil.app;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.support.v7.widget.linearlayoutmanager;import Android.support.v7.widget.recyclerview;import Android.view.gesturedetector;import Android.view.motionevent;import Android.view.view;import Android.view.viewgroup;import Android.widget.textview;import Android.widget.toast;public class MainActivity extends    appcompatactivity {private Recyclerview mrecyclerview;    Private Gesturedetector Mgesturedetector;        Long press event private Onitemlongclicklistener Monitemlongclicklistener = new Onitemlongclicklistener () {@Override public void Onitemlongclick (int position, View Childview) {Toast.maketext (Getapplication (), "long press:" + Positio        N, Toast.length_short). Show ();    }    }; Click event Private Onitemclicklistener Monitemclicklistener = new Onitemclicklistener () {@Override public vo ID Onitemclick (int position, View childview) {Toast.makeText (Getapplication (), "click:" + position, Toast.length_short). Show ();    }    };        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Create a simple test recyclerview Mrecyclerview = (recyclerview) Findviewbyid (R.id.recyclerview);        Linearlayoutmanager LayoutManager = new Linearlayoutmanager (this);        Layoutmanager.setorientation (linearlayoutmanager.vertical);        Mrecyclerview.setlayoutmanager (LayoutManager);        Mrecyclerview.setadapter (New Itemadapter ());            Here is the key code for adding a tap, long press event Mrecyclerview.addonitemtouchlistener (new Recyclerview.simpleonitemtouchlistener () { @Override public boolean onintercepttouchevent (Recyclerview RV, motionevent e) {if (Mgesturedet                Ector.ontouchevent (e)) {return true;            } return false;        }        }); Mgesturedetector = nEW Gesturedetector (This, 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.g                    Ety ());                        if (Childview! = null) {int position = mrecyclerview.getchildlayoutposition (Childview);                    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);    }        }); }//Long press event interface public interface Onitemlongclicklistener {public void Onitemlongclick (int position, View Childvie    W);    }//Click event Interface public interface Onitemclicklistener {public void Onitemclick (int position, View childview);        } private class Itemviewholder extends Recyclerview.viewholder {public TextView text;            Public Itemviewholder (View Itemview) {super (Itemview); Text = (TextView) Itemview.findviewbyid (Android.        R.ID.TEXT1); }} Private class Itemadapter extends Recyclerview.adapter<itemviewholder> {@Override public It  Emviewholder Oncreateviewholder (viewgroup parent, int viewtype) {View view = View.inflate (Parent.getcontext (), Android.     R.layout.simple_list_item_1, NULL);       Itemviewholder holder = new Itemviewholder (view);        return holder; } @Override public void Onbindviewholder (itemviewholder holder, int position) {Holder.text.setTe        XT (String.valueof (position));        } @Override public int getitemcount () {return 99; }    }}


Because I intercepted the Recyclerview touch event inside the onintercepttouchevent, and through the Gesturedetector
Managed processing, which extends and further encapsulates more complex event-handling interfaces.


Appendix:
1, "Recyclerview Click event" Link: http://blog.csdn.net/zhangphil/article/details/46771865



Android Recyclerview Click, long by Event standard implementation: Based on Onitemtouchlistener + Gesturedetector

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.