当前位置:  编程技术>移动开发

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

    来源: 互联网  发布时间:2014-10-22

    本文导语:  代码如下:/** * 知识点1:ListView item:两种长按弹出菜单方式* 知识点2:ListView SimpleAdapter的使用* 知识点 3:在java代码中创建一个ListView*/ public class ListOnLongClickActivity extends Activity {         private LinearLayout myListViewlayout;    ...

代码如下:

/**

* 知识点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 list = new ArrayList();

        @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 getListData() {

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

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

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

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

                _map = new HashMap();
                _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

代码片段,

   

   



    
 
 

您可能感兴趣的文章:

  • Android开发:TextView加入滚动条示例
  • android开发教程之switch控件使用示例
  • android开启免提切换功能示例
  • Android开发之注册登录方法示例
  • 手写android布局示例
  • Android示例程序 apps-for-android
  • android网络编程之android连接网络的简单示例代码
  • android获取当前手机号示例程序
  • android读取assets文件示例
  • android读取raw文件示例
  • android实现来电静音示例(监听来电)
  • android开机自启动app示例分享
  • android开发教程之自定义控件checkbox的样式示例
  • Android创建文件实现对文件监听示例
  • android输入框与文本框加滚动条scrollview示例
  • android保存Bitmap图片到指定文件夹示例
  • android播放gif格式图片示例
  • Android获取apk程序签名信息代码示例
  • android中设置TextView/Button 走马灯(Marquee)效果示例
  • android教程使用webview访问https的url处理sslerror示例
  • Android Horizontal ListView
  • android ListView的右边滚动滑块启用方法 分享
  • android ListView自动滚动方法
  • android listview 水平滚动和垂直滚动的小例子
  • 滑动刷新的ListView Android PullToRefresh
  • android中如果为listview的item添加listener,也能为item中的button添加listener
  • android中ListView多次刷新重复执行getView的解决方法
  • Android笔记之:在ScrollView中嵌套ListView的方法
  • android ListView 一些重要属性详解
  • Android用ListView显示SDCard文件列表的小例子
  • Android入门之ListView应用解析(一)
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • Android 导航菜单 RibbonMenu
  • android 自定义Android菜单背景的代码
  • android实现始终显示overflow菜单的方法
  • Android动态添加menu菜单的简单方法
  • 用Android MenuInflater创建菜单项的方法步骤
  • Android界面设计(APP设计趋势 左侧隐藏菜单右边显示content)
  • Android Spinner 下拉菜单的使用
  • android游戏载入的activity跳转到游戏主菜单的activity具体实现
  • android教程之使用popupwindow创建菜单示例
  • android在连拍菜单中增加连拍张数选项功能实现代码
  • android 如何在 主菜单 来一键点图标, 播放视频(产品广告),只要驱动部分的JAVA层我不写
  • Android 2.3.7.r1 camera录像过程中按menu菜单键时会停止录像
  • 在Android中创建菜单项Menu以及获取手机分辨率的解决方法
  • Android开发技巧之我的菜单我做主(自定义菜单)
  • Android 关机弹出选择菜单的深入解析
  • Android滑动式菜单 SlidingMenu
  • Android仿UC底部菜单栏实现原理与代码
  • android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍
  • Android提高之多级树形菜单的实现方法
  • android底部菜单栏实现原理与代码
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥)
  • android中如果为listview的item添加listener,也能为item中的button添加listener iis7站长之家
  • Android开发需要的几点注意事项总结
  • Android系统自带样式 (android:theme)
  • android 4.0 托管进程介绍及优先级和回收机制
  • Android网络共享软件 Android Wifi Tether
  • Android访问与手机通讯相关类的介绍
  • Android 图标库 Android GraphView
  • Android及andriod无线网络Wifi开发的几点注意事项
  • 轻量级Android开发工具 Android Tools
  • Android 2.3 下StrictMode介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3