博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ListView项目长按的菜单功能总结
阅读量:6115 次
发布时间:2019-06-21

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

hot3.png

ListView上的Item长按弹出菜单或者对话框是常见的功能之一:

其实现如下:

extends ListActivity{

在方法:

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.localfilebrowser);
registerForContextMenu(this.getListView());

增加方法:

[java]
  1. @Override
  2. public void onCreateContextMenu(ContextMenu menu, View view,
  3. ContextMenuInfo menuInfo) {
  4. AdapterView.AdapterContextMenuInfo info;
  5. try {
  6. info = (AdapterView.AdapterContextMenuInfo) menuInfo;
  7. } catch (ClassCastException e) {
  8. Log.e("test", "bad menuInfo", e);
  9. return;
  10. }
  11. currentPath = new File(paths.get(info.position));
  12. showDialog(UPDATE_FILEPATH);
  13. }
@Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { AdapterView.AdapterContextMenuInfo info; try { info = (AdapterView.AdapterContextMenuInfo) menuInfo; } catch (ClassCastException e) { Log.e("test", "bad menuInfo", e); return; } currentPath = new File(paths.get(info.position)); showDialog(UPDATE_FILEPATH); }

本文使用了弹出式窗口,如果是菜单的话,需要增加如下代码

[java]
  1. @Override
  2. public boolean onContextItemSelected(MenuItem item) {
  3. super.onContextItemSelected(item);
  4. AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
  5. .getMenuInfo();
  6. // Remember current selection
  7. IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter();
  8. if (adapter == null) {
  9. return false;
  10. }
  11. return true;
  12. }
@Override public boolean onContextItemSelected(MenuItem item) { super.onContextItemSelected(item); AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item .getMenuInfo(); // Remember current selection IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter(); if (adapter == null) { return false; } return true; }

showDialog(UPDATE_FILEPATH);对应于如下代码

[java]
  1. @Override
  2. protected Dialog onCreateDialog(int id) {
  3. switch (id) {
  4. case UPDATE_FILEPATH:
  5. LayoutInflater inflater = LayoutInflater.from(this);
  6. View view = inflater.inflate(R.layout.dialog_change_filepath, null);
  7. final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath);
  8. final TextView newet = (TextView) view.findViewById(R.id.newfilepath);
  9. prefs = PreferenceManager.getDefaultSharedPreferences(this);
  10. oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", ""));
  11. if(currentPath!=null)
  12. newet.setText(newet.getText()+currentPath.getAbsolutePath());
  13. return new AlertDialog.Builder(this).setIcon(
  14. android.R.drawable.ic_dialog_alert).setTitle(
  15. R.string.dialog_filepath).setView(view)
  16. .setPositiveButton(android.R.string.ok,
  17. new OnClickListener() {
  18. public void onClick(DialogInterface dialog,
  19. int which) {
  20. Editor editor = prefs.edit();
  21. editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath());
  22. editor.commit();
  23. ListOSDir.this.finish();
  24. }
  25. }).setNegativeButton(android.R.string.cancel,
  26. new OnClickListener() {
  27. public void onClick(DialogInterface dialog,
  28. int which) {
  29. // Cancel should not do anything.
  30. removeDialog(UPDATE_FILEPATH);
  31. }
  32. }).create();
  33. }
  34. return null;
  35. }
@Override protected Dialog onCreateDialog(int id) { switch (id) { case UPDATE_FILEPATH: LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.dialog_change_filepath, null); final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath); final TextView newet = (TextView) view.findViewById(R.id.newfilepath); prefs = PreferenceManager.getDefaultSharedPreferences(this); oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", "")); if(currentPath!=null) newet.setText(newet.getText()+currentPath.getAbsolutePath()); return new AlertDialog.Builder(this).setIcon( android.R.drawable.ic_dialog_alert).setTitle( R.string.dialog_filepath).setView(view) .setPositiveButton(android.R.string.ok, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Editor editor = prefs.edit(); editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath()); editor.commit(); ListOSDir.this.finish(); } }).setNegativeButton(android.R.string.cancel, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Cancel should not do anything. removeDialog(UPDATE_FILEPATH); } }).create(); } return null; }

