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

Android ListView實(shí)現(xiàn)單選及多選等功能示例

 更新時(shí)間:2017年08月23日 09:58:16   作者:遲做總比不做強(qiáng)  
這篇文章主要介紹了Android ListView實(shí)現(xiàn)單選及多選等功能的方法,結(jié)合實(shí)例形式分析了ListView單選、多選及長(zhǎng)按多選等功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android ListView實(shí)現(xiàn)單選及多選等功能的方法。分享給大家供大家參考,具體如下:

在項(xiàng)目中也遇到過(guò)給ListView的item添加選擇功能。比如一個(gè)網(wǎng)購(gòu)APP,有個(gè)歷史瀏覽頁(yè)面,這個(gè)頁(yè)面現(xiàn)點(diǎn)擊item單選/多選及全選刪除功能。

當(dāng)時(shí)也是通過(guò)在數(shù)據(jù)中添加一個(gè)是否選擇的字段來(lái)記錄item的狀態(tài),然后根據(jù)這個(gè)字段有相應(yīng)的position位置進(jìn)行選擇狀態(tài)更改及刪除操作。

剛剛看了Android API Demos中17種ListView的實(shí)現(xiàn)方法,發(fā)現(xiàn)ListView自身就帶有我們所需要的單選,多選功能而且實(shí)現(xiàn)起來(lái)相當(dāng)方便。

/**
 * 單選或多選功能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<String>(this,
        android.R.layout.simple_list_item_single_choice, GENRES));
    final ListView listView = getListView();
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//添加這一句話,就實(shí)現(xiàn)單選功能
      //listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//添加這一句話,就實(shí)現(xiàn)多選功能
  }
  private static final String[] GENRES = new String[] {
    "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
    "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
  };
}

/**
 * 長(zhǎng)按多選,添加了選擇模式
 * @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<String>(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;
}

當(dāng)我們通過(guò)以上這些方法實(shí)現(xiàn)ListView選中之后,我們可以把對(duì)應(yīng)的item位置記錄下來(lái),就可以對(duì)相應(yīng)地?cái)?shù)據(jù)進(jìn)行操作了

/**
 * 帶懸浮提示框的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<String>(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" };
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

乌拉特后旗| 中江县| 阿拉善左旗| 基隆市| 阳城县| 甘谷县| 武汉市| 太湖县| 双柏县| 仁寿县| 平果县| 东台市| 淮滨县| 庐江县| 新闻| 登封市| 伊川县| 中宁县| 凤山县| 呼伦贝尔市| 高密市| 宜川县| 吉林省| 公安县| 青川县| 黔西县| 玉屏| 乐清市| 信宜市| 钟山县| 临泽县| 新晃| 镇江市| 洞头县| 富锦市| 遂昌县| 大丰市| 辽阳市| 赤城县| 浪卡子县| 成安县|