博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
listview中的item长按显示一排功能的菜单
阅读量:7276 次
发布时间:2019-06-29

本文共 8033 字,大约阅读时间需要 26 分钟。

hot3.png

项目说明.txt
1
实现在listview中的item长按,下面出现一排功能菜单
2.jpg
TestListViewItemActivity.java
1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
package com.lee.listViewitem; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.os.Vibrator; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; public class TestListViewItemActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {
private ListView noteBookslist; private NoteBookAdapter noteBookAdapter; private List
fileTypeList; private int currentPosition = -1; private int mSingleChoiceID = -1; private String[] mItems; @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.note_list); initView(); setData(); } private void initView() {
noteBookslist = (ListView) findViewById(R.id.notebook_list); noteBookslist.setOnItemClickListener(this); noteBookslist.setOnItemLongClickListener(this); } private void setData() {
fileTypeList = new ArrayList
(); for (int i = 0; i < 10; i++) {
fileTypeList.add("Test" + i); } if (noteBookAdapter == null) {
noteBookAdapter = new NoteBookAdapter(this, fileTypeList); noteBookslist.setAdapter(noteBookAdapter); } else {
noteBookAdapter.notifyDataSetChanged(); } } @Override protected void onResume() {
updateNoteBookList(); super.onResume(); } private void updateNoteBookList() {
if (noteBookAdapter != null) {
setData(); noteBookAdapter.notifyDataSetChanged(); } } @Override public void onItemClick(AdapterView
listview, View arg1, int position, long arg3) {
currentPosition = -1; noteBookAdapter.notifyDataSetChanged(); } @Override public boolean onItemLongClick(AdapterView
listview, View arg1, int position, long arg3) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); vibrator.vibrate(40); currentPosition = position; noteBookAdapter.notifyDataSetChanged(); return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish(); } return super.onKeyDown(keyCode, event); } private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
noteBookAdapter.notifyDataSetChanged(); } } }; class NoteBookAdapter extends BaseAdapter {
private Context context; private LayoutInflater inflater; private List
fileTypeList; public NoteBookAdapter(Activity activity, List
fileTypeList) {
this.context = activity; this.fileTypeList = fileTypeList; inflater = LayoutInflater.from(context); } public int getCount() {
return fileTypeList.size(); } public int getItemViewType(int position) {
return position; } public Object getItem(int arg0) {
return fileTypeList.get(arg0); } public long getItemId(int position) {
return position; } public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null; if (convertView == null) {
convertView = inflater.inflate(R.layout.item_comm_note, parent, false); holder = new ViewHolder(); holder.fileNameText = (TextView) convertView.findViewById(R.id.item_name); holder.fileTimeText = (TextView) convertView.findViewById(R.id.item_time); holder.fileSizeText = (TextView) convertView.findViewById(R.id.item_pm); holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.layout_other); holder.openLinearLayout = (LinearLayout) convertView.findViewById(R.id.item_open); holder.editLinearLayout = (LinearLayout) convertView.findViewById(R.id.item_edit); holder.moveLinearLayout = (LinearLayout) convertView.findViewById(R.id.item_move); holder.deleteLinearLayout = (LinearLayout) convertView.findViewById(R.id.item_delete); convertView.setTag(holder); } else {
holder = (ViewHolder) convertView.getTag(); } String strs = fileTypeList.get(position); holder.fileNameText.setText(strs); holder.fileTimeText.setText(""); holder.fileSizeText.setText(""); if (position == currentPosition) {
holder.linearLayout.setVisibility(View.VISIBLE); holder.openLinearLayout.setClickable(true); holder.editLinearLayout.setClickable(true); holder.moveLinearLayout.setClickable(true); holder.deleteLinearLayout.setClickable(true); holder.openLinearLayout.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
currentPosition = -1; } }); holder.editLinearLayout.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
// TODO Auto-generated method stub currentPosition = -1; } }); holder.moveLinearLayout.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(TestListViewItemActivity.this); mSingleChoiceID = -1; builder.setIcon(android.R.drawable.ic_dialog_info); builder.setTitle("�ƶ���"); builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mSingleChoiceID = whichButton; } }); builder.setPositiveButton("ȷ\t��", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (mSingleChoiceID > 0) {
currentPosition = -1; handler.sendEmptyMessage(1); } } }); builder.setNegativeButton("ȡ\t��", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
} }); builder.create().show(); } }); holder.deleteLinearLayout.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
// TODO Auto-generated method stub currentPosition = -1; fileTypeList.remove(position); handler.sendEmptyMessage(1); } }); } else {
holder.linearLayout.setVisibility(View.GONE); holder.openLinearLayout.setClickable(false); holder.editLinearLayout.setClickable(false); holder.moveLinearLayout.setClickable(false); holder.deleteLinearLayout.setClickable(false); } return convertView; } class ViewHolder {
public TextView fileNameText; public TextView fileTimeText; public TextView fileSizeText; public LinearLayout linearLayout; public LinearLayout openLinearLayout; public LinearLayout editLinearLayout; public LinearLayout moveLinearLayout; public LinearLayout deleteLinearLayout; } } }

转载于:https://my.oschina.net/u/1014520/blog/181212

你可能感兴趣的文章
Java URLClassLoader实现插件功能开发
查看>>
Thread线程的停止与Timer定时器的停止方法
查看>>
spring MVC自定义action名称
查看>>
python如何识别字符串有多少个中文字符
查看>>
MyEclipse10安装checkStyle与findBugs插件及基本使用
查看>>
MAC 安装更新 ANT
查看>>
微软新ML框架 interpret-尝试
查看>>
创建可序列化的自定义数据结构
查看>>
ios SDWebImage
查看>>
innobackupex部分备份
查看>>
mongodb查询速度慢是什么原因?
查看>>
shell top解析
查看>>
Spring RestTemplate 详解
查看>>
HTML5编程之旅 第5站Web Workers
查看>>
oracle 性能优化 02_OWI及性能视图
查看>>
<转>MySQL5.5数据库复制搭建报错之Could not initialize maste...
查看>>
职场老人谈:Linux学习分享
查看>>
针对Activity的启动模式理解1---standard模式
查看>>
用docker部署nginx+php环境时,访问php文件不执行
查看>>
第四次工业革命:自主经济的崛起
查看>>