Item not clicked on a GridView
-
11-06-2021 - |
質問
I have a GridView whose items are ToggleButtons, and I defined an event listener for clicks on items, but nothing happens when I click on them. Here's the listener:
paintActionsGrid.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,View view,int position,
long id)
{
((ToggleButton)view).setChecked(true);
paintBoardView.setPaintAction(paintActions[position]);
}
});
I put a break point in the listener, but it's not reached, which means the listener is not called at all! Can somebody please tell me what's wrong? Is it related to the fact that the items are ToggleButtons? I can attach the adapter code if needed. Thanks.
解決
The click event is consumed by ToggleButton hence not passed to the GridView. Similar to this post.
他のヒント
Yeah the problem is ToggleButton by default has its clickable enabled and hence your ItemClickListener won't work. The idea is to set the ToggleButton's focus to false by default.
Assuming that you have a custom xml for your ToggleButton, add this line to it,
android:focusable="false"
Since you have a focused element, this problem occurs. Try this simple idea.