4

Basically, I would like to implement a GridView wherein the items themselves are clickable, but within the GridView are clickable Buttons and Checkboxes.

Here's a sample layout:

Gridview
-----------------------------------------
|[ImageView]        |[ImageView]        |
|[TextView]         |[TextView]         |
|[Button][Checkbox] |[Button][Checkbox] |
-----------------------------------------
|[ImageView]        |[ImageView]        |
|[TextView]         |[TextView]         |
|[Button][Checkbox] |[Button][Checkbox] |
-----------------------------------------

Basically, what i want to do is, when the user clicks the CheckBox, multiple items can now be selected from the GridView. When the users clicks the Button, A Popup is shown. When the user clicks anywhere else, a new Activity is started. The whole point of this is instead of long-pressing to show the context menu, I would like a button to take its place.

Any suggestions on how I can approach this scenario? In my current setup, if I add the Button within the adapter's GetView() method, only the Button is clickable. The whole GridView item is not clickable. When I remove the Button, the GridView item is clickable again. It seems that it's only the whole GridView Item or the Button is clickable (responds to OnClickListener(). Is there a way to make them both clickable?

Neilers
  • 411
  • 5
  • 12

2 Answers2

11

This post is old, but just for reference/googlers, I have the following solution:

You need to write all the events for buttons, checkboxes in getView of the imageAdapter only. Then in your layout.xml file under the gridview tag add these lines:

android:clickable="true"
android:descendantFocusability="beforeDescendants"

and add these lines to your button and checkboxes:

android:focusable="false"
android:focusableInTouchMode="false"

And if you want some other activity to start if any other area of the gridview item is clicked/touched you would need to use the (standard code)/(your own matching implementation):

GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }

Cheers, });

AliR
  • 2,065
  • 1
  • 27
  • 37
1

You can make an item xml and use it as your gridView item. Then with an adapter initialize the buttons and checkboxes in your gridView. After you can add your clickListener not to the gridView items, but to the layout in the item xml, so it's child views won't respond to the click event.

superM
  • 8,605
  • 8
  • 42
  • 51
  • Yes, i currently have that set-up. My problem is with the `OnClickListeners()`. When I add the `Button`, the `OnClickListener()` of the entire `GridView` item is not fired, only the `Button`'s `OnClickListener()`. I'm looking for a way to have both the whole `GridView`'s and the `Button`'s `OnClickListener()`s fire-able, if there's such a term. – Neilers Sep 12 '11 at 10:26
  • You could try added OnClickListener() not to the entire GridView but to the layout containing it. So when user click on anywhere in the gridView but your buttons this event will be fired – superM Sep 12 '11 at 10:30
  • I think you did not get what I meant, but I get your idea. It's not the whole `GridView` but the specific `GridViewItem` that I want to be clickable. I'll try implementing `OnClickListener()`s to both the `ImageView` and `TextVied`, or encase them both in a `LinearLayout` and implement an `OnClickListener()` on that `LinearLayout`. – Neilers Sep 12 '11 at 10:46