详谈Android ListView的选择模式
程序员文章站
2023-01-21 10:04:52
效果图:
listview 定义了choicemode属性,描述是这样的:
用于为视图定义选择行为。默认情况下,列表时没有任何选择行为的。如果把choicemod...
效果图:
listview 定义了choicemode属性,描述是这样的:
用于为视图定义选择行为。默认情况下,列表时没有任何选择行为的。如果把choicemode设置为singlechoice,列表允许有一个列表项处于被选状态。如果把choicemode设置为multiplechoice,那么列表允许有任意数量的列表项处于被选状态
listview以某种方式通过checkable接口处理视图的选择状态,listview源码中有这么一段:
if (mchoicemode != choice_mode_none && mcheckstates != null) { if (child instanceof checkable) { ((checkable) child).setchecked(mcheckstates.get(position)); } else if (getcontext().getapplicationinfo().targetsdkversion >= android.os.build.version_codes.honeycomb) { child.setactivated(mcheckstates.get(position)); } }
如果需要listview处理选择行为,需要令列表项对应的自定义视图实现checkable接口,这个需要自定义
创建一个countries.java
public class countries { public static final string[] countries = new string[] { "afghanistan", "albania", "algeria", "american samoa", "andorra", "angola", "anguilla", "antarctica", "antigua and barbuda", "argentina", "armenia", "aruba", "australia", "austria", "azerbaijan", "bahrain", "bangladesh", "barbados", "belarus", "belgium", "belize", "benin", "bermuda", "bhutan", "bolivia", "bosnia and herzegovina", "botswana", "bouvet island", "brazil", "british indian ocean territory", "british virgin islands", "brunei", "bulgaria", "burkina faso", "burundi", "cote d'ivoire", "cambodia", "cameroon", "canada", "cape verde", "cayman islands", "central african republic", "chad", "chile", "china", "christmas island", "cocos (keeling) islands", "colombia", "comoros", "congo", "cook islands", "costa rica", "croatia", "cuba", "cyprus", "czech republic", "democratic republic of the congo", "denmark", "djibouti", "dominica", "dominican republic", "east timor", "ecuador", "egypt", "el salvador", "equatorial guinea", "eritrea", "estonia", "ethiopia", "faeroe islands", "falkland islands", "fiji", "finland", "former yugoslav republic of macedonia", "france", "french guiana", "french polynesia", "french southern territories", "gabon", "georgia", "germany", "ghana", "gibraltar", "greece", "greenland", "grenada", "guadeloupe", "guam", "guatemala", "guinea", "guinea-bissau", "guyana", "haiti", "heard island and mcdonald islands", "honduras", "*", "hungary", "iceland", "india", "indonesia", "iran", "iraq", "ireland", "israel", "italy", "jamaica", "japan", "jordan", "kazakhstan", "kenya", "kiribati", "kuwait", "kyrgyzstan", "laos", "latvia", "lebanon", "lesotho", "liberia", "libya", "liechtenstein", "lithuania", "luxembourg", "macau", "madagascar", "malawi", "malaysia", "maldives", "mali", "malta", "marshall islands", "martinique", "mauritania", "mauritius", "mayotte", "mexico", "micronesia", "moldova", "monaco", "*", "montserrat", "morocco", "mozambique", "myanmar", "namibia", "nauru", "nepal", "netherlands", "netherlands antilles", "new caledonia", "new zealand", "nicaragua", "niger", "nigeria", "niue", "norfolk island", "north korea", "northern marianas", "norway", "oman", "pakistan", "palau", "panama", "papua new guinea", "paraguay", "peru", "philippines", "pitcairn islands", "poland", "portugal", "puerto rico", "qatar", "reunion", "romania", "russia", "rwanda", "sqo tome and principe", "saint helena", "saint kitts and nevis", "saint lucia", "saint pierre and miquelon", "saint vincent and the grenadines", "samoa", "san marino", "saudi arabia", "senegal", "seychelles", "sierra leone", "singapore", "slovakia", "slovenia", "solomon islands", "somalia", "south africa", "south georgia and the south sandwich islands", "south korea", "spain", "sri lanka", "sudan", "suriname", "svalbard and jan mayen", "swaziland", "sweden", "switzerland", "syria", "*", "tajikistan", "tanzania", "thailand", "the bahamas", "the gambia", "togo", "tokelau", "tonga", "trinidad and tobago", "tunisia", "turkey", "turkmenistan", "turks and caicos islands", "tuvalu", "virgin islands", "uganda", "ukraine", "united arab emirates", "united kingdom", "united states", "united states minor outlying islands", "uruguay", "uzbekistan", "vanuatu", "vatican city", "venezuela", "vietnam", "wallis and futuna", "western sahara", "yemen", "yugoslavia", "zambia", "zimbabwe" }; }
在view文件夹下创建一个countryview.java
public class countryview extends linearlayout implements checkable { private textview mtitle; private checkbox mcheckbox; public countryview(context context) { this(context, null); } public countryview(context context, attributeset attrs) { super(context, attrs); layoutinflater inflater = layoutinflater.from(context); view v = inflater.inflate(r.layout.country_view, this, true); mtitle = (textview) v.findviewbyid(r.id.country_view_title); mcheckbox = (checkbox) v.findviewbyid(r.id.country_view_checkbox); } public void settitle(string title) { mtitle.settext(title); } @override public boolean ischecked() { return mcheckbox.ischecked(); } @override public void setchecked(boolean checked) { mcheckbox.setchecked(checked); } @override public void toggle() { mcheckbox.toggle(); } }
在adapter文件夹下 countryadapter
public class countryadapter extends arrayadapter<country> { public countryadapter(context context, int textviewresourceid, list<country> objects) { super(context, textviewresourceid, objects); } @override public view getview(int position, view convertview, viewgroup parent) { if ( convertview == null ) { convertview = new countryview(getcontext()); } country country = getitem(position); countryview countryview = (countryview) convertview; countryview.settitle(country.getname()); return convertview; } }
在model文件夹下country.java
public class country { private string name; public country() { } public string getname() { return name; } public void setname(string name) { this.name = name; } }
主界面
public class hack30activity extends activity { private listview mlistview; private countryadapter madapter; private list<country> mcountries; private string mtoastfmt; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_hack30); createcountrieslist(); mtoastfmt = getstring(r.string.activity_main_toast_fmt); madapter = new countryadapter(this, -1, mcountries); mlistview = (listview) findviewbyid(r.id.activity_main_list); mlistview.setadapter(madapter); } public void onpickcountryclick(view v) { int pos = mlistview.getcheckeditemposition(); if (listview.invalid_position != pos) { string msg = string.format(mtoastfmt, mcountries.get(pos) .getname()); toast.maketext(this, msg, toast.length_short).show(); } } private void createcountrieslist() { mcountries = new arraylist<country>(countries.countries.length); for (int i = 0; i < countries.countries.length; i++) { country country = new country(); country.setname(countries.countries[i]); mcountries.add(country); } } }
country_view.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <textview android:id="@+id/country_view_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.9" android:padding="10dp" /> <checkbox android:id="@+id/country_view_checkbox" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.1" android:clickable="false" android:focusable="false" android:focusableintouchmode="false" android:gravity="center_vertical" android:padding="10dp" /> </linearlayout>
activity_hack30.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onclick="onpickcountryclick" android:text="@string/activity_main_add_selection" /> <listview android:id="@+id/activity_main_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:choicemode="singlechoice" /> </linearlayout> <string name="activity_main_toast_fmt">chosen country: %s</string> <string name="activity_main_add_selection">pick country</string>
以上这篇详谈android listview的选择模式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
推荐阅读
-
Android编程实现ListView内容无限循环显示的方法
-
android中ListView多次刷新重复执行getView的解决方法
-
详谈Android中onTouch与onClick事件的关系(必看)
-
android基础教程之android的listview与edittext冲突解决方法
-
Android实现读取SD卡下所有TXT文件名并用listView显示出来的方法
-
解决android Listview的item中最外层Margin失效的问题
-
Android textview 实现长按*选择复制功能的方法
-
Android 仿京东商城底部布局的选择效果(Selector 选择器的实现)
-
Android selector背景选择器的使用详解
-
Android checkbox的listView(多选,全选,反选)具体实现方法