As mentioned in the previous Android Recylerview introductory tutorial, Recyclerview is no longer responsible for the layout and display of item views, so Recyclerview is not open for item Onitemclick and other click events, This needs to be implemented by the developer themselves. The bottom of the blog is the demo program to run the animation.
The GitHub link to the demo is served.
In the investigation process, found that there are students to modify the Recyclerview source code to achieve the click of the item, but that this is not an elegant solution, finally 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 up a separate OnClick listener for the root layout and open it to adapter, That can not be assembled when the Recyclerview can be set Itemclicklistener, but this listener is not set to Recyclerview but set to adapter.
Let's first look at the Viewholder code:
public class Myviewholder extends Viewholder implements Onclicklistener,
onlongclicklistener{Public ImageView IV;
Public TextView TV;
Private Myitemclicklistener Mlistener;
Private Myitemlongclicklistener Mlongclicklistener; Public Myviewholder (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); /** * Click on Monitor/@Override public void OnClick (View v) {if (Mlistener!= null) {Mlistener.onitemclick v,getposition (
)); /** * Long Press Monitor/@Override public boolean Onlongclick (View arg0) {if (Mlongclicklistener!= null) {Mlongclicklist
Ener.onitemlongclick (arg0, GetPosition ());
return true; }}</span>
Because when constructing viewholder, Rootview will be passed in as a required parameter, so we just need to get the rootview and give it a fixed point to click the Listener event.
The next thing to consider is how to pass the listener in. The demo set the Interface:myitemclicklistener of the Listener click event:
Public interface Myitemclicklistener {public
void Onitemclick (View view,int postion);
}
Myitemclicklistener imitates ListView's Onitemclicklistener and opens up view and position two parameters, which are handy for developers accustomed to using ListView. As you can see from Viewholder code, executing the OnClick method invokes GetPosition () to callback the current item's position to listener. GetPosition () is a Viewholder built-in method that can be used directly.
As mentioned above, listener is set to adapter, so adapter need to open up the relevant methods:
@Override public
Myviewholder Oncreateviewholder (viewgroup parent, int viewtype) {
View Itemview = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.item, parent,false);
Myviewholder VH = new Myviewholder (itemview,mitemclicklistener,mitemlongclicklistener);
return vh;
}
/**
* Set Item click Monitor
* @param listener
*
/public void Setonitemclicklistener (Myitemclicklistener Listener) {
This.mitemclicklistener = listener;
}
public void Setonitemlongclicklistener (Myitemlongclicklistener listener) {
This.mitemlongclicklistener = Listener;
}
Last blog (Android-recylerview) mentioned that adapter's Oncreateviewholder is responsible for instantiating each item's view, so I pass listener to Viewholder when I instantiate the view.
Finally, the assembly of Recyclerview according to the requirements of the Click to monitor the set:
/**
* Initialize Recylerview/
private void Initview () {
Mrecyclerview = (recyclerview) Findviewbyid ( R.id.recyclerview);
Mylayoutmanager manager = new Mylayoutmanager (this);
Manager.setorientation (linearlayout.horizontal);//default is Linearlayout.vertical
Mrecyclerview.setlayoutmanager ( Manager);
Mrecyclerview.setitemanimator (New Defaultitemanimator ());
}
private void InitData () {
this.mdata = new arraylist<myitembean> ();
for (int i=0;i<20;i++) {
Myitembean bean = new Myitembean ();
bean.tv = "Xmy" +i;
Mdata.add (bean);
This.madapter = new Myadapter (mdata);
This.mRecyclerView.setAdapter (madapter);
Recyclerview.itemdecoration decoration = new Mydecoration (this);
This.mRecyclerView.addItemDecoration (decoration);
This.mAdapter.setOnItemClickListener (this);
This.mAdapter.setOnItemLongClickListener (this);
Demo for Viewholder set onclick and Onlongclicklistener, in the activity we implemented the interface method and print inside toast hint:
@Override public
void Onitemclick (view view, int postion) {
Myitembean bean = Mdata.get (postion);
if (bean!= null) {
Toast.maketext (this, bean.tv, Toast.length_short). Show ();
@Override public
void Onitemlongclick (view view, int postion) {
Myitembean bean = Mdata.get (postion);
if (bean!= null) {
Toast.maketext (this, "Longclick" +bean.tv, Toast.length_short). Show ();
Here is the demo run animation.
The above is a small set to introduce the Android Recyclerview click Item Settings Event, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!