Android Gridview and OnItemclick Button

Here is my buttonAdapter class, which I find accurate:

package com.example.test; import android.content.Context; import android.graphics.Color; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.GridView; public class ButtonAdapter extends BaseAdapter { private Context mContext; public String [] fName = { "File 1", "File 2", "Roflcopters"}; // Gets the context so it can be used later public ButtonAdapter(Context c) { mContext = c; } // Total number of things contained within the adapter public int getCount () { return 8; } // Require for structure, not really used in my code. public Object getItem (int position) { return null; } // Require for structure, not really used in my code. Can be used to get the id of an item in the adapter for manual control. public long getItemId (int position) { return position; } public View getView (int position, View convertView, ViewGroup parent){ Button btn; if (convertView == null) { // if it not recycled, initialize some attributes btn = new Button (mContext); btn.setLayoutParams (new GridView.LayoutParams (190, 190)); btn.setPadding (1, 1, 1, 1); } else { btn = (Button) convertView; } // btn.setText(filesnames[position]); // filenames is an array of strings //btn.setTextColor (Color.WHITE); //btn.setBackgroundResource (R.drawable.sample_2); //btn.setBackgroundColor (Color.BLACK); btn.setHighlightColor(Color.GREEN); btn.setId (position); return btn; } } 

Here is my home class. I cannot get onItemClick to work. What am I doing wrong here:

 package com.example.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.Toast; public class home extends Activity implements OnItemClickListener { public final static String EXTRA_MESSAGE1 = "com.example.text.MESSAGE"; public void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView (R.layout.activity_home); GridView gridview = (GridView) findViewById (R.id.gridview); gridview.setAdapter (new ButtonAdapter (this)); /*gridview.setOnItemClickListener (new OnItemClickListener () { public void onItemClick (AdapterView <?> parent, View v, int position, long id) { Toast.makeText (home.this, "" + position, Toast.LENGTH_LONG).show (); Intent intent = new Intent (this, alarm.class); String message = "Position:" + position; intent.putExtra(EXTRA_MESSAGE1, message); startActivity (intent); } }); * */ } @Override public void onItemClick (AdapterView <?> parent, View v, int position, long id) { Intent intent = new Intent (this, alarm.class); String message = "Position:" + position; intent.putExtra(EXTRA_MESSAGE1, message); startActivity (intent); } } 

OnItemClick does not work, and does not comment on 'setOnItemClickListener' when it is not commented, and 'onItemClick' is commented. What am I doing wrong?

+4
source share
4 answers

If the GridView, ListView have controls available, such as BUtton, then onItemClick will not onItemClick .
You need to implement Button Click listener in your adapter getView method.

as

 public View getView(int position, View convertView, ViewGroup parent) { Button btn; if (convertView == null) { // if it not recycled, initialize some // attributes btn = new Button (mContext); btn.setLayoutParams(new GridView.LayoutParams(190, 190)); btn.setPadding(1, 1, 1, 1); } else { btn = (Button) convertView; } // btn.setText(filesnames[position]); // filenames is an array of // strings //btn.setTextColor (Color.WHITE); // btn.setBackgroundResource (R.drawable.sample_2); // btn.setBackgroundColor (Color.BLACK); btn.setHighlightColor(Color.GREEN); btn.setId(position); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Handle the click here } }); return btn; } 
+5
source

You can add this line to the parent layout of the GridView elements:

 android:descendantFocusability="blocksDescendants" 

Then onItemClickListener.onItemClick() will not fire if you click on subviews for which OnClickListener is defined separately for them.

+2
source

I tested that Set Button.onClickListener () (in API 15) would not solve the problem.

Thus, the GridView will not launch onItemClick if it contains clickable views.

You can use ImageView instead of Button.

0
source

I have the same problem when I tried to implement onitemclick on a gridview where it was populated with a button. Since the button steals the focus of each place on the gridview , you must give the inflated android:focusable="false" button android:focusable="false" . however, the button occupies almost the entire space inside the grid, so you need to click on the very edge of the button to trigger the onitemclick . I suggest you set onclick or use an image and create it as a button.

-1
source

All Articles