如何将Android的ListView控件中的每一个Item项添加图标

如何将Android的ListView控件中的每一个Item项添加图标,第1张

使用Adapter的notifyDataSetChanged方法会回调Adapter的getView方法,你可以在这个getView方法里添加标志位,只有是当前执行的Item才显示图标,这个图标可以每个Item都有,然后其他的GONE,当然这种方法比较浪费资源;也可以执行到这个Item时向这个Item动态添加一个ImageView,执行完时再移除这个ImageView.

菜单Memu是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu),以下说的是创建OptionsMenu

 一、概述

public boolean onCreateOptionsMenu(Menu menu):使用此方法调用OptionsMenu 。

public boolean onOptionsItemSelected(MenuItem item):选中菜单项后发生的动作。

public void onOptionsMenuClosed(Menu menu):菜单关闭后发生的动作。

public boolean onPrepareOptionsMenu(Menu menu):选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单。

public boolean onMenuOpened(int featureId, Menu menu):单打开后发生的动作。

二、默认样式

默认样式是在屏幕底部d出一个菜单,这个菜单我们就叫他选项菜单OptionsMenu,一般情况下,选项菜单最多显示2排每排3个菜单项,这些菜单项有文字有图标,也被称作Icon Menus,如果多于6项,从第六项开始会被隐藏,在第六项会出现一个More里,点击More才出现第六项以及以后的菜单项,这些菜单项也被称作Expanded Menus。下面介绍。

1.main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="请点击 Menu键显示选项菜单" android:id="@+id/TextView02" />

</LinearLayout>

2.重载onCreateOptionsMenu(Menu menu)方法

重载onCreateOptionsMenu(Menu menu)方法,并在此方法中添加菜单项,最后返回true,如果false,菜单则不会显示。

3.为菜单项注册事件

使用onOptionsItemSelected(MenuItem item)方法为菜单项注册事件

public boolean onOptionsItemSelected(MenuItem item)

先是两个layout:

1、main.xml

复制代码

复制代码

1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3 android:orientation="vertical"

4 android:layout_width="fill_parent"

5 android:layout_height="fill_parent">

6 <ListView

7 android:layout_width="fill_parent"

8 android:layout_height="fill_parent"

9 android:focusable="false"

10 android:id="@+id/lvImageList" >

11 </ListView>

12 </LinearLayout>

复制代码

复制代码

2、listitem.xml

复制代码

复制代码

1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3 android:orientation="horizontal"

4 android:layout_width="fill_parent"

5 android:layout_height="?android:attr/listPreferredItemHeight">

6 <ImageView

7 android:id="@+id/itemImgImageInfo"

8 android:layout_marginTop="4dip"

9 android:layout_marginBottom="4dip"

10 android:layout_width="?android:attr/listPreferredItemHeight"

11 android:layout_height="?android:attr/listPreferredItemHeight">

12 </ImageView>

13 <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"

14 android:layout_width="fill_parent"

15 android:layout_height="fill_parent"

16 android:paddingLeft="4dip"

17 android:mode="twoLine">

18 <CheckedTextView

19 android:id="@+id/itemChkImageInfo"

20 android:layout_width="fill_parent"

21 android:layout_height="wrap_content"

22 android:gravity="center_vertical"

23 android:textAppearance="?android:attr/textAppearanceSmall"

24 android:checkMark="?android:attr/listChoiceIndicatorMultiple">

25 </CheckedTextView>

26 <TextView

27 android:id="@+id/itemTxtImageInfo"

28 android:layout_width="fill_parent"

29 android:layout_height="wrap_content"

30 android:gravity="center_vertical|top"

31 android:layout_marginBottom="4dip"

32 android:layout_below="@+id/itemChkImageInfo"

33 android:textAppearance="?android:attr/textAppearanceSmall">

34 </TextView>

35 </TwoLineListItem>

36 </LinearLayout>

复制代码

复制代码

接着是代码:

复制代码

复制代码

1 package com.android.MultipleChoiceImageList

2

3 import java.util.ArrayList

4 import java.util.HashMap

5 import java.util.List

6 import java.util.Map

7

8 import android.app.Activity

9 import android.content.Context

10 import android.database.Cursor

11 import android.graphics.Bitmap

12 import android.net.Uri

13 import android.os.Bundle

14 import android.provider.MediaStore

15 import android.provider.MediaStore.Images

16 import android.view.LayoutInflater

17 import android.view.View

18 import android.view.ViewGroup

19 import android.widget.AdapterView

20 import android.widget.CheckedTextView

21 import android.widget.ImageView

22 import android.widget.ListView

23 import android.widget.SimpleAdapter

24 import android.widget.TextView

25 import android.widget.AdapterView.OnItemClickListener

26

