我有一个CardScrollView
,其中有多个项目,我希望能够在项目上拉出菜单,类似于内置的时间轴。
我知道卡片上没有附加特定的菜单,所以我在活动级别准备了菜单。
然而,似乎有些东西吞噬了所有onKeyDown事件。
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);
}
}
答案 0 :(得分:4)
如果您只需轻轻点击CardScrollView
中的卡片,就可以拨打setOnItemClickListener
来附加AdapterView.OnItemClickListener
,就像使用标准Android ListView
一样1}}。对于这个基本用例,这通常比使用GestureDetector
简单得多。
答案 1 :(得分:3)
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;
}
您的菜单现在应该打开!