不叫onMenuItemClick的第一次 - SherlockListActivity [英] doesn't call onMenuItemClick for first time - SherlockListActivity

查看:150
本文介绍了不叫onMenuItemClick的第一次 - SherlockListActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用SherlockListActivity, 我创建了动作条有一个项目和放大器;其clickListener 当我创建活动后点击它的第一次,不叫onMenuItemClick,但在后先点击每一次点击,工作没有问题

为什么?

 私人无效BuildTopActionBar(){
        BitmapDrawable BG =(BitmapDrawable)getResources()。getDrawable(
                R.drawable.ic_action_bg);
        bg.setTileModeXY(TileMode.REPEAT,TileMode.REPEAT);
        getSupportActionBar()setBackgroundDrawable(BG)。

        this.getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
        this.getSupportActionBar()setHomeButtonEnabled(真)。
        this.getSupportActionBar()的setTitle(书签)。
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            //这就是所谓的时候,家里(上)按钮pssed在操作栏$ P $。
            完();
            返回true;
        案例add_bookmark_item:
            // 加
            item.setOnMenuItemClickListener(新OnMenuItemClickListener(){

                公共布尔onMenuItemClick(菜单项项){
                    customAmbilWarnaDialog对话框=新customAmbilWarnaDialog(
                            BookMarksActivity.this,为0xffffffff,
                            新OnAmbilWarnaListener(){

                                公共无效OnCancel的(
                                        customAmbilWarnaDialog对话框){
                                }

                                公共无效的OnOK(customAmbilWarnaDialog对话框,
                                        INT的颜色,字符串名称,诠释hasName){
                                    myDbHelper.AddNewBook​​mark(姓名,
                                            currentPageNum,颜色,hasName);
                                    // code
                                    lv.smoothScrollToPosition((lv.getCount() -  1));
                                }
                            });
                    dialog.show();
                    返回true;
                }
            });

        }

        返回super.onOptionsItemSelected(项目);

    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){

        menu.add(0,add_bookmark_item,0,添加书签)
                .setIcon(R.drawable.bookmark_add)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                                | MenuItem.SHOW_AS_ACTION_ALWAYS);

        返回true;
    }
 

解决方案

的原因是,我们在调用 item.setOnMenuItemClickListener(新OnMenuItemClickListener(){... onOptionsItemSelected()方法。

这意味着它将被设置在第一时间点击菜单项(然后重置每一次点击)和里面的code将只运行一次,你'已经点击它至少一次。

或者调用这 onCreateOptionsMenu()或只需移动从 onMenuItemClick的code()的开关/箱

I use SherlockListActivity, I created ActionBar with one item & its clickListener when i click on it for first time after creating activity, doesn't call onMenuItemClick, but in every click after first click, work with no problem

Why ?

private void BuildTopActionBar() {
        BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(
                R.drawable.ic_action_bg);
        bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
        getSupportActionBar().setBackgroundDrawable(bg);

        this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.getSupportActionBar().setHomeButtonEnabled(true);
        this.getSupportActionBar().setTitle("bookmarks");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This is called when the Home (Up) button is pressed in the Action Bar.
            finish();
            return true;
        case add_bookmark_item:
            // add
            item.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                public boolean onMenuItemClick(MenuItem item) {
                    customAmbilWarnaDialog dialog = new customAmbilWarnaDialog(
                            BookMarksActivity.this, 0xffffffff,
                            new OnAmbilWarnaListener() {

                                public void onCancel(
                                        customAmbilWarnaDialog dialog) {
                                }

                                public void onOk(customAmbilWarnaDialog dialog,
                                        int color, String name, int hasName) {
                                    myDbHelper.AddNewBookmark(name,
                                            currentPageNum, color, hasName);
                                    // code
                                    lv.smoothScrollToPosition((lv.getCount() - 1));
                                }
                            });
                    dialog.show();                  
                    return true;
                }
            });

        }

        return super.onOptionsItemSelected(item);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(0, add_bookmark_item, 0, "add bookmark")
                .setIcon(R.drawable.bookmark_add)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                                | MenuItem.SHOW_AS_ACTION_ALWAYS);

        return true;
    }

解决方案

the reason is that you're calling the item.setOnMenuItemClickListener(new OnMenuItemClickListener() {... in the onOptionsItemSelected() method.

This means it will be set the first time you click the menuItem (and then reset on every click) and the code inside it will only run once you've clicked it at least once.

either call this in onCreateOptionsMenu() or simply move the code from onMenuItemClick() to the switch/case

这篇关于不叫onMenuItemClick的第一次 - SherlockListActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