"Android Interface Effect 48" Android-recyclerview-item Click event Settings

Source: Internet
Author: User

As mentioned in the previous blog Android-recylerview , Recyclerview is no longer responsible for the layout and display of the item view, so Recyclerview does not have a click event for item Open Onitemclick, This needs to be implemented by the developer themselves. Blog at the bottom of the demo program to run the animation.

On the demo's GitHub link .

In the investigation process, found that the classmate modified Recyclerview source to achieve the item's click Monitoring, but that this is not an elegant solution, and ultimately decided to make a fuss on the recyclerview.viewholder.

The idea is: because Viewholder we can get the root layout of each item, so if we set a separate OnClick listener for the root layout and open it to adapter, that's not You can set the Itemclicklistener when you assemble the Recyclerview, but this listener is not set to Recyclerview but set to adapter.

We first look at the Viewholder code:

 Public classMyviewholderextendsViewholderImplementsonclicklistener,onlongclicklistener{ PublicImageView IV;  PublicTextView TV; PrivateMyitemclicklistener Mlistener; PrivateMyitemlongclicklistener Mlongclicklistener;  PublicMyviewholder (View rootview,myitemclicklistener listener,myitemlongclicklistener longclicklistener) {Super(Rootview); IV=(ImageView) Rootview.findviewbyid (R.ID.ITEM_IV); TV=(TextView) Rootview.findviewbyid (R.ID.ITEM_TV);  This. Mlistener =Listener;  This. Mlongclicklistener =Longclicklistener; Rootview.setonclicklistener ( This); Rootview.setonlongclicklistener ( This); }        /*** Tap to listen*/@Override Public voidOnClick (View v) {if(Mlistener! =NULL) {Mlistener.onitemclick (v,getposition ()); }      }        /*** Long Press to monitor*/@Override Public BooleanOnlongclick (View arg0) {if(Mlongclicklistener! =NULL) {Mlongclicklistener.onitemlongclick (arg0, GetPosition ()); }          return true; }    }

Because when constructing the Viewholder, Rootview will be passed in as a required parameter, so we just need to get the rootview and give it a point-and-click Listener event.

The next thing to consider is how to pass the listener in. The demo sets the Interface:myitemclicklistener of the listen-to-click event:

     Public Interface Myitemclicklistener {          publicvoid onitemclick (view view,int  postion);      }  

Myitemclicklistener mimics the Onitemclicklistener of the ListView, opening the two parameters of view and position, which is more handy for developers who are accustomed to using the ListView. As you can see from the code in Viewholder, when the OnClick method is executed, GetPosition () is called to callback the position of the current item to listener. GetPosition () is a built-in method of Viewholder and can be used directly.

As mentioned above, listener is set to adapter, so adapter need to open up relevant methods:

@Override PublicMyviewholder Oncreateviewholder (viewgroup parent,intViewType) {View Itemview= Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.item, parent,false); Myviewholder VH=NewMyviewholder (Itemview,mitemclicklistener,mitemlongclicklistener); returnVH; }                /*** Set Item click to listen *@paramListener*/           Public voidSetonitemclicklistener (Myitemclicklistener listener) { This. Mitemclicklistener =Listener; }                     Public voidSetonitemlongclicklistener (Myitemlongclicklistener listener) { This. Mitemlongclicklistener =Listener; }  

As mentioned in the previous blog (Android-recylerview ), adapter's Oncreateviewholder is responsible for instantiating the view of each item, So I pass the listener to Viewholder when I instantiate the view.

Finally, the Assembly of Recyclerview when the need to set the click Monitoring:

        /*** Initialize Recylerview*/          Private voidInitview () {Mrecyclerview=(Recyclerview) Findviewbyid (R.id.recyclerview); Mylayoutmanager Manager=NewMylayoutmanager ( This); Manager.setorientation (linearlayout.horizontal);//default is Linearlayout.verticalMrecyclerview.setlayoutmanager (manager); Mrecyclerview.setitemanimator (Newdefaultitemanimator ()); }                    Private voidInitData () { This. Mdata =NewArraylist<myitembean>();  for(inti=0;i<20;i++) {Myitembean Bean=NewMyitembean (); Bean.tv= "Xmy" +i;              Mdata.add (Bean); }               This. Madapter =NewMyadapter (Mdata);  This. Mrecyclerview.setadapter (Madapter); Recyclerview.itemdecoration Decoration=NewMydecoration ( This);  This. mrecyclerview.additemdecoration (decoration);  This. Madapter.setonitemclicklistener ( This);  This. Madapter.setonitemlongclicklistener ( This); }  

The demo set the onclick and Onlongclicklistener for Viewholder, and in the activity we implemented the interface method and printed the toast prompt inside:

@Override Public voidOnitemclick (View view,intpostion) {Myitembean Bean=Mdata.get (postion); if(Bean! =NULL) {Toast.maketext ( This, Bean.tv, Toast.length_short). Show (); }} @Override Public voidOnitemlongclick (View view,intpostion) {Myitembean Bean=Mdata.get (postion); if(Bean! =NULL) {Toast.maketext ( This, "Longclick" +bean.tv, Toast.length_short). Show (); }      } 

Here is the demo run animation.




"Android Interface Effect 48" Android-recyclerview-item Click event Settings

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.