Getting Touch Events on CardScrollView

I have CardScrollViewone in which there are several elements, and I would like to be able to pull out the menu on the element, like the built-in timeline.

I know that a map cannot be tied to a specific menu, so I have a menu prepared at the activity level.

However, it looks like something similar to all the 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);
  }
}
+4
source share
2 answers

CardScrollView, setOnItemClickListener, AdapterView.OnItemClickListener, Android ListView. , GestureDetector .

+4

. GestureDetector, GDK. , :

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

!

+3

All Articles