Android Gallery View 给定项目的 onClickListener 时出现滚动问题

发布于 2024-10-20 02:36:48 字数 280 浏览 6 评论 0原文

我在我的应用程序中使用了图库。

因为我在每个画廊项目中有两个图像,如下所示

在此处输入图像描述

每个兔子和鼠标图像都组合为一个画廊项目。

因此,我为两个图像都提供了 onclickListener,但如果我这样提供,我将无法通过触摸这些图像来滚动...如果我删除该单个图像的 onClickListener,我就可以滚动。

如何存档每个图像的滚动和 onClick。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝眼泪 2024-10-27 02:36:48

回答了您的问题。您必须让您的活动同时处理 onClick 和手势。

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

晌融 2024-10-27 02:36:48

就我而言,我只是使用 Gallery.setOnItemClickListener 和监听器来处理对父 Activity 的回调。

当我像上面的解决方案中那样监听 Activity 时,我没有注册点击。

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.

灼疼热情 2024-10-27 02:36:48

我也面临这个问题。经过 2 天的工作,我找到了一个完美的解决方案:

  1. 为画廊也设置 onItemClickListener
  2. 在activity上,监听gallery和activity的onTouchEvent,记下原始坐标

    <前><代码>@Override
    公共布尔 onTouch(View v, MotionEvent 事件) {
    x = (int)event.getRawX();
    y = (int)event.getRawY();
    返回假;
    }

    @覆盖
    公共布尔onTouchEvent(MotionEvent事件){
    x = (int)event.getRawX();
    y = (int)event.getRawY();
    返回 super.onTouchEvent(event);
    }

  3. gallery的原始坐标

    onItemClick,你会得到每个查看其内部并检查点击坐标。

    矩形框 = new Rect();
    image[i].getGlobalVisibleRect(frame);
    if (frame.contains(x, y)) {//做你想做的事}
    

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}
    
半暖夏伤 2024-10-27 02:36:48

我遇到了同样的问题,但很容易解决。我所做的是将 setOnItemClickListener 添加到 GalleryView,然后获取我想要的视图,在我的例子中是 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);
                    }

                }
            });

在您的情况下,您可以首先检查显示了哪些图像,然后根据该信息您可以操纵视图。希望这有帮助

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

孤凫 2024-10-27 02:36:48

我的活动中有多个画廊,我这样做:

实现 OnItemClickListener

public class ImageBoardActivity extends Activity implements OnItemClickListener {

重写 onItemClick() 方法

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
    // do what you want here...
}

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...
}
原谅过去的我 2024-10-27 02:36:48

我的解决方案:

不要这样做!

:=)) (花了超过 6 个小时尝试解决这个问题...对我来说不起作用...)
使用了另一种方法(不同的布局)

My Solution:

don't do this!

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文