时间:2021-01-26 15:24:51 | 栏目:Android代码 | 点击:次
本文实例讲述了Android实现便于批量操作可多选的图片ListView。分享给大家供大家参考,具体如下:
之前项目需要实现一个可多选的图片列表,用户选中一到多张图片后,批量上传。但是网上有可多选普通列表的代码、也有单纯图片列表的代码,却没有两者合并的代码,只好自己实现一个。
废话不说,直接上代码。
先是两个layout:
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"> <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:focusable="false" android:id="@+id/lvImageList" > </ListView> </LinearLayout>
2、listitem.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"> <ImageView android:id="@+id/itemImgImageInfo" android:layout_marginTop="4dip" android:layout_marginBottom="4dip" android:layout_width="?android:attr/listPreferredItemHeight" android:layout_height="?android:attr/listPreferredItemHeight"> </ImageView> <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="4dip" android:mode="twoLine"> <CheckedTextView android:id="@+id/itemChkImageInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:textAppearance="?android:attr/textAppearanceSmall" android:checkMark="?android:attr/listChoiceIndicatorMultiple"> </CheckedTextView> <TextView android:id="@+id/itemTxtImageInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|top" android:layout_marginBottom="4dip" android:layout_below="@+id/itemChkImageInfo" android:textAppearance="?android:attr/textAppearanceSmall"> </TextView> </TwoLineListItem> </LinearLayout>
接着是代码:
package com.android.MultipleChoiceImageList; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.provider.MediaStore.Images; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.CheckedTextView; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity { private ListView lvImageList; private String imageID= "imageID"; private String imageName = "imageName"; private String imageInfo = "imageInfo"; private ArrayList<String> fileNames = new ArrayList<String>(); private MultipleChoiceImageListAdapter mAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lvImageList=(ListView) this.findViewById(R.id.lvImageList); lvImageList.setItemsCanFocus(false); lvImageList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(R.id.itemChkImageInfo); checkedTextView.toggle(); mAdapter.setCheckItem(position, checkedTextView.isChecked()); } }); try{ String[] from = {imageID, imageName, imageInfo}; int[] to = {R.id.itemImgImageInfo, R.id.itemChkImageInfo, R.id.itemTxtImageInfo}; mAdapter = new MultipleChoiceImageListAdapter(MainActivity.this, GetImageList(), R.layout.listitem, from, to); lvImageList.setAdapter(mAdapter); } catch(Exception ex){ return; } } //获取图片列表 private ArrayList<Map<String, String>> GetImageList(){ ArrayList<Map<String, String>> imageList = new ArrayList<Map<String,String>>(); HashMap<String, String> imageMap; //读取SD卡中所有图片 Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE}; String selection = MediaStore.Images.Media.MIME_TYPE + "=?"; String[] selectionArg ={"image/jpeg"}; Cursor mCursor = this.managedQuery(uri, projection, selection, selectionArg, MediaStore.Images.Media.DISPLAY_NAME); imageList.clear(); if (mCursor != null) { mCursor.moveToFirst(); while (mCursor.getPosition() != mCursor.getCount()) { imageMap= new HashMap<String, String>(); imageMap.put(imageID, mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media._ID))); imageMap.put(imageName, mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME))); imageMap.put(imageInfo, " " + (mCursor.getLong(mCursor.getColumnIndex(MediaStore.Images.Media.SIZE))/1024)+"KB"); imageList.add(imageMap); fileNames.add(mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA))); mCursor.moveToNext(); } mCursor.close(); } return imageList; } //可多选图片列表适配器 class MultipleChoiceImageListAdapter extends SimpleAdapter { private Map<Integer, Boolean> map; private List<Integer> state; private List<? extends Map<String, ?>> mList; LayoutInflater mInflater; public MultipleChoiceImageListAdapter(Context context, List<Map<String, String>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); map = new HashMap<Integer, Boolean>(); mInflater = LayoutInflater.from(context); mList = data; for(int i = 0; i < data.size(); i++) { map.put(i, false); } state = new ArrayList<Integer>(); } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } //设置条目选中状态 public void setCheckItem(int position, Boolean isChecked){ map.put(position, isChecked); if (state.contains(position)) state.remove((Object)position); if (isChecked){ state.add(position); } } //获取列表中已选中条目 public long[] getCheckItemIds(){ int count = state.size(); long[] ids = new long[count]; for (int i = 0; i < count; i++) { ids[i]= (long)state.get(i); } return ids; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = mInflater.inflate(R.layout.listitem, null); } CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.itemChkImageInfo); checkedTextView.setChecked(map.get(position)); checkedTextView.setText((String)mList.get(position).get(imageName)); TextView textView = (TextView) convertView.findViewById(R.id.itemTxtImageInfo); textView.setText((String)mList.get(position).get(imageInfo)); //显示图片缩略图 ImageView image = (ImageView) convertView.findViewById(R.id.itemImgImageInfo); Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong((String)mList.get(position).get(imageID)), Images.Thumbnails.MICRO_KIND, null); image.setImageBitmap(bm); return convertView; } } }
下面是模拟器上的效果:
由于是初学java和android,代码写得比较糙,请见谅,欢迎指正,不胜感激。
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。