Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Gallery View Scrolling problem when onClickListener for items given

I have used gallery in my app.

In that i have two images in each gallery item like this

enter image description here

Each rabbit and mouse image is combined as a single gallery item.

So I give onclickListener for both images but if I give like that I can't scroll by touching those images... If I remove onClickListener for that individual images I am able to scroll.

How to archive both scroll and onClick for each images.

like image 596
Ganapathy C Avatar asked Mar 03 '11 08:03

Ganapathy C


4 Answers

This answers your question. You have to let your activity handle both onClick and Gestures.

like image 110
Reno Avatar answered Nov 10 '22 16:11

Reno


In my case I just used the Gallery.setOnItemClickListener with the Listener handling the callback to the parent Activity.

When I had the Activity listening as in the the solution above the clicks didn't register for me.

like image 44
androider Avatar answered Nov 10 '22 17:11

androider


I faced to this problem too. And after 2 days working, I found a perfect solution for this:

  1. Set onItemClickListener for gallery too.
  2. On the activity, listen to onTouchEvent of gallery and activity, write down the raw coordinate

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        x = (int)event.getRawX();
        y = (int)event.getRawY();
        return false;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = (int)event.getRawX();
        y = (int)event.getRawY();
        return super.onTouchEvent(event);
    }
    
  3. onItemClick for the gallery, you get each view inside it and check the click coordinate.

    Rect frame = new Rect();
    image[i].getGlobalVisibleRect(frame);
    if (frame.contains(x, y)) {//do whatever you want}
    
like image 3
tsengvn Avatar answered Nov 10 '22 15:11

tsengvn


I had this same problem but solved it pretty easy. What I did was is added setOnItemClickListener to the GalleryView and then grabbed the view that i wanted, which was in my case a TextView.

 private boolean isVisble = true;
 gallery_images.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    TextView image_text =  ((TextView)arg1.findViewById(R.id.image_text));
                    if(!isVisble){

                        isVisble = true;

                        image_text.setVisibility(TextView.VISIBLE);
                    }
                    else{
                        isVisble = false;
                        image_text.setVisibility(TextView.GONE);
                    }

                }
            });

In your case you could first check which images are shown and based on that information you can maniuplate the view. Hope this helps

like image 2
Bami Avatar answered Nov 10 '22 15:11

Bami