CAB激活菜单时OnClickEvent发生在安卓菜单、发生在、CAB、OnClickEvent

2023-09-12 02:19:19 作者:执灯一盏

试图从动作条上的菜单项时,点击激活CAB菜单。这里是我设置GridView控件用来听多选择。该multiModeChoiceListener工作正常时,就在GridView的任何项目我长preSS。这是工作的罚款。现在我有一个要求,什么时候preSS在操作栏的菜单项激活CAB菜单。一旦它pssed $ P $,在CAB菜单应阅读0项被选中。之后,它应该允许我从GridView的单点击选择项目。我怎样才能实现这个功能?

GridView控件设置监听器:

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

MultiChoiceModeListener.java

 公共类MultiChoiceModeListener工具
    GridView.MultiChoiceModeListener {

公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
    。mode.getMenuInflater()膨胀(R.menu.featured_multiselect,菜单);
    菜单项MI = menu.findItem(R.id.close);
    mi.setIcon(R.drawable.cancel);
    mode.setTitle(选择项);
    返回true;
}

在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
    返回true;
}

公共布尔onActionItemClicked(ActionMode模式,菜单项项){
    Toast.makeText(getApplicationContext(),item.getTitle(),
            Toast.LENGTH_SHORT).show();
    如果(item.getTitle()的toString()。equalsIgnoreCase(关闭)){
        mode.finish();
    }
    返回true;
}

公共无效onDestroyActionMode(ActionMode模式){
    新ChangeNotifier()changeOnFavoriteStore = TRUE;
    新AddFavorites()执行(增加,DEVICE_ID,dataArray中);
    如果(通知==​​真){
        Toast.makeText(getApplicationContext(),
                所选项目将被添加到收藏夹,
                Toast.LENGTH_SHORT).show();
        通知= FALSE;
    }
}

公共无效onItemCheckedStateChanged(ActionMode模式,INT位置,
        长ID,布尔检查){
    INT selectCount = gridView.getCheckedItemCount();
    如果(selectCount大于0){
        通知= TRUE;
        dataArray.add(位置);
        开关(selectCount){
        情况1:
            mode.setSubtitle(一个项目添加到收藏夹);
            打破;
        默认:
            mode.setSubtitle(+ selectCount
                    )+添加到收藏夹项目;
            打破;
        }
    }

   }
 

OnMenuItemClick方式:

 在$ P $公共布尔ppareOptionsMenu(最后菜单菜单){

    最终的菜单项editItem = menu.findItem(R.id.editit);

    editItem.setOnMenuItemClickListener(新OnMenuItemClickListener(){

        @覆盖
        公共布尔onMenuItemClick(菜单项项){
            //在CAB菜单应该在这里启动。因此,它读取0项动作条中选择

            返回false;
        }

    });
 
Brevent下载 Brevent 安卓版v2.0.8

解决方案

从你的问题我明白,你想从点击的一个启动的GridView 相关CAB的菜单项。我不知道你是否能做到这一点(但我可能是错误的)作为 MultiChoiceModeListener 预计的要检查的项目启动。根据您的布局和的GridView 的整体外观,我想你可以在该的GridView (没有内容显示),并使用 setItemChecked(dummyItemPosition,真)启动的GridView CAB。当然,你需要有额外的逻辑来照顾额外的项目,在你的 MultiChoiceModeListener

 公共无效onItemCheckedStateChanged(ActionMode模式,INT位置,
        长ID,布尔检查){
    如果(位置== theDummyPosition)
         返回; //所以我们开始CAB但目前还没有检查任何项目
    }
    INT selectCount = gridView.getCheckedItemCount();
    如果(selectCount大于0){
        通知= TRUE;
        dataArray.add(位置);
        //如果您选择另一个项目,你将有两个选择项(由于虚拟物品),所以你需要照顾它
        开关(selectCount){
        情况1:
            mode.setSubtitle(一个项目添加到收藏夹);
            打破;
        默认:
            mode.setSubtitle(+ selectCount
                    )+添加到收藏夹项目;
            打破;
        }
    }

   }
 

上面的解决方案是一个黑客,它很可能会更容易失去 MultiChoiceModeListener ,简单地启动 ActionMode 可以操作两种情况。

Trying to activate CAB menu when clicking on MenuItem from ActionBar. Here is how I set the GridView for listening to Multi Choice. The multiModeChoiceListener is working fine when I long press on Any item in the GridView. It is working fine. Now I have a requirement to activate the CAB menu when do press on a menu item in Action Bar. Once it is pressed, the CAB menu should read that 0 items are selected. After that it should allow me to select items from GridView on single clicks. How can I achieve this feature?

GridView set listener:

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 method:

 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;
        }

    });

解决方案

From your question I understand that you're trying to start the GridView associated CAB from clicking one of the menu items. I don't know if you can do this(but I may be mistaken) as the MultiChoiceModeListener expects an item to be checked to start. Depending on your layout and the overall appearance of the GridView, I think you could have a dummy item(as an extra item in the adapter) at the end of the GridView(with no content showing) and use setItemChecked(dummyItemPosition, true) to start the GridView CAB. Of course you'll need to have additional logic to take care of that extra item in your 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;
        }
    } 

   }

The solution above is a hack, most likely it would be much easier to lose the MultiChoiceModeListener and simply start an ActionMode that you can manipulate for both situations.