top three articles
use of the Android V7 Compatibility Pack Recyclerview (iii)--use of the layout manager
use of the Android V7 Compatibility Pack Recyclerview (ii)
use of the Android V7 Compatibility Pack Recyclerview (i)
This paper introduces the use of Recyclerview and the flexibility of common related classes and layout managers. Write so many articles, not related to user interaction, then how to handle the Click event.
In Recyclerview you will be surprised to find that there is no Onitemclicklistener listener in this class to listen to our click events, nor does the Onitemlongclicklistener listener listen to our long-press events. Instead of the Onitemtouchlistener listener, how do we achieve our click events and long-press events?
Our code is based on the code of the previous article layout Manager and adds event snooping based on it.
If we let ourselves come up with event Click events, we would use Viewholder to handle events indirectly, for example. First, add the listener interface to the adapter.
interface OnItemClickListener { void onClick(View v); } interface OnItemLongClickListener { void onLongClick(View v); } private OnItemClickListener onClickListener; private OnItemLongClickListener onLongClickListener;
The constructor is then overloaded so that it can receive listener instances
public Cardviewadapter (string[] data) {this (data, nu ll , null ); } public cardviewadapter (string[] data, Onitemclicklistener onclicklistener) {this (data, Onclicklistener, null ); } public cardviewadapter (string[] data, Onitemclicklistener Onclicklistener, Onitemlongclicklistener onlongclicklistener) {this . Data = data; this . Onclicklistener = Onclicklistener; this . Onlongclicklistener = Onlongclicklistener; }
The constructor of the three arguments is called, and the assignment is completed within the constructor.
The original Viewholder constructor is transformed to handle the click event, and of course the Click event can be handled directly in the Oncreateviewholder function.
Public Viewholder(View Itemlayoutview,FinalOnitemclicklistener Onclicklistener,FinalOnitemlongclicklistener Onlongclicklistener) {Super(Itemlayoutview); info = (TextView) Itemlayoutview.findviewbyid (R.id.info_text); Itemlayoutview.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View v) {//When the listener is not empty, make a callback if(Onclicklistener! =NULL) {Onclicklistener.onclick (v); } } }); Itemlayoutview.setonlongclicklistener (NewOnlongclicklistener () {@Override Public Boolean Onlongclick(View v) {//When the listener is not empty, make a callback if(Onlongclicklistener! =NULL) {Onlongclicklistener.onlongclick (v); }//Returns TRUE, consumes the event and prevents it from continuing to pass return true; } }); }
Look carefully, in fact, the code is quite a bit, then let us call.
Madapter =NewCardviewadapter (data,NewOnitemclicklistener () {@Override Public void OnClick(View v) {TextView info = (TextView) V.findviewbyid (R.id.info_text); Toast.maketext (Getapplicationcontext (),"click"+info.gettext (), Toast.length_long). Show (); } },NewOnitemlongclicklistener () {@Override Public void Onlongclick(View v) {TextView info = (TextView) V.findviewbyid (R.id.info_text); Toast.maketext (Getapplicationcontext (),"Long Press"+info.gettext (), Toast.length_long). Show (); } });
The next two arguments can pass null values, which means that the listener is not set.
Run as follows.
We will find that the above code coupling is still a bit high, the event is directly coupled with the adapter, in addition, we should have a better way to deal with this click event. Yes, there is not a Onitemtouchlistener listener, and then with the gesture can not do it.
Well, look at the code, explain it in detail in the comments.
PackageCn.edu.zafu.layoutmanager;ImportAndroid.content.Context;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.view.GestureDetector;ImportAndroid.view.MotionEvent;ImportAndroid.view.View;/** * Listener, implement Onitemtouchlistener interface * * @author lizhangqu * * 2015-3-12 * * Public class Recycleritemclicklistener implementsRecyclerview. Onitemtouchlistener { PrivateOnitemclicklistener Mlistener;PrivateGesturedetector Mgesturedetector;//Click Callback Public interface onitemclicklistener { Public void Onitemclick(View view,intposition); Public void Onitemlongclick(View view,intposition); } Public Recycleritemclicklistener(Context context,FinalRecyclerview Recyclerview, Onitemclicklistener listener) {Mlistener = listener;//Identify and process gesturesMgesturedetector =NewGesturedetector (Context,NewGesturedetector.simpleongesturelistener () {@Override Public Boolean Onsingletapup(Motionevent e) {//After tapping on the touchscreen, the bounce must return TRUE or the Click cannot be triggered return true; }@Override Public void onlongpress(Motionevent e) {//Long Press //Based on Findchildviewunder (float x, float y) to figure out which item is selectedView Childview = Recyclerview.findchildviewunder (E.getx (), e.gety ());//have item selected and listener NOT NULL trigger long press event if(Childview! =NULL&& Mlistener! =NULL) {Mlistener.onitemlongclick (Childview, Recyclerview.getchi Ldposition (Childview)); } } }); }@Override Public Boolean onintercepttouchevent(Recyclerview view, motionevent e) {View Childview = View.findchildviewunder (E.getx (), e.gety ());if(Childview! =NULL&& Mlistener! =NULL&& mgesturedetector.ontouchevent (e)) {//Trigger Click eventMlistener.onitemclick (Childview, View.getchildposition (Childview));return true; }return false; }@Override Public void ontouchevent(Recyclerview view, motionevent motionevent) { }}
Called in activity
Mrecyclerview.addonitemtouchlistener (NewRecycleritemclicklistener (Getapplicationcontext (), Mrecyclerview,NewOnitemclicklistener () {@Override Public void Onitemlongclick(View view,intPosition) {Toast.maketext (), Getapplicationcontext (),"Long Press"+data[position], Toast.length_short). Show (); }@Override Public void Onitemclick(View view,intPosition) {Toast.maketext (), Getapplicationcontext (),"Short Press"+data[position], Toast.length_short). Show (); } }));
Clearly, the second approach is decoupled from the adapter. Should say better than the first method.
Then there is no way to handle the click event. Let's search from the almighty GitHub.
Https://github.com/lucasr/twoway-view
In the sample directory of the project there is a class with such a code
FinalItemclicksupport ItemClick = itemclicksupport.addto (Mrecyclerview); Itemclick.setonitemclicklistener (NewOnitemclicklistener () {@Override Public void Onitemclick(Recyclerview Parent, View Child,intPositionLongID) {Mtoast.settext ("Item clicked:"+ position); Mtoast.show (); } }); Itemclick.setonitemlongclicklistener (NewOnitemlongclicklistener () {@Override Public Boolean Onitemlongclick(Recyclerview Parent, View Child,intPositionLongID) {Mtoast.settext ("Item long pressed:"+ position); Mtoast.show ();return true; } });
The code above has a Itemclicksupport class, right, which is the support for providing events. So where is the class, in fact it's in the core directory of the project. I directly copy the code in this directory into my project's package, delete an unrelated class, directly use it, and of course I need to copy a resource file is Ids.xml
The actual running effect, is the same as the above two ways, then its implementation by what difference, in fact, and the second way there is no big difference, basically is onitemtouchlistener plus gesture to achieve, but its logic may be more rigorous, design more excellent. In addition, there is a Itemselectionsupport class in this directory, which provides the function of item selection, provides a single-choice multi-selection method, however, when I test found that there is a bug, so, the use of this class has been skipped.
At this point, Recyclerview's Click event is finished.
SOURCE download
http://download.csdn.net/detail/sbsujjbcy/8495337
Android V7 Compatibility Pack Recyclerview use (iv)--click event Handling in different ways