重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么在Android应用中利用ListView实现一个选择模式

怎么在Android应用中利用ListView实现一个选择模式?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联建站长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为密云企业提供专业的成都网站制作、成都网站建设、外贸营销网站建设密云网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

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 {

 public CountryAdapter(Context context, int textViewResourceId,
   List 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 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(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



  

  

activity_hack30.xml

<?xml version="1.0" encoding="utf-8"?>


  

看完上述内容,你们掌握怎么在Android应用中利用ListView实现一个选择模式的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网页题目:怎么在Android应用中利用ListView实现一个选择模式
文章起源:http://cqcxhl.cn/article/gdhgde.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP