Java tutorial
/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.luksprog.playground.fragment; import android.annotation.SuppressLint; import android.os.Build; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.ActionMode; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AbsListView.MultiChoiceModeListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.luksprog.playground.R; public class ListCABFragment extends ListFragment { private String[] mCountries = { "Romania", "Germany", "England", "USA", "Japan", "France" }; private static final boolean POST_HONEYCOMB = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (POST_HONEYCOMB) { // make sure we are on a version above Honeycomb otherwise will // access things that aren't available postHoneycombCAB(); } else { // probably do nothing and implement the normal context menu?!? } setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mCountries)); } @SuppressLint({ "NewApi", "NewApi" }) private void postHoneycombCAB() { getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); getListView().setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { ((ListView) parent).setItemChecked(position, ((ListView) parent).isItemChecked(position)); return false; } }); getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() { private int nr = 0; @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { getActivity().getMenuInflater().inflate(R.menu.listcab_menu, menu); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.item1: Toast.makeText(getActivity(), "Option1 clicked", Toast.LENGTH_SHORT).show(); break; case R.id.item2: Toast.makeText(getActivity(), "Option2 clicked", Toast.LENGTH_SHORT).show(); break; } return false; } @Override public void onDestroyActionMode(ActionMode mode) { nr = 0; } @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { if (checked) { nr++; } else { nr--; } mode.setTitle(nr + " rows selected!"); } }); } }