`
jean7155
  • 浏览: 63403 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Contextual Action Mode

阅读更多
    我在网上查了N多文章, 结果发现,在ANDROID自带的APIDEMO中,居然有Contextual Action Mode的例子, 真是踏破铁鞋无觅处,得来全不费工夫啊!!

    这个东东很有意思, 比如在一个ListView中,当长点击某个item时, 在android的header部分会出现一个勾,旁边显示有多少项已被选择。 这个功能比较适合用于数据的batch操作。

1. 先在需要使用该功能的activity或ListFragment里创建一个callback类,执行ListView.MultiChoiceModeListener.我是使用在ListFragment中,如果要在Activity中执行,将getActivity()的方法替换成XXActivity.this即可。
	 private class ModeCallback implements ListView.MultiChoiceModeListener {
                // 设置一个menu,menu显示在header的最右边,在左边,显示这个menu的标题:被选择的项目,然后使用setSubtitle方法显示被选择了多少项。
	        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    // 在Activity中,比如Activity的名称叫MyActivity
                    // MenuInflater inflater = MyActivity.this.getMenuInflater();
	            MenuInflater inflater = getActivity().getMenuInflater();
	            
	            inflater.inflate(R.menu.list_select_menu, menu);
	            mode.setTitle("被选择的项目");
	            setSubtitle(mode);
	            return true;
	        }

	        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
	            return true;
	        }
                // 当点击menu中的菜单项时,运行自定义的操作, 此处只显示一些提示信息。
	        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
	            switch (item.getItemId()) {
	            case R.id.share:
	                Toast.makeText(getActivity(), "Shared " + getListView().getCheckedItemCount() +
	                        " items", Toast.LENGTH_SHORT).show();
	                mode.finish();
	                break;
	            default:
	                Toast.makeText(getActivity(), "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("选择了1项");
	                    break;
	                default:
	                    mode.setSubtitle("选择了" + checkedCount + "项");
	                    break;
	            }
	        }
	    }



2. 在Activity中调用这个类, 一般是在create的时候调用
。。。。。。
ListView lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setMultiChoiceModeListener(new ModeCallback());
。。。。。。


3. 备注:menu/list_select_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     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.
-->

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/share"
          android:title="Share"
          android:icon="@android:drawable/ic_menu_share"
          android:showAsAction="always" />
</menu>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics