一.通过activity启动Context Action Bar
1.主java
public class ActivityActionModeFrgmt extends Fragment implements OnCheckedChangeListener, ActionMode.Callback { private ActionMode mActionMode; private CheckBox mCheckBox; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.frgmt_cab_for_activity, container, false); mCheckBox = (CheckBox) v.findViewById(R.id.vid_check_box); mCheckBox.setOnCheckedChangeListener(this); return v; } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { mActionMode = getActivity().startActionMode(this);// activity启动一个继承ActionMode,需要ActionMode.Callback }else { mActionMode.finish(); } } // for ActionMode.Callback @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.menu_context_for_activity, menu);// 创建context action // bar的菜单 return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { mode.setTitle("Checked"); return true; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.action_delete: Toast.makeText(getActivity(), "delete", Toast.LENGTH_SHORT).show(); break; case R.id.action_send: Toast.makeText(getActivity(), "send", Toast.LENGTH_SHORT).show(); break; case R.id.action_share: Toast.makeText(getActivity(), "share", Toast.LENGTH_SHORT).show(); break; case R.id.action_settings: Toast.makeText(getActivity(), "settings", Toast.LENGTH_SHORT) .show(); break; default: break; } mode.finish(); return true; } @Override public void onDestroyActionMode(ActionMode mode) { mCheckBox.setChecked(false); } }
2.菜单文件 menu_context_for_activity.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_delete" android:icon="@android:drawable/ic_menu_delete" android:showAsAction="ifRoom|withText" android:title="@string/delete" android:titleCondensed="delete"> </item> <item android:id="@+id/action_share" android:showAsAction="ifRoom|withText" android:title="@string/share"/> <item android:id="@+id/action_send" android:showAsAction="ifRoom|withText" android:title="@string/send"/> <item android:id="@+id/action_settings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_settings"/> </menu>
3.布局文件 frgmt_cab_for_activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/aty_action_mode" /> <CheckBox android:id="@+id/vid_check_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/view_s_check_mode" /> </LinearLayout>
二. ListView 用Context Action Bar 多选删除示例
1.MultiCabFrgmt.java
public class MultiCabFrgmt extends Fragment implements MultiChoiceModeListener { private ListView mListView; private SelectionArrayAdapter adapter; private List<ItemData> data; private ShareActionProvider mShareActionProvider; public boolean isItemSelected(int position) { return false; } private void init() { data = new ArrayList<ItemData>(); ItemData ida; for (int i = 0; i < 32; ++i) { ida = new ItemData(); ida.txt = "item" + i; data.add(ida); } adapter = new SelectionArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, data); mListView.setAdapter(adapter); mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);//CHOICE_MODE_SINGLE mListView.setMultiChoiceModeListener(this); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.frgmt_cab_for_listview, container, false); mListView = (ListView) v.findViewById(R.id.id_listview); init(); return v; } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.menu_context_for_listview, menu); MenuItem mi = menu.findItem(R.id.action_share); mShareActionProvider = (ShareActionProvider) mi.getActionProvider(); mShareActionProvider.setShareIntent(new Intent(Intent.ACTION_SEND) .setType("image/*")); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return true; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.action_delete: { for (int i = data.size() - 1; i >= 0; --i) { if (data.get(i).selected) { data.remove(i); } } } break; case R.id.action_share: break; case R.id.action_settings: } mode.finish(); return true; } @Override public void onDestroyActionMode(ActionMode mode) { for (int i = 0; i < data.size(); i++) { data.get(i).selected = false; } } @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { ItemData ida = data.get(position); ida.selected = !ida.selected; adapter.notifyDataSetChanged(); mode.setTitle(mListView.getCheckedItemCount() + " selected"); } }
2.SelectionArrayAdapter.java
public class SelectionArrayAdapter extends ArrayAdapter<ItemData> { private List<ItemData> data; private LayoutInflater inflater; private int resource; private void init(Context context,int resource,List<ItemData> data){ this.data = (ArrayList<ItemData>) data; inflater = LayoutInflater.from(context); this.resource = resource; } public SelectionArrayAdapter(Context context, int resource, int textViewResourceId, List<ItemData> data) { super(context, resource, textViewResourceId, data); init(context,resource,data); } public SelectionArrayAdapter(Context context, int resource, List<ItemData> data) { super(context, resource, data); init(context,resource,data); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (null == convertView) { convertView = inflater.inflate(this.resource, null); } TextView tv = (TextView) convertView.findViewById(android.R.id.text1); ItemData item = getItem(position); tv.setText(item.txt); if (item.selected) { convertView.setBackgroundColor(0x7f0f0f0f);// this is a selected }else{ convertView.setBackgroundColor(0x7ff0f0f0); // default color } return convertView; } @Override public ItemData getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public void clear() { super.clear(); } @Override public int getCount() { int sz = data.size(); return sz; } }
3.Context Action Bar的操作项菜单文件 menu_context_for_listview.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_share" android:orderInCategory="100" android:actionProviderClass="android.widget.ShareActionProvider" android:showAsAction="always" android:title="@string/share"/> <item android:id="@+id/action_delete" android:showAsAction="ifRoom|withText" android:title="@string/delete"/> <item android:id="@+id/action_settings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_settings"/> </menu>
时间: 2024-10-11 08:35:41