欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-18 12:12:40
复制代码 代码如下:/** * 知识点1:listview item:两种长按弹出菜单方式* 知识点2:listview simpleadapter的使用* 知...

复制代码 代码如下:

/**

* 知识点1:listview item:两种长按弹出菜单方式
* 知识点2:listview simpleadapter的使用
* 知识点 3:在java代码中创建一个listview
*/

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) {
                                /*
                                 * 点击列表项时触发onitemclick方法,四个参数含义分别为
                                 * arg0:发生单击事件的adapterview
                                 * arg1:adapterview中被点击的view 
                                 * position:当前点击的行在adapter的下标
                                 * id:当前点击的行的id
                                 */
                                toast.maketext(listonlongclickactivity.this,
                                                "您选择的是" + list.get(position).get("name").tostring(),
                                                toast.length_short).show();
                        }
                });

                /**
                 * item 长按方式弹出菜单多选方式1
                 * item 长按方式弹出菜单多选方式2
                 * itemonlongclick1()与itemonlongclick2()不共存,按实际需要选择
                 */
        //        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()
                                                                                                .getstringarray(
                                                                                                                r.array.arrcontent);
                                                                                toast.maketext(
                                                                                                listonlongclickactivity.this,
                                                                                                pk[which], toast.length_long)
                                                                                                .show();
                                                                                if (pk[which].equals("删除")) {
                                                                                        // 按照这种方式做删除操作,这个if内的代码有bug,实际代码中按需操作
                                                                                        list.remove(arg2);
                                                                                        adapter = (simpleadapter) mlistview
                                                                                                        .getadapter();
                                                                                        if (!adapter.isempty()) {
                                                                                                adapter.notifydatasetchanged(); // 实现数据的实时刷新
                                                                                        }
                                                                                }
                                                                        }
                                                                })
                                                .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>