4

I have a CardScrollView that has multiple items in it and I would like to be able to pull up a menu on an item, similar to the built in Timeline.

I know Card cannot have a specific menu attached to it so I have the menu prepared at the Activity level.

However, something seems to be swallowing all onKeyDown events.

public class HostsView extends CardScrollView {
  private String TAG = "HostsView";
  private HostsCardScrollAdapter cards;
  private Activity parent;

  public HostsView(Activity parent, HostDatabase hostDb) {
    super(parent);
    cards = new HostsCardScrollAdapter(parent);
            //populates the cards and what not
    this.setAdapter(cards);
    this.activate();
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
                  //I never see this log
    Log.d(TAG, "Key event " + event.toString());
    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
        parent.openOptionsMenu();
        return true;
    }
    return super.onKeyDown(keyCode, event);
  }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kurtisnelson
  • 367
  • 1
  • 7

2 Answers2

4

If you only need to handle a simple tap on a card in a CardScrollView, you can call setOnItemClickListener to attach an AdapterView.OnItemClickListener, just as you would with a standard Android ListView. This is typically much simpler than working with GestureDetector for this basic use case.

Tony Allevato
  • 6,429
  • 1
  • 29
  • 34
  • This doesn't seem to work at all with the CardScrollView, tried both setOnItemClickListener and setOnItemSelectedListener. Is there a trick? GestureDetector does work ok though. – javram Dec 25 '13 at 05:45
  • `OnItemClickListener` should fire when an item is tapped (as long as you're not doing something elsewhere that is causing touch events to get intercepted), and `OnItemSelectedListener` should fire when you scroll from one card to another. Can you post some code where it doesn't work for you? – Tony Allevato Dec 25 '13 at 17:11
  • Wow, a response on Christmas day! Thats dedication! I tried to recreate this again, and it seems to be working now. which is good. The OnItemClickListener is about half the code. The only thing that I had changed since yesterday was that I am now using a custom view, instead of the Card class. Could that have had any effect? – javram Dec 25 '13 at 17:43
  • (Family was sleeping, why not check StackOverflow? :) The Card class shouldn't cause any problems there -- there's nothing on the Card class's views that would take the focus away from the scroller itself. My first suspicion would be an activate/deactivate problem, maybe? But I'm glad to hear it's working now, at least! – Tony Allevato Dec 25 '13 at 17:49
  • thanks again, one more quick, semi-related question. Where could I go to find the view hierarchy for the card class as xml? I would like to use the card as the basis for my custom view, instead of trying to roll my own. Is the source available somewhere? – javram Dec 25 '13 at 17:56
  • The source for those layouts is not available. The layouts themselves may change during the time that the GDK is in the sneak peek face, so depending on the exact view hierarchy or the views' IDs is something we don't recommend since it would be very fragile. If you need a specific kind of layout, it's best to roll your own from scratch. – Tony Allevato Dec 25 '13 at 18:28
3

yesterday I came across the same problem. I solved it with a GestureDetector, as the GDK documentation recommends. Here is the code I used:

private GestureDetector mGestureDetector;

@Override
public void onCreate(Bundle savedInstanceState) {
mGestureDetector = createGestureDetector(this);
}


private GestureDetector createGestureDetector(Context context) {
    GestureDetector gestureDetector = new GestureDetector(context);
    gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
        @Override
        public boolean onGesture(Gesture gesture) {
            if (gesture == Gesture.LONG_PRESS || gesture == Gesture.TAP) {
                Log.d(MainActivity.TAG, "Tap"); //When I tap the touch panel, I only get LONG_PRESS
                openOptionsMenu();
                return true;
            } else if (gesture == Gesture.TWO_TAP) {

                return true;
            } else if (gesture == Gesture.SWIPE_RIGHT) {

                return true;
            } else if (gesture == Gesture.SWIPE_LEFT) {

                return true;
            }
            return false;
        }
    });
    return gestureDetector;
}

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if (mGestureDetector != null) {
        return mGestureDetector.onMotionEvent(event);
    }
    return false;
}

Your menu should open now!

Racker
  • 1,316
  • 1
  • 11
  • 13
  • 1
    The GestureDetector is recommended in https://developers.google.com/glass/develop/gdk/input/touch but it only worked for me with the LONG_PRESS – Racker Dec 05 '13 at 09:39
  • This works, but the canonically correct answer would be to use the `AdapterView.OnItemClickListener` as suggested in the other answer below. – wmarbut Jan 03 '14 at 14:06