Android中listview长按弹出选择菜单的两种实现

使用ContextMenuPopupMenu均可实现

listview布局

<ListView
        android:layout_width="match_parent"
        android:layout_height="655dp"
        android:id="@+id/listView"></ListView>
private ListView listView;
listView = (ListView) findViewById(R.id.listView);

ContextMenu实现

registerForContextMenu(listView);//进行注册
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        filename=list.get(info.position).getName();//保存选中的文件名
        Toast.makeText(getApplicationContext(),"you choose the file : "+filename,Toast.LENGTH_SHORT).show();
        menu.add(0,0,0,"删除");
        menu.add(0,1,0,"分享");
    }
    //方法1对应
    @Override
    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case 0: {
                myDialog(filename);
                break;
            }
            case 1:{
                share(filename);
                break;
            }
        }
        return true;
    }

PopupMenu实现

 		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                File file= list.get(position);
                filename=file.getName();
                Toast.makeText(getApplicationContext(),"you choose the file : "+filename,Toast.LENGTH_SHORT).show();

                showPopupMenu(view);
                return true;
            }
        });
	private void showPopupMenu(View v){
        //定义PopupMenu对象
        PopupMenu popupMenu = new PopupMenu(this, v);
        //设置PopupMenu对象的布局
        popupMenu.getMenuInflater().inflate(R.menu.menu, popupMenu.getMenu());
        //设置PopupMenu的点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch(item.getItemId()){
                    case R.id.deleteFile:{
                        myDialog(filename);
                        break;
                    }
                    case R.id.shareFile:{
                        share(filename);
                        break;
                    }
                }

                return true;
            }
        });

        //显示菜单
        popupMenu.show();

    }

menu文件夹下新建menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/deleteFile" android:title="删除"></item>
    <item android:id="@+id/shareFile" android:title="分享"></item>

</menu>

效果展示

在这里插入图片描述
实验证明,ContextMenu效果更好,可以直接在所按下之处的正下方显示,但是PopupMenu在Item的正下方显示

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值