Home>

In the previous postAndroid recylerview entry tutorialIt was mentioned that recyclerview is no longer responsible for the layout and display of the item view,Therefore, recyclerview does not open onitemclick and other click events for items.This requires developers to implement it themselves.At the bottom of the blog is a demo program to run the animation.

Senddemo github link.

During the research process,Found that some students modified the recyclerview source code to implement click monitoring for items.But think this is not an elegant solution,Finally decided to make an article on recyclerview.viewholder.

The idea is:because the 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 the adapter, then we can not set the itemclicklistener when we assemble the recyclerview, but this listener is not set to the recyclerview but to the adapter.

We first look at the code of the viewholder:

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 to listen
* /
@override
public void onclick (view v) {
if (mlistener!=null) {
mlistener.onitemclick (v, getposition ());
}
}
/**
* Long press to monitor
* /
@override
public boolean onlongclick (view arg0) {
if (mlongclicklistener!=null) {
mlongclicklistener.onitemlongclick (arg0, getposition ());
}
return true;
}
}</span>

Because when constructing the viewholder, the rootview will be passed in as a required parameter.So we just need to get the rootview and bind it to the click listen event.

The next thing to consider is how to pass the listener in.The interface for monitoring click events is set in the demo:myitemclicklistener:

public interface myitemclicklistener {
public void onitemclick (view view, int postion);
}

myitemclicklistener imitates listview's onitemclicklistener, and opens the two parameters of view and position.This is more handy for developers who are used to using listview.As you can see from the viewholder code,When the onclick method is executed, getposition () is called to callback the position of the current item to the listener. getposition () is a built-in method of viewholder,Can be used directly.

As mentioned above,The listener is set on the adapter, so the adapter needs to open the relevant methods to the outside world:

@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 listener
* @param listener
* /
public void setonitemclicklistener (myitemclicklistener listener) {
this.mitemclicklistener=listener;
}
public void setonitemlongclicklistener (myitemlongclicklistener listener) {
this.mitemlongclicklistener=listener;
}

As mentioned in the previous blog (android-recylerview), the adapter's oncreateviewholder is responsible for instantiating the view of each item, so I passed the listener to the viewholder when instantiating the view.

The last is to set the click listener according to the requirements when assembling the recyclerview:

/**
* Initial recylerview
* /
private void initview () {
mrecyclerview=(recyclerview) findviewbyid (r.id.recyclerview);
mylayoutmanager manager=new mylayoutmanager (this);
manager.setorientation (linearlayout.horizontal);//The 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);
}

The demo sets onclick and onlongclicklistener for the viewholder. In the activity, we implement the interface method and print the toast prompt in it:

@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 ();
}
}

Below is the running animation of the demo.

  • Previous Php use get_class_methods () function to get classification methods
  • Next AngularJS basic ng-mouseenter instruction sample code