Android的GridView控件OnItemLongClick监听OnItemClick之后调用 [英] Android GridView OnItemLongClick listener called after OnItemClick

查看:743
本文介绍了Android的GridView控件OnItemLongClick监听OnItemClick之后调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想表现出不同的上下文菜单,当用户点击短或长的点击在网格视图中的单元格。这个问题我已经是,如果用户短点击OnItemClick监听器被调用,我看到调试器到达code,显示上下文菜单但不是从那里转移到onCreateContextMenu它去onItemLongClick。

我用一个布尔值prevent的长按code执行的是哪个呢prevent正在执行code,但即使这样做onCreateContextMenu不叫都已经试过了。

如果我删除onItemLongClick侦听短按一下监听器工作正常,并在上下文菜单中显示正确。

我知道其他人问类似这样的问题,但我还没有能够找到一个解决方案,工程。如果任何人都可以解决这个或点我在正确的方向,请让我知道,在此先感谢。赏金将被授予任何人谁,甚至可以点我在正确的方向。

这是的$ C $下听众的简化版本:

  mTGrid.setOnItemClickListener(新OnItemClickListener(){
            //这个监听器应该表现出很短的点击GridView控件的上下文菜单。
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
                    mRequiredMenu =标准;
                    parent.showContextMenuForChild(五);

            }
        });

        mTGrid.setOnItemLongClickListener(新OnItemLongClickListener(){
            //这个监听器应该表现出了很点击GridView控件的上下文菜单。
            @覆盖
            公共布尔onItemLongClick(适配器视图<>母公司,视图V,INT位置,长的id){
                    mRequiredMenu =选择;
                        parent.showContextMenuForChild(五);

            }
        });
 

解决方案

我知道你想显示短期的点击和长时间点击一个GridView项目不同的上下文菜单。

首先,你只需要设置监听器短按一下,因为默认的行为会自动显示在长点击右键菜单。

接下来,在OnItemClickListener设置一个布尔标志设置为true。默认值为false长期的点击。

最后,在onCreateContextMenu()检查,如果其短按一下,并显示出不同的上下文菜单(标准),并设置标志设置为false。否则让它显示默认的上下文菜单(选项)。

下面是一些code证明这个想法。

 公共类MainActivity延伸活动{

    私有静态最后的String [] ARR = {A,B,C,D,E,F,G,H,我};

    私人的GridView mTGrid;
    私人布尔isShort;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        mTGrid =(GridView控件)findViewById(R.id.gridView1);
        registerForContextMenu(mTGrid);

        mTGrid.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
                isShort =真;
                openContextMenu(视图);
            }
        });

        ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,R.layout.cell,ARR);
        mTGrid.setAdapter(适配器);
    }

    @覆盖
    公共无效onCreateContextMenu(文本菜单菜单,视图V,ContextMenuInfo menuInfo){
        AdapterView.AdapterContextMenuInfo信息=(AdapterView.AdapterContextMenuInfo)menuInfo;

        如果(isShort){
            。getMenuInflater()膨胀(R.menu.context_standard,菜单);
            menu.setHeaderTitle(标准菜单的+ ARR [info.position]);
            isShort = FALSE;
        }
        其他 {
            。getMenuInflater()膨胀(R.menu.context_options,菜单);
            menu.setHeaderTitle(为选项菜单+改编[info.position]);
        }
    }
}
 

示例应用程序: 你可以下载一个示例应用程序来查看行为。 <一href="http://www.appsrox.com/shared/GridExample_eclipse_project.zip">GridExample_eclipse_project

Basically I want to show a different context menu when the user short clicks or long clicks on a cell in the grid view. The issue I have is that if the user short clicks the OnItemClick listener is called and I see the debugger reach the code that shows the context menu but rather than moving from there to onCreateContextMenu it goes to onItemLongClick.

I have tried using a Boolean to prevent the long click code being executed which does prevent that code being executed, however even when this is done onCreateContextMenu is not called at all.

If I remove the onItemLongClick listener the short click listener works correctly and the context menu is shown correctly.

I know other people have asked questions similar to this but I still haven't been able to find a solution that works. If anyone can solve this or point me in the right direction please let me know, thanks in advance. Bounty will be awarded to anyone who can even point me in the right direction.

This is a simplified version of the code for the listeners:

        mTGrid.setOnItemClickListener(new OnItemClickListener() {
            //this listener should show the context menu for a short click on the gridview.
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    mRequiredMenu = "standard";
                    parent.showContextMenuForChild(v);      

            }
        });

        mTGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
            //this listener should show the context menu for a long click on the gridview.
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
                    mRequiredMenu = "options";
                        parent.showContextMenuForChild(v);      

            }
        });

解决方案

I understand you want to show different context menu for short clicks and long clicks on a GridView item.

First, you just need to set listener for short click since the default behavior will automatically show context menu on long clicks.

Next, set a boolean flag to true in the OnItemClickListener. The default value is false for long clicks.

Finally, in onCreateContextMenu() check if its a short click and show a different context menu (standard) and set flag to false. Else let it show the default context menu (options).

Here is some code to demonstrate the idea.

public class MainActivity extends Activity {

    private static final String[] arr = {"A", "B", "C", "D", "E", "F", "G", "H","I"};

    private GridView mTGrid;
    private boolean isShort;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTGrid = (GridView) findViewById(R.id.gridView1);
        registerForContextMenu(mTGrid);

        mTGrid.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                isShort = true;
                openContextMenu(view);
            }
        });

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.cell, arr);
        mTGrid.setAdapter(adapter);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

        if(isShort) {
            getMenuInflater().inflate(R.menu.context_standard, menu);
            menu.setHeaderTitle("Standard Menu for "+arr[info.position]);
            isShort = false;
        }
        else {
            getMenuInflater().inflate(R.menu.context_options, menu);
            menu.setHeaderTitle("Options Menu for "+arr[info.position]);
        }
    }   
}

Sample Application: You can download a sample application to see the behavior. GridExample_eclipse_project

这篇关于Android的GridView控件OnItemLongClick监听OnItemClick之后调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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