查看: 558|回复: 0

[Android教程] Android编程实现带有图标的ListView并带有长按菜单效果示例

发表于 2017-11-28 08:00:01

本文实例讲述了Android编程实现带有图标的ListView并带有长按菜单效果。分享给大家供大家参考,具体如下:

MainActivity.Java

  1. package com.zxl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.w3c.dom.ls.LSException;
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.view.ContextMenu;
  8. import android.view.ContextMenu.ContextMenuInfo;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.AdapterView.AdapterContextMenuInfo;
  13. import android.widget.ListView;
  14. import android.widget.AdapterView.OnItemClickListener;
  15. import android.widget.Toast;
  16. public class MainActivity extends Activity implements OnItemClickListener {
  17. /** Called when the activity is first created. */
  18. ListView lv;
  19. List<ListViewItem> items;
  20. CustomListViewAdapter adapter;
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. lv=(ListView)findViewById(R.id.listView1);
  26. items=new ArrayList<MainActivity.ListViewItem>();
  27. items.add(new ListViewItem(){{
  28. ThumbnailResource=R.drawable.ic_launcher;
  29. title="Item1";
  30. SubTitle="Item1 Description";
  31. }});
  32. items.add(new ListViewItem(){{
  33. ThumbnailResource=R.drawable.ic_launcher;
  34. title="Item2";
  35. SubTitle="Item2 Description";
  36. }});
  37. items.add(new ListViewItem(){{
  38. ThumbnailResource=R.drawable.ic_launcher;
  39. title="Item3";
  40. SubTitle="Item3 Description";
  41. }});
  42. items.add(new ListViewItem(){{
  43. ThumbnailResource=R.drawable.ic_launcher;
  44. title="Item4";
  45. SubTitle="Item4 Description";
  46. }});
  47. adapter=new CustomListViewAdapter(this, items);
  48. lv.setAdapter(adapter);
  49. registerForContextMenu(lv);
  50. lv.setOnItemClickListener(this);
  51. }
  52. class ListViewItem{
  53. public int ThumbnailResource;
  54. public String title;
  55. public String SubTitle;
  56. }
  57. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  58. // TODO Auto-generated method stub
  59. /* ListViewItem item=items.get(position); //删除
  60. items.remove(item);
  61. adapter=new CustomListViewAdapter(this, items);
  62. lv.setAdapter(adapter);*/
  63. ListViewItem item=items.get(position);
  64. Toast.makeText(MainActivity.this, "this is :"+item, Toast.LENGTH_LONG).show();
  65. }
  66. @Override
  67. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  68. // TODO Auto-generated method stub
  69. super.onCreateContextMenu(menu, v, menuInfo);
  70. menu.add("add");
  71. menu.add("update");
  72. menu.add("delete");
  73. }
  74. @Override
  75. public boolean onContextItemSelected(MenuItem item) {
  76. // TODO Auto-generated method stub
  77. super.onContextItemSelected(item);
  78. if(item.getTitle()=="add"){
  79. Toast.makeText(MainActivity.this, "add", Toast.LENGTH_LONG).show();
  80. items.add(new ListViewItem(){{
  81. ThumbnailResource=R.drawable.ic_launcher;
  82. title="Item new";
  83. SubTitle="Item new Description";
  84. }});
  85. adapter=new CustomListViewAdapter(this, items);
  86. lv.setAdapter(adapter);
  87. }
  88. else if(item.getTitle()=="update"){
  89. //do somthing
  90. }
  91. else if(item.getTitle()=="delete"){
  92. ContextMenuInfo info = item.getMenuInfo();
  93. AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterContextMenuInfo) info;
  94. // 获取选中行位置
  95. int position = contextMenuInfo.position;
  96. ListViewItem itema=items.get(position);
  97. items.remove(itema);
  98. adapter=new CustomListViewAdapter(this, items);
  99. lv.setAdapter(adapter);
  100. }
  101. return true;
  102. }
  103. }
复制代码

CustomListViewAdapter.java

  1. package com.zxl;
  2. import java.util.List;
  3. import com.zxl.MainActivity.ListViewItem;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.BaseAdapter;
  10. import android.widget.ImageView;
  11. import android.widget.ListView;
  12. import android.widget.TextView;
  13. public class CustomListViewAdapter extends BaseAdapter
  14. {
  15. LayoutInflater inflater;
  16. List<ListViewItem> items;
  17. public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
  18. super();
  19. this.items = items;
  20. this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  21. }
  22. public int getCount() {
  23. // TODO Auto-generated method stub
  24. return items.size();
  25. }
  26. public Object getItem(int position) {
  27. // TODO Auto-generated method stub
  28. return null;
  29. }
  30. public long getItemId(int position) {
  31. // TODO Auto-generated method stub
  32. return 0;
  33. }
  34. public View getView(final int position, View convertView, ViewGroup parent) {
  35. // TODO Auto-generated method stub
  36. //items
  37. ListViewItem item=items.get(position);
  38. View vi=convertView;
  39. if(convertView==null)
  40. vi = inflater.inflate(R.layout.item_row, null);
  41. ImageView imgThumbnail=(ImageView) vi.findViewById(R.id.imgThumbnail);
  42. TextView txtTitle =(TextView) vi.findViewById(R.id.txtTitle);
  43. TextView txtSubTitle=(TextView) vi.findViewById(R.id.txtSubTitle);
  44. imgThumbnail.setImageResource(item.ThumbnailResource);
  45. txtTitle.setText(item.title);
  46. txtSubTitle.setText(item.SubTitle);
  47. return vi;
  48. }
  49. }
复制代码

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:id="@+id/textView1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello" />
  11. <ListView
  12. android:id="@+id/listView1"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentLeft="true"
  16. android:layout_below="@+id/textView1"
  17. android:layout_alignParentTop="true" >
  18. </ListView>
  19. </RelativeLayout>
复制代码

item_row.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. android:id="@+id/relativeLayout1"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. xmlns:android="http://schemas.android.com/apk/res/android"
  7. android:padding="5dip">
  8. <ImageView
  9. android:layout_width="78dip"
  10. android:layout_height="78dip"
  11. android:id="@+id/imgThumbnail"
  12. android:layout_alignParentLeft="true"
  13. android:layout_centerInParent="true"
  14. android:layout_marginLeft="-3dip"
  15. android:scaleType="centerInside">
  16. </ImageView>
  17. <TextView
  18. android:textAppearance="?android:attr/textAppearanceLarge"
  19. android:layout_height="wrap_content"
  20. android:text="TextView"
  21. android:layout_width="wrap_content"
  22. android:id="@+id/txtTitle"
  23. android:layout_toRightOf="@+id/imgThumbnail"
  24. android:layout_marginTop="6dip"
  25. android:layout_marginLeft="6dip">
  26. </TextView>
  27. <TextView
  28. android:layout_height="wrap_content"
  29. android:text="TextView"
  30. android:layout_width="wrap_content"
  31. android:id="@+id/txtSubTitle"
  32. android:layout_toRightOf="@+id/imgThumbnail"
  33. android:layout_below="@+id/txtTitle"
  34. android:layout_marginTop="3dip"
  35. android:layout_marginLeft="6dip">
  36. </TextView>
  37. </RelativeLayout>
复制代码

文件结构:

更多关于Android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作json格式数据技巧总结》、《Android资源操作技巧汇总》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条