android的listview多选功能,Android ListView实现单选及多选等功能示例

本文实例讲述了Android ListView实现单选及多选等功能的方法。分享给大家供大家参考,具体如下:

在项目中也遇到过给ListView的item添加选择功能。比如一个网购APP,有个历史浏览页面,这个页面现点击item单选/多选及全选删除功能。

当时也是通过在数据中添加一个是否选择的字段来记录item的状态,然后根据这个字段有相应的position位置进行选择状态更改及删除操作。

刚刚看了Android API Demos中17种ListView的实现方法,发现ListView自身就带有我们所需要的单选,多选功能而且实现起来相当方便。

/**

* 单选或多选功能ListView

* @description:

* @author ldm

* @date 2016-4-21 上午10:44:37

*/

public class SingleChoiceList extends ListActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setListAdapter(new ArrayAdapter(this,

android.R.layout.simple_list_item_single_choice, GENRES));

final ListView listView = getListView();

listView.setItemsCanFocus(false);

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//添加这一句话,就实现单选功能

//listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//添加这一句话,就实现多选功能

}

private static final String[] GENRES = new String[] {

"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",

"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"

};

}

/**

* 长按多选,添加了选择模式

* @description:

* @author ldm

* @date 2016-4-21 上午10:47:55

*/

public class ChoiceModeList extends ListActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

ListView lv = getListView();

lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

lv.setMultiChoiceModeListener(new ModeCallback());

setListAdapter(new ArrayAdapter(this,

android.R.layout.simple_list_item_checked, mStrings));

}

@Override

protected void onPostCreate(Bundle savedInstanceState) {

super.onPostCreate(savedInstanceState);

getActionBar().setSubtitle("Long press to start selection");

}

private class ModeCallback implements ListView.MultiChoiceModeListener {

public boolean onCreateActionMode(ActionMode mode, Menu menu) {

MenuInflater inflater = getMenuInflater();

inflater.inflate(R.menu.list_select_menu, menu);

mode.setTitle("Select Items");

setSubtitle(mode);

return true;

}

public boolean onPrepareActionMode(ActionMode mode, Menu menu) {

return true;

}

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

switch (item.getItemId()) {

case R.id.share:

Toast.makeText(ChoiceModeList.this, "Shared " + getListView().getCheckedItemCount() +

" items", Toast.LENGTH_SHORT).show();

mode.finish();

break;

default:

Toast.makeText(ChoiceModeList.this, "Clicked " + item.getTitle(),

Toast.LENGTH_SHORT).show();

break;

}

return true;

}

public void onDestroyActionMode(ActionMode mode) {

}

public void onItemCheckedStateChanged(ActionMode mode,

int position, long id, boolean checked) {

setSubtitle(mode);

}

private void setSubtitle(ActionMode mode) {

final int checkedCount = getListView().getCheckedItemCount();

switch (checkedCount) {

case 0:

mode.setSubtitle(null);

break;

case 1:

mode.setSubtitle("One item selected");

break;

default:

mode.setSubtitle("" + checkedCount + " items selected");

break;

}

}

}

private String[] mStrings = Cheeses.sCheeseStrings;

}

当我们通过以上这些方法实现ListView选中之后,我们可以把对应的item位置记录下来,就可以对相应地数据进行操作了

/**

* 带悬浮提示框的ListView

*

* @description:

* @author ldm

* @date 2016-4-21 上午10:55:51

*/

public class List9 extends ListActivity implements ListView.OnScrollListener {

private final class RemoveWindow implements Runnable {

public void run() {

removeWindow();

}

}

private RemoveWindow mRemoveWindow = new RemoveWindow();

Handler mHandler = new Handler();

private WindowManager mWindowManager;

private TextView mDialogText;

private boolean mShowing;

private boolean mReady;

private char mPrevLetter = Character.MIN_VALUE;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

setListAdapter(new ArrayAdapter(this,

android.R.layout.simple_list_item_1, mStrings));

getListView().setOnScrollListener(this);

LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

mDialogText = (TextView) inflate.inflate(R.layout.list_position, null);

mDialogText.setVisibility(View.INVISIBLE);

mHandler.post(new Runnable() {

public void run() {

mReady = true;

WindowManager.LayoutParams lp = new WindowManager.LayoutParams(

LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT,

WindowManager.LayoutParams.TYPE_APPLICATION,

WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE

| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

PixelFormat.TRANSLUCENT);

mWindowManager.addView(mDialogText, lp);

}

});

}

