欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ListView長按彈出菜單二種實現(xiàn)方式示例

 更新時間:2013年11月29日 14:28:37   作者:  
這篇文章主要介紹了Android ListView長按彈出菜單的方法,大家參考實現(xiàn)

復(fù)制代碼 代碼如下:

/**

* 知識點1:ListView item:兩種長按彈出菜單方式
* 知識點2:ListView SimpleAdapter的使用
* 知識點 3:在java代碼中創(chuàng)建一個ListView
*/

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

        // 創(chuàng)建一個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);
                // 創(chuàng)建布局參數(shù)
                LinearLayout.LayoutParams listviewParams = new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.FILL_PARENT,
                                LinearLayout.LayoutParams.FILL_PARENT);
                // 當滑動列表上,默認顯示的黑色
                mListView.setCacheColorHint(Color.WHITE);
                // 將列表添加到流式布局myListViewlayout中
                myListViewlayout.addView(mListView, listviewParams);

                FillListData();

                // 列表現(xiàn)的單機事件
                mListView.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int position, long id) {
                                /*
                                 * 點擊列表項時觸發(fā)onItemClick方法,四個參數(shù)含義分別為
                                 * arg0:發(fā)生單擊事件的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, "對比");

                                        }
                                });
        }

        // 長按菜單響應(yīng)函數(shù)
        public boolean onContextItemSelected(MenuItem item) {

                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                                .getMenuInfo();
                MID = (int) info.id;// 這里的info.id對應(yīng)的就是數(shù)據(jù)庫中_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內(nèi)的代碼有bug,實際代碼中按需操作
                                                                                        list.remove(arg2);
                                                                                        adapter = (SimpleAdapter) mListView
                                                                                                        .getAdapter();
                                                                                        if (!adapter.isEmpty()) {
                                                                                                adapter.notifyDataSetChanged(); // 實現(xiàn)數(shù)據(jù)的實時刷新
                                                                                        }
                                                                                }
                                                                        }
                                                                })
                                                .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>

相關(guān)文章

  • Android 中為什么要用Fragment.setArguments(Bundle bundle)來傳遞參數(shù)

    Android 中為什么要用Fragment.setArguments(Bundle bundle)來傳遞參數(shù)

    這篇文章主要介紹了Android 中為什么要用Fragment.setArguments(Bundle bundle)來傳遞參數(shù),非常不錯,具有參考借鑒價值,需要的朋友參考下
    2017-01-01
  • Android自定義模擬時鐘控件

    Android自定義模擬時鐘控件

    這篇文章主要為大家詳細介紹了Android自定義模擬時鐘控件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下<BR>
    2022-01-01
  • Android自定義View之酷炫圓環(huán)(二)

    Android自定義View之酷炫圓環(huán)(二)

    這篇文章主要介紹了Android自定義View之酷炫圓環(huán),本文是第二篇針對Android圓環(huán)實現(xiàn)方法進行的詳細闡述,感興趣的小伙伴們可以參考一下
    2016-01-01
  • OnSharedPreferenceChangeListener詳解及出現(xiàn)不觸發(fā)解決辦法

    OnSharedPreferenceChangeListener詳解及出現(xiàn)不觸發(fā)解決辦法

    本文主要介紹 Android OnSharedPreferenceChangeListener的知識,在Android應(yīng)用開發(fā)過程中會遇到監(jiān)聽器不觸發(fā)事件問題,這里介紹了相應(yīng)的解決辦法
    2016-08-08
  • Kotlin實現(xiàn)Android系統(tǒng)懸浮窗詳解

    Kotlin實現(xiàn)Android系統(tǒng)懸浮窗詳解

    大家好,本篇文章主要講的是Kotlin實現(xiàn)Android系統(tǒng)懸浮窗詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Android通過Socket與服務(wù)器之間進行通信的示例

    Android通過Socket與服務(wù)器之間進行通信的示例

    這篇文章主要介紹了Android通過Socket與服務(wù)器之間進行通信的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 詳解flutter如何實現(xiàn)局部導(dǎo)航管理

    詳解flutter如何實現(xiàn)局部導(dǎo)航管理

    這篇文章主要為大家介紹了詳解flutter如何實現(xiàn)局部導(dǎo)航管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Android中BaseAdapter的用法分析與理解

    Android中BaseAdapter的用法分析與理解

    這篇文章主要介紹了Android中BaseAdapter的用法分析與理解,結(jié)合一個項目開發(fā)中BaseAdapter的使用分析了BaseAdapter的功能、作用及用法理解,需要的朋友可以參考下
    2016-08-08
  • Android TextView對齊的兩種方法

    Android TextView對齊的兩種方法

    這篇文章主要介紹了Android TextView對齊的兩種方法的相關(guān)資料,在開發(fā)Android APP 的時候經(jīng)常會用到TextView 輸入用戶信息或者其他信息,總是不能對齊,這里提供兩種方法,需要的朋友可以參考下
    2017-07-07
  • Android利用控制點的拖拽畫一個粽子

    Android利用控制點的拖拽畫一個粽子

    端午節(jié)就要到了,本文我們將利用控制點的拖拽式移動,動態(tài)調(diào)整位置來調(diào)整繪制一個簡單的粽子圖形,感興趣的小伙伴可以跟隨小編一起動手嘗試一下
    2022-05-05

最新評論