您好我在沉浸式普通卡上创建菜单时遇到问题。这就是我到目前为止所做的:
我有一个实现OnItemClickListener的CardScrollAdapter,如下所示:
私有类ExampleCardScrollAdapter扩展CardScrollAdapter实现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) {
View rowView = convertView;
ViewHolder holder;
if(rowView==null) {
rowView = mInflater.inflate(R.layout.card_layout, null);
holder = new ViewHolder();
holder.mainText = (TextView) rowView.findViewById(R.id.tvMainText);
holder.fullImage = (ImageView) rowView.findViewById(R.id.fullImage);
holder.image1=(ImageView) rowView.findViewById(R.id.ivImage1);
holder.image2 = (ImageView) rowView.findViewById(R.id.ivImage2);
holder.image3 = (ImageView) rowView.findViewById(R.id.ivImage3);
holder.llImages = (LinearLayout) rowView.findViewById(R.id.llImages);
holder.llSecondaryImages = (LinearLayout) rowView.findViewById(R.id.llSecondaryImages);
rowView.setTag( holder );
} else holder = (ViewHolder) convertView.getTag();
CustomCard myObject = (CustomCard) getItem(position);
if (myObject.getCompanyInfo()!=null) {
System.out.println(myObject.getImageURL());
System.out.println(myObject.getCompanyInfo());
holder.mainText.setText(myObject.getCompanyInfo());
imageLoader.DisplayImage(myObject.getImageURL(), holder.image1);
holder.mainText.setVisibility(ImageView.VISIBLE);
holder.llImages.setVisibility(ImageView.VISIBLE);
holder.image1.setVisibility(ImageView.VISIBLE);
} else {
imageLoader.DisplayImage(myObject.getImageURL(), holder.fullImage);
holder.llImages.setVisibility(ImageView.VISIBLE);
holder.fullImage.setVisibility(ImageView.VISIBLE);
}
holder.image2.setVisibility(ImageView.GONE);
holder.image3.setVisibility(ImageView.GONE);
holder.llSecondaryImages.setVisibility(ImageView.GONE);
return rowView;
}
public class ViewHolder {
public TextView mainText;
public ImageView fullImage;
public ImageView image1;
public ImageView image2;
public ImageView image3;
public LinearLayout llImages;
public LinearLayout llSecondaryImages;
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("Click" + position);
onCreateOptionsMenu(null);
}
}
当我点击我的卡片时,它会打印“点击”和卡片的位置,以便工作。
之后我想运行看起来像这样的onCreateOptionsMenu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.card_menu, menu);
return true;
}
我遇到的问题是了解如何使onCreateOptionsMenu运行? 我必须从onItemClick方法而不是null?
提供什么菜单菜单// Joakim
答案 0 :(得分:2)
对于这种情况,您不需要单独的活动 - 只有当您需要从现场卡片中显示菜单时才应该这样做。
错误源于您原来的onItemClick
方法:您应该拨打openOptionsMenu
来显示菜单,而不是onCreateOptionsMenu
。
答案 1 :(得分:1)
您必须定义在单击菜单时运行的onOptionsItemSelected(MenuItem项)。作为参考,您可以看到定义的here菜单项。
答案 2 :(得分:1)
仅供参考我最终是这样做的:
我实施了一项单独的活动:
public class MenuActivity extends Activity {
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
openOptionsMenu();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.card_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection.
switch (item.getItemId()) {
case R.id.navigate:
System.out.println("Click");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onOptionsMenuClosed(Menu menu) {
// Nothing else to do, closing the activity.
finish();
}
}
然后我创建了意图:
checklistIntent = new Intent(MainActivity.this, MenuActivity.class);
在我的OnItemClick中我只做:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(checklistIntent);
}
所有其他东西都和以前一样。
希望这有助于将来。