Context menu in GridView Android

最后都变了- 提交于 2019-12-25 02:31:14

问题


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

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

I want create Context menu only on two item click: "knee_ks","knee_kp". On the other is to be:

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

It's possible?


回答1:


First Register your gridView for context menu

 registerForContextMenu(grid);

and then override a function

@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();
             )
}

Context Menu Listener

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



回答2:


It's possible.
You should use 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
                    }
                }   
            });

Use this example for showing context menu.




回答3:


It seems to be an old topic but I faced the same issue and would like to drop what works for me. Based on @Abhishek proposition, what I did is just replace the menu.close() statement by menu.clear() and voilà! the contextual appears only on position 0 and 2.

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

    getMenuInflater().inflate(R.menu.context_menu, menu);

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    int position = info.position;
            if(!(position==0 || position==2)){
                   menu.clear();
             }
}


来源:https://stackoverflow.com/questions/20920109/context-menu-in-gridview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!