@Override

protected void onResume() {

super.onResume();

mReady = true;

}

@Override

protected void onPause() {

super.onPause();

removeWindow();

mReady = false;

}

@Override

protected void onDestroy() {

super.onDestroy();

mWindowManager.removeView(mDialogText);

mReady = false;

}

public void onScroll(AbsListView view, int firstVisibleItem,

int visibleItemCount, int totalItemCount) {

if (mReady) {

char firstLetter = mStrings[firstVisibleItem].charAt(0);

if (!mShowing && firstLetter != mPrevLetter) {

mShowing = true;

mDialogText.setVisibility(View.VISIBLE);

}

mDialogText.setText(((Character) firstLetter).toString());

mHandler.removeCallbacks(mRemoveWindow);

mHandler.postDelayed(mRemoveWindow, 3000);

mPrevLetter = firstLetter;

}

}

public void onScrollStateChanged(AbsListView view, int scrollState) {

}

private void removeWindow() {

if (mShowing) {

mShowing = false;

mDialogText.setVisibility(View.INVISIBLE);

}

}

private String[] mStrings = new String[] { "Abbaye de Belloc",

"Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",

"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",

"Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler",

"Alverca", "Ambert", "American Cheese", "Ami du Chambertin",

"Beenleigh Blue", "Beer Cheese", "Bel Paese", "Bergader",

"Bergere Bleue", "Berkswell", "Beyaz Peynir", "Bierkase",

"Bishop Kennedy", "Blarney", "Bleu d'Auvergne", "Bleu de Gex",

"Bleu de Laqueuille", "Bleu de Septmoncel", "Bleu Des Causses",

"Blue", "Blue Castello", "Blue Rathgore", "Blue Vein (Australian)",

"Blue Vein Cheeses", "Bocconcini", "Bocconcini (Australian)",

"Boeren Leidenkaas", "Bonchester", "Bosworth", "Bougon",

"Boule Du Roves", "Boulette d'Avesnes", "Boursault", "Boursin",

"Bouyssou", "Bra", "Braudostur", "Breakfast Cheese",

"Brebis du Lavort", "Brebis du Lochois", "Brebis du Puyfaucon",

"Bresse Bleu", "Brick", "Brie", "Brie de Meaux", "Brie de Melun",

"Brillat-Savarin", "Brin", "Brin d' Amour", "Brin d'Amour",

"Brinza (Burduf Brinza)", "Briquette de Brebis",

"Briquette du Forez", "Broccio", "Broccio Demi-Affine",

"Brousse du Rove", "Bruder Basil",

"Brusselae Kaas (Fromage de Bruxelles)", "Bryndza",

"Buchette d'Anjou", "Buffalo", "Chevrotin des Aravis",

"Chontaleno", "Civray", "Coeur de Camembert au Calvados",

"Coeur de Chevre", "Colby", "Cold Pack", "Comte", "Coolea",

"Cooleney", "Coquetdale", "Corleggy", "Cornish Pepper",

"Cotherstone", "Cotija", "Cottage Cheese",

"Cottage Cheese (Australian)", "Cougar Gold", "Coulommiers",

"Coverdale", "Crayeux de Roncq", "Cream Cheese", "Cream Havarti",

"Crema Agria", "Crema Mexicana", "Creme Fraiche", "Crescenza",

"Croghan", "Crottin de Chavignol", "Crottin du Chavignol",

"Crowdie", "Crowley", "Cuajada", "Curd", "Cure Nantais",

"Curworthy", "Cwmtawe Pecorino", "Cypress Grove Chevre",

"Danablu (Danish Blue)", "Danbo", "Danish Fontina",

"Daralagjazsky", "Dauphin", "Delice des Fiouves",

"Denhany Dorset Drum", "Derby", "Dessertnyj Belyj", "Devon Blue",

"Devon Garland", "Dolcelatte", "Doolin", "Doppelrhamstufel",

"Dorset Blue Vinney", "Double Gloucester", "Double Worcester",

"Dreux a la Feuille", "Dry Jack", "Garrotxa", "Gastanberra",

"Geitost", "Gippsland Blue", "Gjetost", "Gloucester",

"Golden Cross", "Gorgonzola", "Gornyaltajski", "Gospel Green",

"Gouda", "Goutu", "Gowrie", "Grabetto", "Graddost",

"Grafton Village Cheddar", "Grana", "Grana Padano", "Grand Vatel",

"Grataron d' Areches", "Gratte-Paille", "Graviera", "Greuilh",

"Greve", "Gris de Lille", "Gruyere", "Gubbeen", "Guerbigny",

"Halloumi", "Halloumy (Australian)", "Haloumi-Style Cheese",

"Harbourne Blue", "Havarti", "Heidi Gruyere", "Hereford Hop",

"Herrgardsost", "Herriot Farmhouse", "Herve", "Hipi Iti",

"Hubbardston Blue Cow", "Hushallsost", "Iberico", "Idaho Goatster",

"Idiazabal", "Il Boschetto al Tartufo", "Ile d'Yeu",

"Isle of Mull", "Jarlsberg", "Jermi Tortes", "Jibneh Arabieh",

"Jindi Brie", "Jubilee Blue", "Juustoleipa", "Kadchgall", "Kaseri",

"Kashta", "Kefalotyri", "Kenafa", "Kernhem", "Kervella Affine",

"Kikorangi", "King Island Cape Wickham Brie", "King River Gold",

"Klosterkaese", "Knockalara", "Kugelkase", "Menallack Farmhouse",

"Menonita", "Meredith Blue", "Mesost", "Metton (Cancoillotte)",

"Meyer Vintage Gouda", "Mihalic Peynir", "Milleens", "Mimolette",

"Mine-Gabhar", "Mini Baby Bells", "Mixte", "Molbo",

"Monastery Cheeses", "Mondseer", "Mont D'or Lyonnais", "Montasio",

"Monterey Jack", "Monterey Jack Dry", "Morbier",

"Morbier Cru de Montagne", "Mothais a la Feuille", "Mozzarella",

"Mozzarella (Australian)", "Mozzarella di Bufala",

"Mozzarella Fresh, in water", "Mozzarella Rolls", "Munster",

"Murol", "Mycella", "Myzithra", "Peekskill Pyramid",

"Pelardon des Cevennes", "Pelardon des Corbieres", "Penamellera",

"Penbryn", "Pencarreg", "Perail de Brebis", "Petit Morin",

"Petit Pardou", "Petit-Suisse", "Picodon de Chevre",

"Picos de Europa", "Piora", "Pithtviers au Foin",

"Plateau de Herve", "Plymouth Cheese", "Podhalanski",

"Poivre d'Ane", "Polkolbin", "Pont l'Eveque", "Port Nicholson",

"Port-Salut", "Postel", "Pouligny-Saint-Pierre", "Pourly",

"Prastost", "Pressato", "Prince-Jean", "Processed Cheddar",

"Provolone", "Provolone (Australian)", "Pyengana Cheddar",

"Pyramide", "Quark", "Quark (Australian)", "Quartirolo Lombardo",

"Quatre-Vents", "Quercy Petit", "Queso Blanco",

"Queso Blanco con Frutas --Pina y Mango", "Queso de Murcia",

"Queso del Montsec", "Saint-Marcellin", "Saint-Nectaire",

"Saint-Paulin", "Salers", "Samso", "San Simon", "Sancerre",

"Sap Sago", "Sardo", "Sardo Egyptian", "Sbrinz", "Scamorza",

"Schabzieger", "Schloss", "Selles sur Cher", "Selva", "Serat",

"Seriously Strong Cheddar", "Serra da Estrela", "Sharpam",

"Shelburne Cheddar", "Shropshire Blue", "Siraz", "Sirene",

"Smoked Gouda", "Somerset Brie", "Sonoma Jack",

"Sottocenare al Tartufo", "Soumaintrain", "Sourire Lozerien",

"Spenwood", "Sraffordshire Organic", "St. Agur Blue Cheese",

"Stilton", "Stinking Bishop", "String", "Sussex Slipcote",

"Sveciaost", "Swaledale", "Sweet Style Swiss", "Swiss",

"Syrian (Armenian String)", "Tala", "Taleggio", "Tamie",

"Tasmania Highland Chevre Log", "Taupiniere", "Teifi", "Telemea",

"Testouri", "Tete de Moine", "Tetilla", "Venaco", "Vendomois",

"Vieux Corse", "Vignotte", "Vulscombe", "Waimata Farmhouse Blue",

"Washed Rind Cheese (Australian)", "Waterloo", "Weichkaese",

"Wellington", "Wensleydale", "White Stilton",

"Zanetti Parmigiano Reggiano" };

}

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/486659.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

