我有一个包含 GridView 的 ViewPager。这是 PagerAdaper 的instantiateItem
方法:
@Override
public Object instantiateItem(ViewGroup container, int position) {
View page = inflater.inflate(R.layout.emoticon_grid, null);
GridView grid = (GridView) page.findViewById(R.id.emoticon_grid);
grid.setAdapter(new EmoticonListAdapter(context, list, position));
container.addView(page);
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(listener != null) {
Emoticon selectedEmoticon = (Emoticon) arg0.getAdapter().getItem(arg2);
listener.emoticonSelected(selectedEmoticon);
}
}
});
return page;
}
据我所知,其他人在将固有的可点击视图(例如按钮)添加到 GridViews 时遇到了类似的问题。但我只使用 ImageView
public View getView(int position, View convertView, ViewGroup parent) {
Emoticon emoticon = emoticons[position];
if(convertView == null) {
convertView = new ImageView(context);
}
((ImageView) convertView).setImageResource(emoticon.ResourceId);
return convertView;
}
在 onItemClickMethod 中添加调试日志和设置断点后,永远不会调用回调。