Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GridView click not being 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>

The GridView data is supplied 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 app starts it gets a list of songs (from xml but currently using a manually entered list) and passes that to the my adapter and then sets the 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, it all worked fine, and I am sure I haven't changed anything. I was trying to make the text for Song Title scroll in a marquee fashion if it was too long, but couldn't get it working. During this phase I discovered that the click stopped working. I have now removed (I believe) all that code, but it still fails.

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

Is there anything wrong with my code that would cause this issue?

like image 892
neildeadman Avatar asked Dec 19 '11 14:12

neildeadman


2 Answers

Check your item layout. If any of the song_list_item layout views are marked as clickable they will block the grid's onclick listener from responding.

like image 146
dmon Avatar answered Sep 22 '22 23:09

dmon


Make sure that the clickable-attribute in the GridView does not override the click-events for the items in the view.

like image 20
Jave Avatar answered Sep 23 '22 23:09

Jave