文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android ListView长按弹出菜单二种实现方式示例

2022-06-06 10:32

关注

代码如下:

public class ListOnLongClickActivity extends Activity {
        private LinearLayout myListViewlayout;
        private ListView mListView;
        SimpleAdapter adapter;
        public int MID;

        // 创建一个List对象,用来存放列表项的每一行map信息
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

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

                myListViewlayout = (LinearLayout) findViewById(R.id.myListViewlayout);
                mListView = new ListView(this);
                // 创建布局参数
                LinearLayout.LayoutParams listviewParams = new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.FILL_PARENT,
                                LinearLayout.LayoutParams.FILL_PARENT);
                // 当滑动列表上,默认显示的黑色
                mListView.setCacheColorHint(Color.WHITE);
                // 将列表添加到流式布局myListViewlayout中
                myListViewlayout.addView(mListView, listviewParams);

                FillListData();

                // 列表现的单机事件
                mListView.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int position, long id) {
                               
                                Toast.makeText(ListOnLongClickActivity.this,
                                                "您选择的是" + list.get(position).get("name").toString(),
                                                Toast.LENGTH_SHORT).show();
                        }
                });

               
        //        ItemOnLongClick1();
                ItemOnLongClick2();
        }

        // 填充ListView资源
        private void FillListData() {

                adapter = new SimpleAdapter(ListOnLongClickActivity.this,
                                getListData(), R.layout.listviewrow, new String[] { "name",
                                                "price" }, new int[] { R.id.tv_name, R.id.tv_price });
                mListView.setAdapter(adapter);
        }

        private List<Map<String, Object>> getListData() {

                Map<String, Object> _map = new HashMap<String, Object>();
                _map.put("name", "小米");
                _map.put("price", "2350元");
                list.add(_map);

                _map = new HashMap<String, Object>();
                _map.put("name", "HTC G18");
                _map.put("price", "3340元");
                list.add(_map);

                _map = new HashMap<String, Object>();
                _map.put("name", "iphone 5");
                _map.put("price", "5450元");
                list.add(_map);

                _map = new HashMap<String, Object>();
                _map.put("name", "iPhone 4S");
                _map.put("price", "4650元");
                list.add(_map);

                _map = new HashMap<String, Object>();
                _map.put("name", "MOTO ME525");
                _map.put("price", "1345元");
                list.add(_map);
                return list;

        }

        private void ItemOnLongClick1() {
//注:setOnCreateContextMenuListener是与下面onContextItemSelected配套使用的
                mListView
                                .setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

                                        public void onCreateContextMenu(ContextMenu menu, View v,
                                                        ContextMenuInfo menuInfo) {
                                                menu.add(0, 0, 0, "购买");
                                                menu.add(0, 1, 0, "收藏");
                                                menu.add(0, 2, 0, "对比");

                                        }
                                });
        }

        // 长按菜单响应函数
        public boolean onContextItemSelected(MenuItem item) {

                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                                .getMenuInfo();
                MID = (int) info.id;// 这里的info.id对应的就是数据库中_id的值

                switch (item.getItemId()) {
                case 0:
                        // 添加操作
                        Toast.makeText(ListOnLongClickActivity.this,
                                        "添加",
                                        Toast.LENGTH_SHORT).show();
                        break;

                case 1:
                        // 删除操作
                        break;

                case 2:
                        // 删除ALL操作
                        break;

                default:
                        break;
                }

                return super.onContextItemSelected(item);

        }

        private void ItemOnLongClick2() {
                mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

                        @Override
                        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                        final int arg2, long arg3) {
                                list.remove(arg2);
                                new AlertDialog.Builder(ListOnLongClickActivity.this)
                                                .setTitle("对Item进行操作")
                                                .setItems(R.array.arrcontent,
                                                                new DialogInterface.OnClickListener() {
                                                                        public void onClick(DialogInterface dialog,
                                                                                        int which) {
                                                                                String[] PK = getResources()
                                                                      &nb sp;                         .getStringArray(
                                                                                                                R.array.arrcontent);
                                                                                Toast.makeText(
                                                                                                ListOnLongClickActivity.this,
                                                                                                PK[which], Toast.LENGTH_LONG)
                                                                                                .show();
                                                                                if (PK[which].equals("删除")) {
                                                                        & nbsp;               // 按照这种方式做删除操作,这个if内的代码有bug,实际代码中按需操作
                                                                                        list.remove(arg2);
                                                                                        adapter = (SimpleAdapter) mListView
                                                                                                        .getAdapter();
                                                                                        if (!adapter.isEmpty()) {
                                                                                                adapter.notifyDataSetChanged(); // 实现数据的实时刷新
                                                                                        }
                                                                         & nbsp;      }
                                                                        }
                                                                })
                                                .setNegativeButton("取消",
                                                                new DialogInterface.OnClickListener() {
                                                                        public void onClick(DialogInterface dialog,
                                                                                        int which) {
                                                                                // TODO Auto-generated method stub

                                                                        }
                                                                }).show();
                                return true;
                        }
                });

        }
}

 


-----------
listviewrow.xml

代码片段, <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/listviewbg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/tv_price"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black" />

</LinearLayout>

您可能感兴趣的文章:Android左右滑出菜单实例分析android底部菜单栏实现原理与代码android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍基于Android实现点击某个按钮让菜单选项从按钮周围指定位置弹出Android界面设计(APP设计趋势 左侧隐藏菜单右边显示content)Android开发技巧之我的菜单我做主(自定义菜单)Android仿QQ空间底部菜单示例代码Android实现原生侧滑菜单的超简单方式Android之用PopupWindow实现弹出菜单的方法详解Android实现爆炸式菜单按钮弹出效果


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