git 生成ssh key_ubuntu git生成ssh key (公钥私钥)配置github或者码云

Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的Git则需要SSH的配置。github的SSH配置如下:设置Git的user name和email:git config --global user.name "xx"git config --global user.email &qu…

ThinkPHP5模型操作中的自动时间戳总结

ThinkPHP5中提供了非常优秀的自动时间戳功能。使用起来非常方便。 但是官网手册中的说明还是不是很详尽,因此整理再次,以方便后续使用时查阅。 一、一般情况下的自动填充create_time,update_time两个字段 对于固定的时间戳和时间日期型的字段&#xff0c…

android圆形头像 demo,Android图像处理之绘制圆形头像

在Android中,绘制圆形和绘制图片都是很容易的事情,但是绘制圆形图片就有点难倒人了。以前为了偷懒就直接去github上找一个开源项目,后来才发现绘制圆形图片其实也是很简单的事。绘制圆形图片也需要两个步骤:绘制圆形和绘制图片&am…

芯片项目烂尾怎么办?国家发改委回应了!

来源:北京日报客户端芯片项目烂尾的报道近日引发关注。对此,国家发改委新闻发言人孟玮在今日(20日)上午举行的例行发布会上回应,将会同有关部门强化顶层设计,狠抓产业规划布局,努力维护产业发展…

idea中二级包为什么创建不了_IDEA通过Maven打包JavaFX工程(OpenJFX11)

1 概述 最近研究JFX,写出来了但是打包不了,这。。。尴尬。。。 IDEA的文档说只支持Java8打成jar包: 尝试过直接使用Maven插件的package,不行,也尝试过Build Artifacts,也不行,各种奇奇怪怪的问题…

鸿蒙系统发布会16号几点,华为鸿蒙手机系统正式定档发布,12月16日于我们见面...

#华为鸿蒙系统#大家都知道,华为手机已经无法预装谷歌GMS服务了,对于大部分海外用户来说,GMS服务还是相当重要的,谷歌全家桶内置了地图、搜索、视频娱乐等功能,但凡是搭载了安卓系统的智能手机,那么就无法离…

二叉树和红黑二叉树

二叉树是树形结构的一个类型,由一个根节点和左子树、右子树组成。排序二叉树特性:左子树上的值均小于根节点的值;右子树的值均大于根节点的值。(相当于链表)平衡二叉树任何节点的两个子树的最大高度差为1,被…

Gartner发布2021年重要战略科技趋势

来源:Gartner公司全球领先的信息技术研究和顾问公司Gartner于今日发布企业机构在2021年需要深挖的重要战略科技趋势。分析师们在本周举行的Gartner IT Symposium/Xpo大会美洲站虚拟会议上展示了自己的发现。Gartner研究副总裁Brian Burke表示:“各企业职…

html右侧分为两个框架,html – 两个框架一个滚动条

我正在尝试创建两个框架并使它们一起滚动,例如,如果页面顶部有更改的菜单栏 –我正在使用旋转木马 – 或底部的页脚,必须作为页面的一部分出现.我希望页面看起来像一个也能够滚动的页面,但页面实际上由两个框架组成.用这个……/p>"http://www.w3.org/TR/xhtml1/DTD/xht…

pause pod 什么是pod_Kubernetes 自主式Pod清单 干货太多先马住慢慢看

