gpt4 book ai didi

android - 在 Android 中发生 OnClickEvent 时激活 CAB 菜单

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:11:51 26 4
gpt4 key购买 nike

单击 ActionBar 中的 MenuItem 时尝试激活 CAB 菜单。下面是我如何设置 GridView 来收听 Multi Choice。当我长按 GridView 中的任何项目时,multiModeChoiceListener 工作正常。它工作正常。现在我需要在按下操作栏中的菜单项时激活 CAB 菜单。按下后,CAB 菜单应显示已选择 0 个项目。之后,它应该允许我通过单击从 GridView 中选择项目。我怎样才能实现这个功能?

GridView 设置监听器:

gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL); 
gv.setMultiChoiceModeListener(new MultiChoiceModeListener());

MultiChoiceModeListener.java

public class MultiChoiceModeListener implements
GridView.MultiChoiceModeListener {

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.featured_multiselect, menu);
MenuItem mi = menu.findItem(R.id.close);
mi.setIcon(R.drawable.cancel);
mode.setTitle("Select Items");
return true;
}

public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(getApplicationContext(), item.getTitle(),
Toast.LENGTH_SHORT).show();
if (item.getTitle().toString().equalsIgnoreCase("Close")) {
mode.finish();
}
return true;
}

public void onDestroyActionMode(ActionMode mode) {
new ChangeNotifier().changeOnFavoriteStore = true;
new AddFavorites().execute("add", device_id, dataArray);
if (notify == true) {
Toast.makeText(getApplicationContext(),
"Selected items are added to Favorites",
Toast.LENGTH_SHORT).show();
notify = false;
}
}

public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
int selectCount = gridView.getCheckedItemCount();
if (selectCount > 0) {
notify = true;
dataArray.add(position);
switch (selectCount) {
case 1:
mode.setSubtitle("One item added to favorites");
break;
default:
mode.setSubtitle("" + selectCount
+ " items added to favorites");
break;
}
}

}

OnMenuItemClick 方法:

 public boolean onPrepareOptionsMenu(final Menu menu) {

final MenuItem editItem = menu.findItem(R.id.editit);

editItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

@Override
public boolean onMenuItemClick(MenuItem item) {
//the CAB menu should be activated here. So that it reads that 0 items are selected in ActionBar

return false;
}

});

最佳答案

根据您的问题,我了解到您正在尝试通过单击其中一个菜单项来启动与 GridView 关联的 CAB。我不知道你是否可以这样做(但我可能会弄错),因为 MultiChoiceModeListener 需要检查一个项目才能开始。根据您的布局和 GridView 的整体外观,我认为您可以在 GridView(没有内容显示)并使用 setItemChecked(dummyItemPosition, true) 启动 GridView CAB。当然,您需要有额外的逻辑来处理 MultiChoiceModeListener 中的额外项目:

 public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
if (position == theDummyPosition)
return; // so we start the CAB but there aren't any items checked
}
int selectCount = gridView.getCheckedItemCount();
if (selectCount > 0) {
notify = true;
dataArray.add(position);
// if you select another item you'll have two selected items(because of the dummy item) so you need to take care of it
switch (selectCount) {
case 1:
mode.setSubtitle("One item added to favorites");
break;
default:
mode.setSubtitle("" + selectCount
+ " items added to favorites");
break;
}
}

}

上面的解决方案是一个 hack,很可能丢失 MultiChoiceModeListener 并简单地启动一个您可以在两种情况下都可以操作的 ActionMode 会容易得多。

关于android - 在 Android 中发生 OnClickEvent 时激活 CAB 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14128558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com