GOLDEE
GOLDEE

Reputation: 2318

GridView not getting clicked first time

First of all I want to say , this question may be seems like it has been asked before but I have tried all solution suggested before on this issue ,that's why I am putting this question.

Now problem is , I am using a Gridview to show images , I have set clicklistener on images in Gridview's getview method but I am unable to get click on very first image , All other images are getting clicked but when I click on first image its onclick event not triggered and it triggers when I click on other images or any other view . Can anyone faced this problem before , plz help if got any clue . Here is my code :

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(_activity);       
        } else {
            imageView = (ImageView) convertView;
        }

         imageLoader.DisplayImage(_filePaths.get(position), imageView);
         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
                    imageWidth));

        // image view click listener
        imageView.setOnClickListener(new OnImageClickListener(position));

        return imageView;
    }

    class OnImageClickListener implements OnClickListener {

        int _postion;

        // constructor
        public OnImageClickListener(int position) {
            this._postion = position;
        }

        @Override
        public void onClick(View v) {
            Log.i("Clicked on Image", "Yes");
            Intent i = new Intent(_activity, Slideshow.class);
            i.putExtra("position", _postion);           
            _activity.startActivity(i);
            _activity.overridePendingTransition(R.anim.slide_top_in, R.anim.slide_top_out);
        }

    }

Upvotes: 2

Views: 713

Answers (2)

Chintan Khetiya
Chintan Khetiya

Reputation: 16142

You are calling wrong Click Listener. View OnClickListener is only for single view while AdapterView OnItemClickListener is for Grid or List.

gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView <? > parent, View v, int position, long id) {
            Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
        }
    });

Interface definition for a callback to be invoked when an item in this AdapterView has been clicked.

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
  • Callback method to be invoked when an item in this AdapterView has been clicked.

  • Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

  • Parameters

    1. parent - The AdapterView where the click happened.

    2. view - The view within the AdapterView that was clicked (this will be a view provided by the adapter)

    3. position - The position of the view in the adapter.

    4. id - The row id of the item that was clicked.

Upvotes: 2

GareginSargsyan
GareginSargsyan

Reputation: 1895

Try using GridView.setOnItemClickListener() instead of setting OnClickListeners on individual images. Here is the method's documentation: http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)

Upvotes: 4

Related Questions