AndroidAnnotations: how to access View on @ItemClick

时光总嘲笑我的痴心妄想 提交于 2019-12-13 03:55:07

问题


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, I use View to get tag of selected GridView.

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
           String url = (String) view.getTag();
           intents.put("image", url);

           DetailActivity_.launch(HomeActivity.this, view.findViewById(R.id.image), intents);
       }
 });

Any idea?


回答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)
void itemClicked(int position) {
    int firstPosition = gridView.getFirstVisiblePosition();
    int lastPosition = 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.




回答2:


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

@ItemClick(R.id.yourgrid_id)
void itemClicked(Object item) {

//DO SOMETHING
}

item type depends on your Grid adapter.



来源:https://stackoverflow.com/questions/27861030/androidannotations-how-to-access-view-on-itemclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!