Reputation: 7425
How do I have the menu pop up on a regular Card
. I have tried to use this code:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
openOptionsMenu();
return true;
}
return false;
}
But it doesn't seem to work. Also, is it possible to put Live Cards
in an Activity
like I did here. I believe I am using Static Cards
, but eventually I will need access to the camera and video functions through these Cards.
Activity
public class NewChecklistActivity extends Activity {
private GestureDetector mGestureDetector;
private List<Card> mCards;
private CardScrollView mCardScrollView;
StepCardScrollAdapter adapter;
Intent checklistIntent;
String allStepsJSONString;
ArrayList<String[]> allStepsArray;
ArrayList<String[]> checklistsArray;
ArrayList<String[]> stepsArray;
ArrayList<String[]> checklistSteps;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checklistsArray = (ArrayList<String[]>) this.getIntent().getSerializableExtra("checklists");
stepsArray = (ArrayList<String[]>) this.getIntent().getSerializableExtra("steps");
createCards();
mCardScrollView = new CardScrollView(this);
adapter = new StepCardScrollAdapter();
mCardScrollView.setAdapter(adapter);
mCardScrollView.activate();
setContentView(mCardScrollView);
mCardScrollView.setOnItemClickListener(adapter);
checklistIntent = new Intent(this, ChecklistActivity.class);
}
private void createCards() {
mCards = new ArrayList<Card>();
Card card;
String[] checklist;
for (int i = 0; i < checklistsArray.size(); i++) {
checklist = checklistsArray.get(i);
card = new Card(this);
card.setText(checklist[1]);
mCards.add(card);
}
}
private class StepCardScrollAdapter extends CardScrollAdapter implements OnItemClickListener {
@Override
public int findIdPosition(Object id) {
return -1;
}
@Override
public int findItemPosition(Object item) {
return mCards.indexOf(item);
}
@Override
public int getCount() {
return mCards.size();
}
@Override
public Object getItem(int position) {
return mCards.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return mCards.get(position).toView();
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
checklistIntent.putExtra("steps", getChecklistSteps(adapter.findItemPosition(position)));
startActivity(checklistIntent);
}
}
Upvotes: 1
Views: 1662
Reputation: 6429
It looks like you're using a CardScrollView
, which is going to intercept all of the touchpad gestures and that's why your onKeyDown
method isn't getting called.
For this specific case, you can attach an OnItemClickListener
to the card scroller and use that listener to find out when one of the cards is tapped. Then, call openOptionsMenu
from there.
If you need to have different menu items for different cards, you'll need to store the index of the card that gets passed into the listener and then use that information inside onPrepareOptionsMenu
to load different items. The flow would look like this:
onItemClick
method, you store the index you're given in that field, and then you call openOptionsMenu
.onPrepareOptionsMenu
to be called. Override that method and do whatever modifications you need to the menu based on the index stored in that field (inflate a menu, add/remove items, etc.). You have to do this inside onPrepareOptionsMenu
instead of onCreateOptionsMenu
because you want it to happen every time the user taps, not just the first time the menu is initialized.onOptionsItemSelected
as you normally would.Upvotes: 10