其中:removeDialog(UPDATE_FILEPATH); 是解决

因为create 一个dialog的开销很大,所以android会先查找有没有dialog实例存在,如果有,不create,prepared之后就show,弹出窗口一直都相同的问题

整个程序是选择一个目录来设置下载需要存储的路径的,代码如下:

[java]
  1. package cn.cnic.cerc.moblie.clbclient.filemanager;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import android.app.AlertDialog;
  6. import android.app.Dialog;
  7. import android.app.ListActivity;
  8. import android.content.DialogInterface;
  9. import android.content.SharedPreferences;
  10. import android.content.DialogInterface.OnClickListener;
  11. import android.content.SharedPreferences.Editor;
  12. import android.os.Bundle;
  13. import android.os.Environment;
  14. import android.preference.PreferenceManager;
  15. import android.util.Log;
  16. import android.view.ContextMenu;
  17. import android.view.LayoutInflater;
  18. import android.view.MenuItem;
  19. import android.view.View;
  20. import android.view.Window;
  21. import android.view.ContextMenu.ContextMenuInfo;
  22. import android.widget.AdapterView;
  23. import android.widget.ArrayAdapter;
  24. import android.widget.ListView;
  25. import android.widget.TextView;
  26. import android.widget.AdapterView.AdapterContextMenuInfo;
  27. import cn.cnic.cerc.moblie.clbclient.R;
  28. public class ListOSDir extends ListActivity {
  29. private static final int UPDATE_FILEPATH = 0;
  30. /** Called when the activity is first created. */
  31. private List<String> items = null;// 存放名称
  32. private List<String> paths = null;// 存放路径
  33. private String rootPath = "/";
  34. private TextView tv;
  35. private SharedPreferences prefs;
  36. @Override
  37. public void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  40. setContentView(R.layout.localfilebrowser);
  41. registerForContextMenu(this.getListView());
  42. tv = (TextView) this.findViewById(R.id.TextView);
  43. this.getFileDir(rootPath);// 获取rootPath目录下的文件.
  44. prefs = PreferenceManager.getDefaultSharedPreferences(this);
  45. if(prefs.getString("CLB_DOWNLOAD_PATH", "").equals(""))
  46. {
  47. File sdcardDir = Environment.getExternalStorageDirectory();
  48. String path = sdcardDir.getParent() + sdcardDir.getName();
  49. String fileDirectory = path + java.io.File.separator + "clb";
  50. Editor editor = prefs.edit();
  51. editor.putString("CLB_DOWNLOAD_PATH", fileDirectory);
  52. editor.commit();
  53. }
  54. }
  55. public void getFileDir(String filePath) {
  56. try {
  57. this.tv.setText("当前路径:" + filePath);// 设置当前所在路径
  58. items = new ArrayList<String>();
  59. paths = new ArrayList<String>();
  60. File f = new File(filePath);
  61. File[] files = f.listFiles();// 列出所有文件
  62. // 如果不是根目录,则列出返回根目录和上一目录选项
  63. if (!filePath.equals(rootPath)) {
  64. items.add("返回根目录");
  65. paths.add(rootPath);
  66. items.add("返回上一层目录");
  67. paths.add(f.getParent());
  68. }
  69. // 将所有文件存入list中
  70. if (files != null) {
  71. int count = files.length;// 文件个数
  72. for (int i = 0; i < count; i++) {
  73. File file = files[i];
  74. if (file.isDirectory()) {
  75. items.add(file.getName());
  76. paths.add(file.getPath());
  77. }
  78. }
  79. }
  80. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
  81. android.R.layout.simple_list_item_1, items);
  82. this.setListAdapter(adapter);
  83. } catch (Exception ex) {
  84. ex.printStackTrace();
  85. }
  86. }
  87. @Override
  88. protected void onListItemClick(ListView l, View v, int position, long id) {
  89. super.onListItemClick(l, v, position, id);
  90. String path = paths.get(position);
  91. File file = new File(path);
  92. // 如果是文件夹就继续分解
  93. if (file.isDirectory()) {
  94. this.getFileDir(path);
  95. } else {
  96. new AlertDialog.Builder(this).setTitle("提示").setMessage(
  97. file.getName() + " 是一个文件!").setPositiveButton("OK",
  98. new DialogInterface.OnClickListener() {
  99. public void onClick(DialogInterface dialog, int which) {
  100. }
  101. }).show();
  102. }
  103. }
  104. private File currentPath;
  105. @Override
  106. public void onCreateContextMenu(ContextMenu menu, View view,
  107. ContextMenuInfo menuInfo) {
  108. AdapterView.AdapterContextMenuInfo info;
  109. try {
  110. info = (AdapterView.AdapterContextMenuInfo) menuInfo;
  111. } catch (ClassCastException e) {
  112. Log.e("test", "bad menuInfo", e);
  113. return;
  114. }
  115. currentPath = new File(paths.get(info.position));
  116. showDialog(UPDATE_FILEPATH);
  117. }
  118. @Override
  119. public boolean onContextItemSelected(MenuItem item) {
  120. super.onContextItemSelected(item);
  121. AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
  122. .getMenuInfo();
  123. // Remember current selection
  124. IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter();
  125. if (adapter == null) {
  126. return false;
  127. }
  128. return true;
  129. }
  130. @Override
  131. protected Dialog onCreateDialog(int id) {
  132. switch (id) {
  133. case UPDATE_FILEPATH:
  134. LayoutInflater inflater = LayoutInflater.from(this);
  135. View view = inflater.inflate(R.layout.dialog_change_filepath, null);
  136. final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath);
  137. final TextView newet = (TextView) view.findViewById(R.id.newfilepath);
  138. prefs = PreferenceManager.getDefaultSharedPreferences(this);
  139. oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", ""));
  140. if(currentPath!=null)
  141. newet.setText(newet.getText()+currentPath.getAbsolutePath());
  142. return new AlertDialog.Builder(this).setIcon(
  143. android.R.drawable.ic_dialog_alert).setTitle(
  144. R.string.dialog_filepath).setView(view)
  145. .setPositiveButton(android.R.string.ok,
  146. new OnClickListener() {
  147. public void onClick(DialogInterface dialog,
  148. int which) {
  149. Editor editor = prefs.edit();
  150. editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath());
  151. editor.commit();
  152. ListOSDir.this.finish();
  153. }
  154. }).setNegativeButton(android.R.string.cancel,
  155. new OnClickListener() {
  156. public void onClick(DialogInterface dialog,
  157. int which) {
  158. // Cancel should not do anything.
  159. removeDialog(UPDATE_FILEPATH);
  160. }
  161. }).create();
  162. }
  163. return null;
  164. }
  165. }
