Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving touch events on CardScrollView

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);
  }
}
like image 354
kurtisnelson Avatar asked Mar 22 '23 12:03

kurtisnelson


1 Answers

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.

like image 178
Tony Allevato Avatar answered Apr 20 '23 21:04

Tony Allevato