Android GridView Click Not Detected

I have the following LinearLayout with a GridView in it:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridView android:id="@+id/songs_grid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:columnWidth="250dp" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" /> </LinearLayout> 

GridView data is provided by a custom adapter (using a simple ViewHolder ):

 public class SongsAdapter extends BaseAdapter { private Context context; private LayoutInflater mInflater; private ArrayList<Song> songList; public SongsAdapter(Context context, ArrayList<Song> songList) { this.context = context; this.songList = songList; mInflater = LayoutInflater.from(this.context); } @Override public int getCount() { return songList.size(); } public Object getItem(int position) { return songList.get(position); } public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { SongViewHolder viewHolder; if (convertView == null) { convertView = mInflater.inflate(R.layout.song_list_item, null); viewHolder = new SongViewHolder(); //Get Views from Layout Template viewHolder.AlbumArt = (ImageView)convertView.findViewById(R.id.albumart); viewHolder.Title = (TextView)convertView.findViewById(R.id.title); viewHolder.Artist = (TextView)convertView.findViewById(R.id.artist); } else { viewHolder = (SongViewHolder) convertView.getTag(); } Song song = songList.get(position); Bitmap bitmap = song.getAlbumArt(); Bitmap missing = BitmapFactory.decodeResource(context.getResources(), R.drawable.albumart_missing); Bitmap albumArt = bitmap == null ? missing : bitmap; viewHolder.AlbumArt.setImageBitmap(albumArt); viewHolder.Title.setText(song.getTitle()); viewHolder.Artist.setText(song.getArtist()); convertView.setTag(viewHolder); return convertView; } } 

When the application starts, it gets a list of songs (from xml, but currently uses a manually entered list) and passes it to my adapter, and then sets OnItemClickListener :

 GridView gridView = (GridView) findViewById(R.id.songs_grid); gridView.setAdapter(new SongsAdapter(this, fk.getSongList())); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { LinearLayout layout = (LinearLayout)view; TextView songTitle = (TextView)layout.findViewById(R.id.song_title); Song song = fk.getSong((String) songTitle.getText()); if(song != null) { Intent play = new Intent(getBaseContext(), Play.class); startActivity(play); } } }); 

When I first implemented this code, everything worked fine, and I'm sure I haven't changed anything. I tried to make a text to scroll the song name in the layout if it was too long, but could not get it to work. At this point, I found that the click stops working. I have now deleted (I believe) all this code, but it still doesn't work.

When I touch / click (whatever you call) nothing happens. no errors.

Is there something wrong with my code that might cause this problem?

+7
source share
5 answers

Check the location of the item. If any of the layout views song_list_item is marked as clickable, they will block the onclick grid listener.

+36
source

Make sure the clickable attribute in the GridView does not override click events for elements in the view.

+6
source

I ran into this problem and the solution is similar to dmon's answer. If one of the children is selected, it also prohibits clicking. Therefore, if android:textIsSelectable true.

+1
source

if you make the whole image and text representation of android: focusable = "false" android: focusableInTouchMode = "false"

There will be no problems with the click listener in gridview

+1
source

Add this to ur xml cell

 android:descendantFocusability="blocksDescendants" 
0
source

All Articles