package cn.cnic.cerc.moblie.clbclient.filemanager; import java.io.File; import java.util.ArrayList; import java.util.List; import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.DialogInterface.OnClickListener; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.AdapterContextMenuInfo; import cn.cnic.cerc.moblie.clbclient.R; public class ListOSDir extends ListActivity { private static final int UPDATE_FILEPATH = 0; /** Called when the activity is first created. */ private List<String> items = null;// 存放名称 private List<String> paths = null;// 存放路径 private String rootPath = "/"; private TextView tv; private SharedPreferences prefs; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.localfilebrowser); registerForContextMenu(this.getListView()); tv = (TextView) this.findViewById(R.id.TextView); this.getFileDir(rootPath);// 获取rootPath目录下的文件. prefs = PreferenceManager.getDefaultSharedPreferences(this); if(prefs.getString("CLB_DOWNLOAD_PATH", "").equals("")) { File sdcardDir = Environment.getExternalStorageDirectory(); String path = sdcardDir.getParent() + sdcardDir.getName(); String fileDirectory = path + java.io.File.separator + "clb"; Editor editor = prefs.edit(); editor.putString("CLB_DOWNLOAD_PATH", fileDirectory); editor.commit(); } } public void getFileDir(String filePath) { try { this.tv.setText("当前路径:" + filePath);// 设置当前所在路径 items = new ArrayList<String>(); paths = new ArrayList<String>(); File f = new File(filePath); File[] files = f.listFiles();// 列出所有文件 // 如果不是根目录,则列出返回根目录和上一目录选项 if (!filePath.equals(rootPath)) { items.add("返回根目录"); paths.add(rootPath); items.add("返回上一层目录"); paths.add(f.getParent()); } // 将所有文件存入list中 if (files != null) { int count = files.length;// 文件个数 for (int i = 0; i < count; i++) { File file = files[i]; if (file.isDirectory()) { items.add(file.getName()); paths.add(file.getPath()); } } } ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); this.setListAdapter(adapter); } catch (Exception ex) { ex.printStackTrace(); } } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); String path = paths.get(position); File file = new File(path); // 如果是文件夹就继续分解 if (file.isDirectory()) { this.getFileDir(path); } else { new AlertDialog.Builder(this).setTitle("提示").setMessage( file.getName() + " 是一个文件!").setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).show(); } } private File currentPath; @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { AdapterView.AdapterContextMenuInfo info; try { info = (AdapterView.AdapterContextMenuInfo) menuInfo; } catch (ClassCastException e) { Log.e("test", "bad menuInfo", e); return; } currentPath = new File(paths.get(info.position)); showDialog(UPDATE_FILEPATH); } @Override public boolean onContextItemSelected(MenuItem item) { super.onContextItemSelected(item); AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item .getMenuInfo(); // Remember current selection IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter(); if (adapter == null) { return false; } return true; } @Override protected Dialog onCreateDialog(int id) { switch (id) { case UPDATE_FILEPATH: LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.dialog_change_filepath, null); final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath); final TextView newet = (TextView) view.findViewById(R.id.newfilepath); prefs = PreferenceManager.getDefaultSharedPreferences(this); oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", "")); if(currentPath!=null) newet.setText(newet.getText()+currentPath.getAbsolutePath()); return new AlertDialog.Builder(this).setIcon( android.R.drawable.ic_dialog_alert).setTitle( R.string.dialog_filepath).setView(view) .setPositiveButton(android.R.string.ok, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Editor editor = prefs.edit(); editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath()); editor.commit(); ListOSDir.this.finish(); } }).setNegativeButton(android.R.string.cancel, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Cancel should not do anything. removeDialog(UPDATE_FILEPATH); } }).create(); } return null; } }
更多

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

你可能感兴趣的文章
Python基础班---第一部分(基础)---Python基础知识---计算机组成原理
查看>>
虚拟机VMware 9安装苹果MAC OSX 10.8图文教程
查看>>
POJ3694 Network
查看>>
微信小程序开发-框架
查看>>
redo、undo、binlog的区别
查看>>
DropDownList 控制日期控件显示格式
查看>>
RecycleView设置顶部分割线(记录一个坑)
查看>>
【设计模式系列】单例模式的7种写法
查看>>
汉字转拼音 (转)
查看>>
Machine Learning Techniques -6-Support Vector Regression
查看>>
会计基础_001
查看>>
Cordova 开发环境搭建及创建第一个app
查看>>
ajax请求拿到多条数据拼接显示在页面中
查看>>
小程序: 查看正在写的页面
查看>>
dedecms生成文档数据库崩溃 mysql daemon failed to start
查看>>
Linux的50个基本命令
查看>>
Objective-C中创建单例方法的步骤
查看>>
[转]无法安装MVC3,一直卡在vs10-kb2483190
查看>>
Codeforces 520B:Two Buttons(思维,好题)
查看>>
web框架-(二)Django基础
查看>>