Android ListView長(zhǎng)按彈出菜單二種實(shí)現(xiàn)方式示例
/**
* 知識(shí)點(diǎn)1:ListView item:兩種長(zhǎng)按彈出菜單方式
* 知識(shí)點(diǎn)2:ListView SimpleAdapter的使用
* 知識(shí)點(diǎn) 3:在java代碼中創(chuàng)建一個(gè)ListView
*/
public class ListOnLongClickActivity extends Activity {
private LinearLayout myListViewlayout;
private ListView mListView;
SimpleAdapter adapter;
public int MID;
// 創(chuàng)建一個(gè)List對(duì)象,用來(lái)存放列表項(xiàng)的每一行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);
// 當(dāng)滑動(dòng)列表上,默認(rèn)顯示的黑色
mListView.setCacheColorHint(Color.WHITE);
// 將列表添加到流式布局myListViewlayout中
myListViewlayout.addView(mListView, listviewParams);
FillListData();
// 列表現(xiàn)的單機(jī)事件
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
/*
* 點(diǎn)擊列表項(xiàng)時(shí)觸發(fā)onItemClick方法,四個(gè)參數(shù)含義分別為
* arg0:發(fā)生單擊事件的AdapterView
* arg1:AdapterView中被點(diǎn)擊的View
* position:當(dāng)前點(diǎn)擊的行在adapter的下標(biāo)
* id:當(dāng)前點(diǎn)擊的行的id
*/
Toast.makeText(ListOnLongClickActivity.this,
"您選擇的是" + list.get(position).get("name").toString(),
Toast.LENGTH_SHORT).show();
}
});
/**
* Item 長(zhǎng)按方式彈出菜單多選方式1
* Item 長(zhǎng)按方式彈出菜單多選方式2
* ItemOnLongClick1()與ItemOnLongClick2()不共存,按實(shí)際需要選擇
*/
// 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, "購(gòu)買(mǎi)");
menu.add(0, 1, 0, "收藏");
menu.add(0, 2, 0, "對(duì)比");
}
});
}
// 長(zhǎng)按菜單響應(yīng)函數(shù)
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
MID = (int) info.id;// 這里的info.id對(duì)應(yīng)的就是數(shù)據(jù)庫(kù)中_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("對(duì)Item進(jìn)行操作")
.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("刪除")) {
// 按照這種方式做刪除操作,這個(gè)if內(nèi)的代碼有bug,實(shí)際代碼中按需操作
list.remove(arg2);
adapter = (SimpleAdapter) mListView
.getAdapter();
if (!adapter.isEmpty()) {
adapter.notifyDataSetChanged(); // 實(shí)現(xiàn)數(shù)據(jù)的實(shí)時(shí)刷新
}
}
}
})
.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左右滑出菜單實(shí)例分析
- android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- android底部菜單欄實(shí)現(xiàn)原理與代碼
- 基于Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕讓菜單選項(xiàng)從按鈕周?chē)付ㄎ恢脧棾?/a>
- Android界面設(shè)計(jì)(APP設(shè)計(jì)趨勢(shì) 左側(cè)隱藏菜單右邊顯示content)
- Android開(kāi)發(fā)技巧之我的菜單我做主(自定義菜單)
- Android仿QQ空間底部菜單示例代碼
- android studio 的下拉菜單Spinner使用詳解
- Android實(shí)現(xiàn)原生側(cè)滑菜單的超簡(jiǎn)單方式
- Android學(xué)習(xí)之菜單的使用方法
相關(guān)文章
Android編程之監(jiān)聽(tīng)器用法實(shí)例分析
這篇文章主要介紹了Android編程之監(jiān)聽(tīng)器用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android監(jiān)聽(tīng)器的功能及針對(duì)短信的監(jiān)聽(tīng)與響應(yīng)操作技巧,需要的朋友可以參考下2016-01-01
Android 數(shù)據(jù)庫(kù)打包隨APK發(fā)布的實(shí)例代碼
有些時(shí)候我們的軟件用到SQLite數(shù)據(jù)庫(kù),這個(gè)時(shí)候怎么把一個(gè)做好的數(shù)據(jù)庫(kù)打包進(jìn)我們的APK呢2013-10-10
android實(shí)現(xiàn)簡(jiǎn)單的乘法計(jì)算代碼
本文完成輸入2個(gè)數(shù)相乘,并顯示其結(jié)果。共涉及到4個(gè)控件的使用學(xué)習(xí),輸入數(shù)字采用EditText,顯示結(jié)果用TextView,運(yùn)算按鈕button以及菜單中的退出鍵2013-11-11
Android布局之絕對(duì)布局AbsoluteLayout詳解
這篇文章主要為大家詳細(xì)介紹了Android布局之絕對(duì)布局AbsoluteLayout的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android采用File形式保存與讀取數(shù)據(jù)的方法
這篇文章主要介紹了Android采用File形式保存與讀取數(shù)據(jù)的方法,涉及Android文件流操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
Android利用方向傳感器獲得手機(jī)的相對(duì)角度實(shí)例說(shuō)明
下面以實(shí)例向大家介紹喜愛(ài)Android利用方向傳感器獲得手機(jī)的相對(duì)角度,不了解的朋友可以參考下2013-06-06
Android 用HttpURLConnection訪(fǎng)問(wèn)網(wǎng)絡(luò)的方法
下面小編就為大家分享一篇Android 用HttpURLConnection訪(fǎng)問(wèn)網(wǎng)絡(luò)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
實(shí)例詳解用戶(hù)輸入 i. 檢測(cè)常用手勢(shì)
通過(guò)本段代碼給大家介紹當(dāng)用戶(hù)輸入i檢測(cè)常用手勢(shì)的相關(guān)內(nèi)容,代碼簡(jiǎn)單易懂,感興趣的朋友一起學(xué)習(xí)吧2016-01-01

