一.通过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>

Contextual Action bar(3) 两个示例的更多相关文章

  1. Contextual Action bar(2) 简介,启动,各函数介绍

    一.Context Action Bar简介 它是一个ActionBar,有各种操作项,但它不是始终显示的ActionBar,它需要上下文才显示.样式如下: 二.Context Action Bar的 ...

  2. Contextual Action bar(1) CAB in Android

    Contextual Action bar (CAB) in Android BY PARESH MAYANI - OCTOBER, 23RD 2013 Before getting into the ...

  3. Action Bar详解

    Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...

  4. Android UI开发第二十四篇——Action Bar

    Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...

  5. 【转】Android UI开发第二十四篇——Action Bar

    Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...

  6. Android Contextual Menus之二:contextual action mode

    Android Contextual Menus之二:contextual action mode 接上文:Android Contextual Menus之一:floating context me ...

  7. Android设计和开发系列第二篇:Action Bar(Develop—API Guides)

    Action Bar IN THIS DOCUMENT Adding the Action Bar Removing the action bar Using a logo instead of an ...

  8. Android设计和开发系列第二篇:Action Bar(Design)

    Action Bar The action bar is a dedicated piece of real estate at the top of each screen that is gene ...

  9. Android系统UI交互控件Action Bar初探

    过年期间,Google正式宣布取消Android系统中MENU键的使用,也就是基于Android 4.0系统的手机都应没有MENU这一固定按键.这无疑是个变革性的改动,在我眼中,这似乎把Android ...

随机推荐

  1. iOS面试常见题

    1.耶稣有13个门徒,当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13个人围坐一圈,从第一个人開始循环报数,数到三排除,最后剩下的人就是叛徒 int people[13] = {1,2,3, ...

  2. iOS开发——高级篇——多线程的安全隐患

    资源共享 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象.同一个变量.同一个文件 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题   一.解 ...

  3. UOJ #35. 后缀排序[后缀数组详细整理]

    #35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符 ...

  4. MapReduce算法形式三:cleanup

    案例三:cleanup 其实这个案例可以不用写这么复杂,不用cleanup也能写,但是为了,突显,突显,突显(重要的事说四遍)cleanup的重要性,琢磨了半天,恩,这样写既可以突显cleanup又显 ...

  5. 如何解决Windows的端口占用问题?

    已知某应用在启动时会创建服务套接字,并将其绑定至端口8888,然而端口8888已被占用,如何解除占用? 以下为解决方案: 在cmd中运行netstat -ano|findstr 8888,其中的参数8 ...

  6. Vue中 key keep-alive

    keep-alive key <!DOCTYPE html> <html> <head> <title></title> <scrip ...

  7. hdu 超级楼梯 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 哦--对了,这些题读者可以直接忽略,我只是想练习一下自己薄弱的地方...... 题目意思我就不说 ...

  8. 如何在cowboy应用中指定mnesia数据库路径

    创建mnesia数据库的步骤简述: 1)定义脚本: -module(mns). -export([setup/0, clean/0]). -record(user, { id, coin, diamo ...

  9. UVA-10600(次小生成树)

    题意: 现在给一个图,问最小生成树和次小生成树的权值和是多少; 思路: 求最小生成树的两种方法,次小生成树是交换最小生成树的其中一条边得到的,现在得到了最小生成树,枚举不在次小生成树中的边,再求一边最 ...

  10. [Selenium] 操作新弹出窗口之验证标题和内容

    1)验证标题 package com.learningselenium.normalwebdriver; import static org.junit.Assert.*; import java.u ...