27 public class MainActivity extends Activity {

28

29 private ListView lvImageList

30

31 private String imageID= "imageID"

32 private String imageName = "imageName"

33 private String imageInfo = "imageInfo"

34

35 private ArrayList<String>fileNames = new ArrayList<String>()

36

37 private MultipleChoiceImageListAdapter mAdapter

38

39 /** Called when the activity is first created. */

40 @Override

41 public void onCreate(Bundle savedInstanceState) {

42 super.onCreate(savedInstanceState)

43 setContentView(R.layout.main)

44

45 lvImageList=(ListView) this.findViewById(R.id.lvImageList)

46 lvImageList.setItemsCanFocus(false)

47 lvImageList.setOnItemClickListener(new OnItemClickListener() {

48 @Override

49 public void onItemClick(AdapterView<?>parent, View view, int position, long id) {

50

51 CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(R.id.itemChkImageInfo)

52 checkedTextView.toggle()

53 mAdapter.setCheckItem(position, checkedTextView.isChecked())

54 }

55 })

56 try{

57 String[] from = {imageID, imageName, imageInfo}

58 int[] to = {R.id.itemImgImageInfo, R.id.itemChkImageInfo, R.id.itemTxtImageInfo}

59 mAdapter = new MultipleChoiceImageListAdapter(MainActivity.this, GetImageList(), R.layout.listitem, from, to)

60 lvImageList.setAdapter(mAdapter)

61 }

62 catch(Exception ex){

63 return

64 }

65 }

66

67 //获取图片列表

68 private ArrayList<Map<String, String>>GetImageList(){

69

70 ArrayList<Map<String, String>>imageList = new ArrayList<Map<String,String>>()

71 HashMap<String, String>imageMap

72

73 //读取SD卡中所有图片

74 Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

75 String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE}

76 String selection = MediaStore.Images.Media.MIME_TYPE + "=?"

77 String[] selectionArg ={"image/jpeg"}

78 Cursor mCursor = this.managedQuery(uri, projection, selection, selectionArg, MediaStore.Images.Media.DISPLAY_NAME)

79 imageList.clear()

80 if (mCursor != null) {

81 mCursor.moveToFirst()

82 while (mCursor.getPosition() != mCursor.getCount())

83 {

84 imageMap= new HashMap<String, String>()

85 imageMap.put(imageID, mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media._ID)))

86 imageMap.put(imageName, mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)))

87 imageMap.put(imageInfo, " " + (mCursor.getLong(mCursor.getColumnIndex(MediaStore.Images.Media.SIZE))/1024)+"KB")

88 imageList.add(imageMap)

89 fileNames.add(mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA)))

90 mCursor.moveToNext()

91 }

92 mCursor.close()

93 }

94 return imageList

95 }

96

97 //可多选图片列表适配器

98 class MultipleChoiceImageListAdapter extends SimpleAdapter {

99

100 private Map<Integer, Boolean>map

101 private List<Integer>state

102 private List<? extends Map<String, ?>>mList

103

104 LayoutInflater mInflater

105

106 public MultipleChoiceImageListAdapter(Context context, List<Map<String, String>>data, int resource, String[] from, int[] to) {

107 super(context, data, resource, from, to)

108 map = new HashMap<Integer, Boolean>()

109 mInflater = LayoutInflater.from(context)

110 mList = data

111 for(int i = 0i <data.size()i++) {

112 map.put(i, false)

113 }

114 state = new ArrayList<Integer>()

115 }

116

117 @Override

118 public int getCount() {

119 return mList.size()

120 }

121

122 @Override

123 public Object getItem(int position) {

124 return position

125 }

126

127 @Override

128 public long getItemId(int position) {

129 return position

130 }

131

132 //设置条目选中状态

133 public void setCheckItem(int position, Boolean isChecked){

134 map.put(position, isChecked)

135 if (state.contains(position))

136 state.remove((Object)position)

137 if (isChecked){

138 state.add(position)

139 }

140 }

141

142 //获取列表中已选中条目

143 public long[] getCheckItemIds(){

144 int count = state.size()

145 long[] ids = new long[count]

146 for (int i = 0i <counti++) {

147 ids[i]= (long)state.get(i)

148 }

149 return ids

150 }

151

152 @Override

153 public View getView(int position, View convertView, ViewGroup parent) {

154 if(convertView == null) {

155 convertView = mInflater.inflate(R.layout.listitem, null)

156 }

157

158 CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.itemChkImageInfo)

159 checkedTextView.setChecked(map.get(position))

160 checkedTextView.setText((String)mList.get(position).get(imageName))

161

162 TextView textView = (TextView) convertView.findViewById(R.id.itemTxtImageInfo)

163 textView.setText((String)mList.get(position).get(imageInfo))

164

165 //显示图片缩略图

166 ImageView image = (ImageView) convertView.findViewById(R.id.itemImgImageInfo)

167 Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong((String)mList.get(position).get(imageID)), Images.Thumbnails.MICRO_KIND, null)

168 image.setImageBitmap(bm)

169

170 return convertView

171 }

172 }

173 }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11952943.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存