Skip to content Skip to sidebar Skip to footer

Androidannotations: How To Access View On @itemclick

I use AndroidAnnotations to build Android application. But I have no idea how to refactor this below to use @ItemClick because @ItemClick doesn't accept View parameter. By the way,

Solution 1:

Unfortunately you cannot add a View parameter, but you can use the position parameter. The problem with that there is no method to return the clicked View, so you have to use some logic to get the View from all the children.

@ViewById(R.id.gridViewId)
GridView gridView;

@ItemClick(R.id.gridViewId)voiditemClicked(int position) {
    intfirstPosition= gridView.getFirstVisiblePosition();
    intlastPosition= gridView.getLastVisiblePosition();

    View clickedView;

    if ((position < firstPosition) || (position > lastPosition))
        clickedView = null;

    clickedView = gridView.getChildAt(position - firstPosition);
    // do sg with clicked view
}

But this is actually not as clean as your code without AA... However, the View parameter may be supported soon. Until that, i suggest to stick with the old way if you really has to work with the View param.

Solution 2:

You can't retrieve the View with @ItemClick annotation, but the selected item itself.

@ItemClick(R.id.yourgrid_id)
voiditemClicked(Object item) {

//DO SOMETHING
}

item type depends on your Grid adapter.

Post a Comment for "Androidannotations: How To Access View On @itemclick"