Contextual Action bar(3) 两个示例
一.通过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) 两个示例的更多相关文章
- Contextual Action bar(2) 简介,启动,各函数介绍
一.Context Action Bar简介 它是一个ActionBar,有各种操作项,但它不是始终显示的ActionBar,它需要上下文才显示.样式如下: 二.Context Action Bar的 ...
- Contextual Action bar(1) CAB in Android
Contextual Action bar (CAB) in Android BY PARESH MAYANI - OCTOBER, 23RD 2013 Before getting into the ...
- Action Bar详解
Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...
- Android UI开发第二十四篇——Action Bar
Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...
- 【转】Android UI开发第二十四篇——Action Bar
Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...
- Android Contextual Menus之二:contextual action mode
Android Contextual Menus之二:contextual action mode 接上文:Android Contextual Menus之一:floating context me ...
- 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 ...
- 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 ...
- Android系统UI交互控件Action Bar初探
过年期间,Google正式宣布取消Android系统中MENU键的使用,也就是基于Android 4.0系统的手机都应没有MENU这一固定按键.这无疑是个变革性的改动,在我眼中,这似乎把Android ...
随机推荐
- C#令人迷惑的DateTime:世界标准时间还是本地时间?
先来看一段代码: 复制内容到剪贴板程序代码 DateTime time = DateTime.Parse("2013-07-05 00:00:00");Console.WriteL ...
- LeetCode(27)题解:Remove Element
https://leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of th ...
- bash shell中的特殊用法
1 ${BASH_SOURCE-$0} 表示当前正在执行的脚本的完整路径. 2 cd -P 以物理路径进入 /usr/localhadoop现在链到/home/houzhizhen/usr/local ...
- zTree async 动态参数处理
async:{ enable: true,//开启异步机制 url: opts.url,//异步地址 otherParam: { 'plateNo': function(){ return $('# ...
- Java WebSocket库:https://github.com/TooTallNate/Java-WebSocket
https://github.com/TooTallNate/Java-WebSocket 以下是简单示例: import com.google.gson.JsonObject; import com ...
- luogu3384 【模板】 树链剖分
题目大意 已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作:操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z操作2: 格式: 2 x ...
- react项目中的注意点
一.ES6 的编译方法 目前主流的浏览器还不支持ES6. 现在一般采用webpack 和 <script type="text/babel">对jsx 语法进行编译, ...
- Linux内核日志开关
Linux内核日志开关 1.让pr_debug能输出 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -59,7 +59,7 ...
- YTU 2419: C语言习题 等长字符串排序
2419: C语言习题 等长字符串排序 时间限制: 1 Sec 内存限制: 128 MB 提交: 650 解决: 249 题目描述 在主函数中输入n(n<=10)个等长的字符串.用另一函数对 ...
- INFO: Ignoring response <403 https://movie.douban.com/top250>: HTTP status code is not handled or not allowed
爬取豆瓣电影top250,出现以下报错: 2018-08-11 22:02:16 [scrapy.core.engine] INFO: Spider opened 2018-08-11 22:02:1 ...