12

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.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Ganapathy C
  • 5,989
  • 5
  • 42
  • 75

6 Answers6

4

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

Community
  • 1
  • 1
Reno
  • 33,594
  • 11
  • 89
  • 102
  • 1
    even though its not working when i set onclick listener for the views in the adapter for gallery items.. – Ganapathy C Mar 04 '11 at 06:41
  • You should use the onSingleTap from the gesture detector... that will work in your case! – WarrenFaith Mar 07 '11 at 14:57
  • onSingleTap? care to explain more please as i have same issue as OP but my onClick listener is inside my baseAdapter – Jono Jul 26 '11 at 10:10
  • I know that if someone come from meta and downvote this good answer just by showing you link to [this question](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) , i am totally against it and posting this comment just because used your answer for example over there in my answer, hope you wont mind it or will remove the reference. – MKJParekh May 17 '12 at 11:12
  • Yeah I remember discussing this is our Android chatroom. I should have deleted this answer. I'll keep it for a while and expand this answer later (I'm a little busy these days), this answer is outdated and crass. – Reno May 17 '12 at 12:10
3

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.

androider
  • 333
  • 5
  • 13
  • None of the other approaches worked for me and after finding this answer, my galleries now worked as expected. Thanks!! – hooked82 Aug 28 '11 at 07:07
3

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}
    
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
tsengvn
  • 88
  • 5
  • I`m having a problem here, where when I set OnTouchListener on gallery, my OnItemClickListener stops working, even if I return false on onTouch. Any ideas? – Paulo Cesar Mar 06 '12 at 23:32
  • did you try to use my approach, i don't need to set OnTouchListener to any view – tsengvn Mar 07 '12 at 08:42
2

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

Bami
  • 111
  • 9
0

I have multiple galleries in my activity and I do it that way:

Implementing the OnItemClickListener:

public class ImageBoardActivity extends Activity implements OnItemClickListener {

Overriding onItemClick() method

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
    // do what you want here...
}
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • i want each elements inside gallery items to be click-able and not the whole gallery items. i know this way.plz read the question again dude.. – Ganapathy C Mar 07 '11 at 15:13
  • How about testing the code, **dude** (don't call me that again!) In my simple case with multiple galleries, the `view` parameter is the ImageView (debug information: android.widget.ImageView@483468d0) used inside the gallery. So, again, test the code, **dude**! – WarrenFaith Mar 07 '11 at 15:18
  • How would you do that if the Gallery contains a `RelativeLayout`. Then this `RelativeLayout` consumes the touch.. not sure yet how to fix it. – Sebastian Roth Jun 11 '11 at 03:26
  • If the layout consume the touch or not can be changed by yourself. If you set an OnTouchListener which returns true, the touch is consumed. If it returns always false, the touch will be delegated to the next child. – WarrenFaith Jun 11 '11 at 13:07
0

My Solution:

don't do this!

:=)) (spend over 6 hours trying to solve this.. didnt work for me...) used another Approach (different Layout)

cV2
  • 5,229
  • 3
  • 43
  • 53
  • what is that different layout ? – Ganapathy C Mar 26 '12 at 06:56
  • 1
    I Wanted to Put 2 Images in a Gallery, to Preview a Magazine, containing double pages.. so page 1, page 2-3, pager 4-5 .. and so on, so i used a layout as gallery for gallery items, which contained two imageviews, and wanted to put conclicklistener to each imageview. i tried everything (onTouchListener, onClickListener, hacks with density pixels..) no really working solution in it (detecting one view in another view, required double clicks..) so i finally set each image as gallery item, (layout containing one imageview) and solved the arrangement via margins in code... :=) hope this helps, :) – cV2 Mar 26 '12 at 09:30
  • can you post the sample layout so others can understand easily .. :) – Ganapathy C Mar 28 '12 at 12:55
  • it doesnt really belong to this post, so for instance, if i would have to solve this layout issue, i would use a gridview instead of a gallery... you can easily select each and every layout :) – cV2 Mar 28 '12 at 13:15
  • but if we need horizontal scrolling then what we can do with grid view ...?? Is there any thing like horizontally scrollable gridview ?? .. client UI need is like this .. changing the UI in different way and also a simple way.. but customizing the view in our way is possible na?? :) – Ganapathy C Mar 29 '12 at 06:58