Android实现便于批量操作可多选的图片ListView实例
程序员文章站
2024-03-06 13:12:08
本文实例讲述了android实现便于批量操作可多选的图片listview。分享给大家供大家参考,具体如下:
之前项目需要实现一个可多选的图片列表,用户选中一到多张图片后,...
本文实例讲述了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程序设计有所帮助。