最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳談Android ListView的選擇模式

 更新時間:2017年04月18日 09:18:32   投稿:jingxian  
下面小編就為大家?guī)硪黄斦凙ndroid ListView的選擇模式。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

效果圖:

ListView 定義了choiceMode屬性,描述是這樣的:

用于為視圖定義選擇行為。默認情況下,列表時沒有任何選擇行為的。如果把choiceMode設置為singleChoice,列表允許有一個列表項處于被選狀態(tài)。如果把choiceMode設置為multipleChoice,那么列表允許有任意數(shù)量的列表項處于被選狀態(tài)

ListView以某種方式通過Checkable接口處理視圖的選擇狀態(tài),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處理選擇行為,需要令列表項對應的自定義視圖實現(xiàn)Checkable接口,這個需要自定義

創(chuàng)建一個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文件夾下創(chuàng)建一個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的選擇模式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Android6.0來電號碼與電話薄聯(lián)系人進行匹配

    Android6.0來電號碼與電話薄聯(lián)系人進行匹配

    這篇文章主要為大家詳細介紹了Android6.0來電號碼與電話薄聯(lián)系人進行匹配的方法,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android控制閃光燈的方法(打開與關閉)

    Android控制閃光燈的方法(打開與關閉)

    這篇文章主要介紹了Android控制閃光燈的方法,可實現(xiàn)閃光燈打開與關閉的效果,涉及Android操作Camera拍照閃光燈的相關技巧,需要的朋友可以參考下
    2016-01-01
  • android 微信 sdk api調用不成功解決方案

    android 微信 sdk api調用不成功解決方案

    最近一直在調用微信的API,卻發(fā)現(xiàn)一直調用不成功,糾結了好久,各方面找教程,現(xiàn)在曬出來和大家分享一下
    2012-11-11
  • Android三種實現(xiàn)定時器的方法

    Android三種實現(xiàn)定時器的方法

    本文給大家分享了3種Android實現(xiàn)定時器的方法的示例,,需要的朋友可以參考下
    2015-02-02
  • 詳解Android應用main函數(shù)的調用

    詳解Android應用main函數(shù)的調用

    Android常識,App主線程初始化了Looper,調用prepare的地方是ActivityThread.main函數(shù)。問題來了,App的main函數(shù)在哪兒調用,下面我們來一起學習一下吧
    2019-06-06
  • Android開發(fā)之merge結合include優(yōu)化布局

    Android開發(fā)之merge結合include優(yōu)化布局

    這篇文章主要為大家詳細介紹了Android開發(fā)之merge結合include優(yōu)化布局,感興趣的朋友可以參考一下
    2016-06-06
  • Flutter學習教程之Route跳轉以及數(shù)據(jù)傳遞

    Flutter學習教程之Route跳轉以及數(shù)據(jù)傳遞

    這篇文章主要給大家介紹了關于Flutter學習教程之Route跳轉以及數(shù)據(jù)傳遞的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-08-08
  • android自定義滾軸選擇器

    android自定義滾軸選擇器

    這篇文章主要為大家詳細介紹了android自定義滾軸選擇器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android架構組件Room指南

    Android架構組件Room指南

    Room是Google推出的Android架構組件庫中的數(shù)據(jù)持久化組件庫, 也可以說是在SQLite上實現(xiàn)的一套ORM解決方案。下面通過本文給大家介紹Android架構組件Room指南,需要的朋友參考下吧
    2017-12-12
  • Android getViewById和getLayoutInflater().inflate()的詳解及比較

    Android getViewById和getLayoutInflater().inflate()的詳解及比較

    這篇文章主要介紹了Android getViewById和getLayoutInflater().inflate()的詳解及比較的相關資料,這里對這兩種方法進行了詳細的對比,對于開始學習Android的朋友使用這兩種方法是個很好的資料,需要的朋友可以參考下
    2016-11-11

最新評論

延吉市| 阜城县| 恩施市| 宁陵县| 南昌市| 五家渠市| 韶山市| 杂多县| 会昌县| 禹城市| 凤凰县| 墨脱县| 和平区| 禄丰县| 敦化市| 普洱| 裕民县| 平安县| 来宾市| 无锡市| 华池县| 宁波市| 洱源县| 盐山县| 桂东县| 岳普湖县| 温州市| 永吉县| 宁远县| 永福县| 绵阳市| 洱源县| 年辖:市辖区| 广昌县| 田东县| 延津县| 铜山县| 铁岭县| 五常市| 康乐县| 肇庆市|