GridView Android中的上下文菜单

时间:2014-01-04 11:02:35

标签: android android-contextmenu

我有GridView

GridView gridView = (GridView) findViewById(R.id.grid_view);

// Instance of ImageAdapter Class
final String [] imageNames = {"knee_kg","knee_ks","knee_kp","knee_kg_90", 
                              "pipe_knee", "cover", "funnel", "crater"};
Integer[] mThumbIds = new Integer[imageNames.length];
for (int i = 0; i<imageNames.length; i++){
    Integer resID = getResources().getIdentifier(imageNames[i], "drawable", 
                                                 getPackageName());
    mThumbIds[i] = resID;
}

OnItemCliclListener

gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
        Class clazz = null;
        String className = imageNameToUpper(imageNames[position]);

        try {
            clazz = Class.forName("com.example.pipecalculator.activities." + className);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Intent intentClass = new Intent(getApplicationContext(), clazz);
        startActivity(intentClass);
    }
});

我只想在两个项目点击上创建上下文菜单:"knee_ks""knee_kp"。 另一个是:

Intent intentClass = new Intent(getApplicationContext(), clazz);
startActivity(intentClass);

有可能吗?

3 个答案:

答案 0 :(得分:2)

首先注册 gridView 以获取上下文菜单

 registerForContextMenu(grid);

然后覆盖功能

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    getMenuInflater().inflate(R.menu.context_menu, menu);
    GridView gv = (GridView) v;
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    int position = info.position;
            if(!(position==0 || position==2))
             {
             menu.close();
             )
}

上下文菜单监听器

     @Override
public boolean onContextItemSelected(MenuItem item) {
   return true;             
      }

答案 1 :(得分:1)

这是可能的。
您应该使用int position

gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                    if (position == 0 || position == 2 /*and other, or just: position < 5*/)
                    { 
                       //Code for positio 0 and 2
                    }
                    else
                    {
                       //Code for othe positions
                    }
                }   
            });

使用this example显示上下文菜单。

答案 2 :(得分:0)

这似乎是一个古老的话题,但是我面临着同样的问题,并且想放弃对我有用的东西。基于@Abhishek命题,我所做的只是将ifc(strip(format) is missing,strip(name),'put('|| strip(name) ||','|| strip(format) ||')') 语句替换为/*edit sashelp.shoes with missing values in Product as test-cases*/ proc sql noprint; create table wocatx as select * from SASHELP.SHOES; update wocatx set Product = ''; quit; /*Macro variable for catx*/ proc sql; select ifc(strip(format) is missing,strip(name),ifc(type='num','put('|| strip(name) ||','|| strip(format) ||')','input('|| strip(name) ||','|| strip(format) ||')')) into :varstr2 separated by ',' from dictionary.columns where libname = "WORK" and memname = "WOCATX"; quit; /*data step with catx*/ data stuff; format all $5000.; set work.wocatx ; all = catx(',',&varstr2.) ; put all; run; /*Macro variable for concat function (||)*/ proc sql; select ifc(strip(format) is missing, 'strip(' || strip(name) || ')', 'strip(put('|| strip(name) ||','|| strip(format) ||'))') into :varstr3 separated by "|| ',' ||" from dictionary.columns where libname = "WORK" and memname = "WOCATX"; quit; /*Data step without catx*/ data stuff1; format all $5000.; set work.wocatx ; all = &varstr3. ; put all; run; 并瞧瞧!上下文仅出现在位置0和2上。

menu.close()