效果图:
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","Hong Kong","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","Mongolia","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","Taiwan","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:@R_403_5095@="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:@R_403_5095@="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的选择模式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)