使用多个视图/布局时,处理触摸事件的Andr​​oid [英] handling touch events in Android when using multiple views/layouts

查看:198
本文介绍了使用多个视图/布局时,处理触摸事件的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新到Android编程,并试图了解与嵌套视图触摸事件。要启动,这是我的应用程序的说明:

I'm very new to Android programming, and trying to understand touch events with nested views. To start, here's a description of my app:

我有,我已经通过图形用户界面编辑器增加了一个相对布局。一切都是默认的。我也创建了一个名为ClipGrid类扩展滚动型。这里面嵌套,我做一个Horizo​​ntalScrollView。里面的,我做一个TableLayout,它的行。该行包含按钮。

I have a relative layout that I've added via the GUI editor. Everything is default. I've also created a class called ClipGrid that extends ScrollView. Nested inside that, I make a HorizontalScrollView. Inside of that, I make a TableLayout and it's rows. The rows contain buttons.

最终的结果是按钮的一个网格。它显示4x4的一次,但可以滚动任意方向显示其他按钮。

The end result is a grid of buttons. It displays 4x4 at once, but can scroll either direction to display other buttons.

我调用它从我这样的主要活动画面:
ClipGrid clip_grid =新ClipGrid(本);
的setContentView(clip_grid);

I call it to the screen from my main activity like this: ClipGrid clip_grid = new ClipGrid(this); setContentView(clip_grid);

我这样做,只是为了测试目的,我想我会在以后改变它时,我想其他视图添加到我的RelativeLayout。但我认为它可能对触摸事件的影响。

I did that just for testing purposes, and I think I will have to change it later when I want to add other views to my relativelayout. But I think it might have implications for touch events.

在最后,我要当电网已移至检测和四合扣新近可视4x4网格我布局的边缘,当用户拿起他们的手指。我只是不知道如何去实现这一点,任何帮助,将AP preciated。谢谢你。

in the end, I want to detect when the grid has been moved and snap the newly viewable 4x4 grid of buttons to the edge of my layout when the user lifts their finger. I'm just not sure how to go about implementing this and any help would be appreciated. Thanks.

推荐答案

触摸事件的处理方式是怎么样的,从顶视图开始,下降到较低的嵌套视图级联效应。基本上,Android将通过该事件的每个视图,直到真正被返回。

The way touch events are handled is kind of a cascading effect that starts from the top view and goes down to the lower nested views. Basically, Android will pass the event to each view until true is returned.

您可以实现的查看 onTouchEvent 事件的一般方法是:

The general way you could implement the onTouchEvent event of a View would be:

@Override
public boolean onTouchEvent(MotionEvent event) {
  boolean actionHandled = false;
  final int action = event.getAction();

  switch(action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
      // user first touches view with pointer
      break;
    case MotionEvent.ACTION_MOVE:
      // user is still touching view and moving pointer around
      break;
    case MotionEvent.ACTION_UP:
      // user lifts pointer
      break;
  }

  // if the action was not handled by this touch, send it to other views
  if (!actionHandled) 
     actionHandled |= super.onTouch(v, MotionEvent.event);

  return actionHandled;
}

这篇关于使用多个视图/布局时,处理触摸事件的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