Skip to content
dexafree edited this page Aug 21, 2014 · 3 revisions

This annotation will trigger an event when an AbsListView (ListView, GridView...) item is selected.

##Annotation arguments @OnItemSelected uses an extra "method" argument to specify the listener method which will call this method.

The annotation must be:

@OnItemSelected(value=R.id.my_view, method=OnItemSelected.Method.ITEM_SELECTED)
// or
@OnItemSelected(value=R.id.my_view, method=OnItemSelected.Method.NOTHING_SELECTED)

How to use

The method should be this way (the method and variable names can be changed):

  • public void onItemSelected(int position)

  • public void onItemSelected(int position, AdapterView adapterView)

  • public void onItemSelected(int position, AdapterView adapterView, View clickedView)

  • public void onNothingSelected(AdapterView adapterView)

Example

@OnItemSelected(value=R.id.list, method=OnItemSelected.Method.ITEM_SELECTED)
public void onItemSelected(int position) {
    Toast.makeText(this, "Item $position selected", Toast.LENGTH_SHORT).show();
}