问题描述
我用AndroidAnnotations构建Android应用程序。但我不知道如何下重构这个使用 @ItemClick
,因为 @ItemClick
不接受查看
参数。顺便说一句,我用查看
来获得选择的GridView的标签。
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);
}
});
任何想法?
推荐答案
可惜你不能添加查看
参数,但可以使用位置参数。这样做的问题是没有方法返回点击查看
,所以你必须使用一些逻辑来获取查看
从所有的孩子们。
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
}
不过,这其实不是因为你的code,无AA ......然而,在查看
参数可以被支持的的。在此之前,我建议坚持使用旧的方式,如果你真的有与查看
参数工作。
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.
这篇关于AndroidAnnotations:如何访问@ItemClick查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!