从自定义适配器的getView显示上下文菜单

时间:2014-05-01 22:26:56

标签: android android-listview imageview contextmenu adapter

我有一个自定义列表视图,每个项目都有可点击的imageview。我想打开一个上下文菜单,当单击这些imageview中的一些时,我想我需要在我的适配器的getView()方法内的imageview的单击监听器内执行此操作,但我不确定如何。有什么指针吗?

2 个答案:

答案 0 :(得分:0)

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    //Inflate convertView. 
    if (convertView == null) {
        convertView = (RelativeLayout) mLayoutInflater.inflate(
                 R.layout.list_item_layout, parent, false);
    }
    //modify view here. 
    ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
    imageView.setOnClickListener(new View.OnClickListener() {
        showMenuDialog();
    });
    return convertView;
}

private void showMenuDialog()
{
    //You can pass this context when initializing the adapter. 
    Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.dialogLayout);
    //The dialogLayout here contains a ListView of the items you want in the menu. 
    ListView menuList = (ListView) dialog.findViewById(R.id.menuList);
    //Update listView with the menu items. 
    menuList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id)         {
            switch(position)
            {
                case 1:
                // Define what first menu item should do.
                dialog.dismiss();
                break;
                //Similarly add more cases. 
            }
        }
    });
    dialog.show();
}

答案 1 :(得分:0)

Try this hope it will help you:

@Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        CellItem cellItem = null;

        if(convertView == null){

            cellItem = new CellItem();
             convertView = (RelativeLayout) mLayoutInflater.inflate(
                 R.layout.list_item_layout, parent, false);
            cellItem.imageView=(ImageView) convertView.findViewById(R.id.imageView);
            cellItem.imageView.setOnClickListener(cellItem.imageClick);
            convertView.setTag(cellItem);
        }else{
            cellItem= (CellItem) convertView.getTag();
        }
}
class CellItem{
    ImageView imageView;

    public OnClickListener imageClick=new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.dialogLayout);
                //The dialogLayout here contains a ListView of the items you want in the menu. 
                ListView menuList = (ListView) dialog.findViewById(R.id.menuList);
                //Update listView with the menu items. 
                menuList.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,int position, long id)         {
                        switch(position)
                        {
                            case 1:
                            // Define what first menu item should do.
                            dialog.dismiss();
                            break;
                            //Similarly add more cases. 
                        }
                    }
                });
                dialog.show();
            }
        };
}