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 previous Article layout manager code, based on the addition of event monitoring.
Let's say we use Viewholder to handle events indirectly, assuming we're going to do the event click events. 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; }
Finally, a three-parameter constructor 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; } }); }
Careful look, in fact the code is quite a lot of, 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 parameters can pass a null value, which means the listener is not set.
Perform such as the following.
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 be.
Well, look at the code, the details in the gaze.
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 was 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 (); } }));
It is obvious that another way to understand the adapter is to decouple it. 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 folder 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 under the project's core folder. I directly copy the code under the folder into my project's package, delete an unrelated class, use it directly, and of course I need to copy a resource file is Ids.xml
The actual execution effect, is the same as the above two ways, then its implementation by what difference, in fact, and the other way there is no big difference, basically is onitemtouchlistener plus gesture to achieve, only its logic may be more rigorous, design more excellent. In addition, the folder under another Itemselectionsupport class, this class provides the item to choose the function, provides the single choice multi-choice method, however, when I test the bug found that there is, so, the use of this class is skipped.
At this point, Recyclerview's Click event is finished.
Source code Download
http://download.csdn.net/detail/sbsujjbcy/8495337
Android V7 Compatibility Pack Recyclerview use (iv)--click event Handling in different ways