Android: Grid view with clickable grid items and nested views (buttons, checkboxes)

前端 未结 2 1914
花落未央
花落未央 2021-01-06 07:33

Basically, I would like to implement a GridView wherein the items themselves are clickable, but within the GridView are clickable Button

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-06 08: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, });

提交回复
热议问题