开发者

How do I programmatically add an Android Gestures view to a custom view?

开发者 https://www.devze.com 2023-01-01 07:36 出处:网络
I have a custom view that works fine and I\'m trying to get gestures into it. The most common technique I see is to add XML, such as this (from Android docs:

I have a custom view that works fine and I'm trying to get gestures into it. The most common technique I see is to add XML, such as this (from Android docs:

<android.gesture.GestureOverlayView
  android:id="@+id/myGestures"
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1.0"
  />

My view is within a RelativeView and when I attempt to reference this GetureOverlayView, I get an exception.

I've also tried to connect within my existing custom view class, like this:

mGestures = new GestureOverlayView(context, attrs);
mGestures.addOnGesturePerformedListener(this);

But the callbacks are never invoked.

Can someone point out my errors, suggest a better way that will allow me to get gesture callback开发者_StackOverflows and/or suggest diagnostic approaches?


Greetings, I came across your question as I struggled to find a solution to the same problem: I have a custom view, a gauge, which I was unable to get gestures to work on (until I found your question).

I was able to solve the problem, hence this response.

I found that my view was returning click events, but the action was always 0, for ACTION_DOWN... Whereas on my other layout, where gestures were working, I would get various actions for any single gesture.

I went ahead and took the linearlayout that was failing, and enclosed it within a gestureOverlayView using your code above. With one difference being that I placed my whole linearlayout within the gestureoverlay. Your example above shows the gestureoverlay as a stand-alone entity with nothing in it, which was likely the cause of your problems.

To use it, I simply register my gesture-listener against the overlayview as follows:

((GestureOverlayView) findViewById(R.id.myGestures)).setOnTouchListener(gestureListener);

My apologies for the quick response, but I hope at least I can shed some light on your issue, or help anybody else who hits this quirk as they are working with gestures.

BH

0

精彩评论

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