开发者

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

Android Gallery View Scrolling problem when onClickListener for items given

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开发者_Python百科 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.


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


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.


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}
    


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


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...
}


My Solution:

don't do this!

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