Can a Gridview have both an onclick and onitemclick listener?
I have a gridview employing an ImageAdapter. Currently, one can click and long click each individual image in the gridview. I am trying to add functionality so that one can also click the gridview itself, that is to say the space not filled by the ImageAdapt开发者_StackOverflow中文版er. This is my code for my attempt thus far:
mGridViewWords.setAdapter(new AACButtonAdapter(this, button_size, TouchButtonAdapter.TOPIC_WORDS, mDB));
mGridViewWords.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
AddWordButtonAction((TouchButton) v);
}
});
mGridViewWords.setOnClickListener(new SelectViewListener());
mGridViewWords.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent_view, View button, int position, long id) {
removeButtonFromView((GridView)parent_view, position, mCurrentWordCategory);
return true;
}
});
however, when trying to use .setOnClickListener()
, I get this error in Logcat:
java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
Does anyone have advice as to different ways to get the functionality I am after? Can one set two adapters for a gridview?
Assuming your GridView takes up the whole screen, one solution would be to place another View under it in a FrameLayout that also fills the whole screen (for example, an ImageView with no android:src specified). If the user clicks an area not occupied by a grid item, the touch event will traverse down to the underlying View, where you can process it as a click in the GridView as you had wanted. Only slightly hacky, but your situation isn't common.
精彩评论