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

 更新时间:2013年11月29日 14:28:37   作者:  
这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现

复制代码 代码如下:

/**

* 知识点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>

相关文章

  • Android 中为什么要用Fragment.setArguments(Bundle bundle)来传递参数

    Android 中为什么要用Fragment.setArguments(Bundle bundle)来传递参数

    这篇文章主要介绍了Android 中为什么要用Fragment.setArguments(Bundle bundle)来传递参数,非常不错,具有参考借鉴价值,需要的朋友参考下
    2017-01-01
  • Android自定义模拟时钟控件

    Android自定义模拟时钟控件

    这篇文章主要为大家详细介绍了Android自定义模拟时钟控件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下<BR>
    2022-01-01
  • Android自定义View之酷炫圆环(二)

    Android自定义View之酷炫圆环(二)

    这篇文章主要介绍了Android自定义View之酷炫圆环,本文是第二篇针对Android圆环实现方法进行的详细阐述,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • OnSharedPreferenceChangeListener详解及出现不触发解决办法

    OnSharedPreferenceChangeListener详解及出现不触发解决办法

    本文主要介绍 Android OnSharedPreferenceChangeListener的知识,在Android应用开发过程中会遇到监听器不触发事件问题,这里介绍了相应的解决办法
    2016-08-08
  • Kotlin实现Android系统悬浮窗详解

    Kotlin实现Android系统悬浮窗详解

    大家好,本篇文章主要讲的是Kotlin实现Android系统悬浮窗详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • Android通过Socket与服务器之间进行通信的示例

    Android通过Socket与服务器之间进行通信的示例

    这篇文章主要介绍了Android通过Socket与服务器之间进行通信的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • 详解flutter如何实现局部导航管理

    详解flutter如何实现局部导航管理

    这篇文章主要为大家介绍了详解flutter如何实现局部导航管理示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android中BaseAdapter的用法分析与理解

    Android中BaseAdapter的用法分析与理解

    这篇文章主要介绍了Android中BaseAdapter的用法分析与理解,结合一个项目开发中BaseAdapter的使用分析了BaseAdapter的功能、作用及用法理解,需要的朋友可以参考下
    2016-08-08
  • Android TextView对齐的两种方法

    Android TextView对齐的两种方法

    这篇文章主要介绍了Android TextView对齐的两种方法的相关资料,在开发Android APP 的时候经常会用到TextView 输入用户信息或者其他信息,总是不能对齐,这里提供两种方法,需要的朋友可以参考下
    2017-07-07
  • Android利用控制点的拖拽画一个粽子

    Android利用控制点的拖拽画一个粽子

    端午节就要到了,本文我们将利用控制点的拖拽式移动,动态调整位置来调整绘制一个简单的粽子图形,感兴趣的小伙伴可以跟随小编一起动手尝试一下
    2022-05-05

最新评论