在讲资源清单之前,先给大家介绍一下究竟什么是Kubernetes资源?有几种资源?① 资源分类:① 1 工作负载:pod、rs(ReplicasSet)、deploy(Deployment)、sts(StatefulSet)、ds(DaemonSet)、job、cronjob① 2 服务发现及负…

不会Python开发的运维终将被淘汰?

Python语言是一种面向对象、直译式计算机程序设计语言,由Guido van Rossum于1989年底发明。Python语法简捷而清晰,具有丰富和强大的类库,具有可扩展性和可嵌入性,是现代比较流行的语言。 一、最流行的语言 IEEE Spectrum 的数据记…

8大领域、6大机遇!读懂2020年AI医疗新风向

来源 | 工业互联网创新中心、36氪研究院编辑 | 蒲蒲蓝皮书如下未来智能实验室的主要工作包括:建立AI智能系统智商评测体系,开展世界人工智能智商评测;开展互联网(城市)云脑研究计划,构建互联网(…

对口高考 html语言,VB对口高考模拟试题1

《VB》对口高考模拟试题1一、选择题。(每个1分,共30分)1、某一VB工程含F1、F2两个窗体,执行“F1.Show: F2.Hide”语句后,F1、F2窗体显示的结果为()。A、都显示B、都不显示C、仅显示F1窗体D、仅显示F2窗体2、删除列表框…

LinkedList源码详解

public class LinkedList<E>extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable, java.io.Serializable LinkedList继承自AbstractSequenceList、实现了List及Deque接口。 AbstractSequenceList提供List接口的骨干实现,以减少…

arduino uno r3单片机封装图_Arduino教程 | 初识Arduino

前一段时间偶然进入创客空间并接触到了开源硬件&#xff0c;小编表示十分感兴趣。于是这周打算汇总一下网络上现有的一些关于Arduino的及教程进行分享。首先感谢一下各位原作者&#xff0c;在此表示十分感谢~什么是Arduino&#xff1f;Arduino是一个能够用来感应和控制现实物理…

神经网络原来是这样和数学挂钩的

来源&#xff1a;遇见数学近几年&#xff0c;有几个被媒体大肆报道的事件&#xff0c;如下表所示。如上所示&#xff0c;深度学习作为人工智能的一种具有代表性的实现方法&#xff0c;取得了很大的成功。那么&#xff0c;深度学习究竟是什么技术呢&#xff1f;深度学习里的“学…

鸿蒙os开发小程序,9岁小学生展示鸿蒙OS开发:这操作太秀了

华为的鸿蒙 OS 正在迅速成熟&#xff0c;这背后离不开无数开发者的支持。近日&#xff0c;鸿蒙 OS 技术社区就向大家介绍了一位特殊的鸿蒙开发者&#xff0c;今年只有 9 岁&#xff0c;还是个小学生 ……在鸿蒙社区&#xff0c;这位小开发者坐在镜头前&#xff0c;向大家演示了…

python 列表推导式

列表推导式的格式&#xff1a; [表达式 for 变量 in 列表] 或者 [表达式 for 变量 in 列表 if 条件] 举例&#xff1a; #!/usr/bin/python # -*- coding: utf-8 -*-li [1,2,3,4,5,6,7,8,9] print [x**2 for x in li]print [x**2 for x in li if x>5]print dict([(x,x*10) …

java调用一个方法后怎么继续执行不等待该方法的返回_Java面试题大全2020版(二)...

今天给大家推送第二部分&#xff0c;主要的大块内容分为&#xff1a;多线程、反射、对象拷贝。三大块内容中涉及到的考点如下&#xff1a;三、多线程35. 并行和并发有什么区别&#xff1f;并行是指两个或者多个事件在同一时刻发生&#xff1b;而并发是指两个或多个事件在同一时…

城市“脑梗” 当前城市大脑面临的困境

来源&#xff1a;中国建设报智慧城市 作者 刘锋 吕本富2020年&#xff0c;城市大脑已经成为前沿科技和城市建设发展的重点&#xff0c;有近百个城市宣布开展城市大脑建设。数百家科技企业进入到泛城市大脑的建设领域&#xff0c;有数百亿的资金投入到城市大脑的建设中。面对…