android ContextMenu 长按菜单 整理 摘

 

listview 的条目长按出菜单

 

 

一。前提概念

 单级目录的list 长按菜单  

AdapterView.AdapterContextMenuInfo

 

 int i = ((AdapterContextMenuInfo)menuInfo).position;
  
  
   menu.setHeaderTitle(messages.get(i).toString()).setHeaderIcon(
   R.drawable.icon);

 

多级目录的ExpandableList  长按选择菜单 :

 

ExpandableListView.ExpandableListContextMenuInfo

ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
String title = ((TextView) info.targetView
 .findViewById(R.id.chatting_name_tv)).getText().toString();
Drawable icon = ((ImageView) info.targetView
.findViewById(R.id.contactIcon)).getDrawable();
  long position = info.packedPosition;

 

 

2.注册 菜单 :

1.registerForContextMenu(chatHistoryLv);

2.//   chatHistoryLv.setOnCreateContextMenuListener(this);

 

还可以 单级 如果 class extends ListActivity :

registerForContextMenu(getListView());

多级  如果 class extends ExpandableListView:

registerForContextMenu(getExpandableListView());

 

1.同2 只是把2封装在源代码里 可点源代码看

 

(与1对应还可以 不用的时候注销注册 菜单 :unRegisterForContextMenu(chatHistoryLv);或.setOnCreateContextMenuListener(null);

 

3.注册菜单事件 :

1.直接建事件在 新建那里:

chatHistoryLv.setOnCreateContextMenuListener(
   new OnCreateContextMenuListener() {

  
   public void onCreateContextMenu(ContextMenu menu, View v,
  
   ContextMenuInfo menuInfo) {
   Log.i(TAG, "chatHistoryLv.setOnCreateContextMenuListener");
   // TODO Auto-generated method stub
  
   menu.setHeaderTitle("长按按钮-ContextMenu");
  
   menu.add(0,0,0,"弹出长按钮菜单子菜单1");
  
   menu.add(0,0,0,"弹出长按钮菜单子菜单2");
  
   }
  
   });

 

2.在外面构:

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  System.out.println("SmsRoom-----oncreateContextMenu");
  Log.d(TAG, "onCreateContextMenu");

  /*
   * one 多级菜单
   */
  
//  Log.i(TAG,"menuInfo.toString==="+menuInfo.toString());
//  ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
//
//  String title = ((TextView) info.targetView
//    .findViewById(R.id.chatting_name_tv)).getText().toString();
//  // Drawable icon = ((ImageView) info.targetView
//  // .findViewById(R.id.contactIcon)).getDrawable();
//
//  long position = info.packedPosition;

  /*
   * two 单级菜单

   */
  
   int i = ((AdapterContextMenuInfo)menuInfo).position;
  
  
   menu.setHeaderTitle(messages.get(i).toString()).setHeaderIcon(
   R.drawable.icon);

 

  menu.add(0, MENU_MSG_EDIT, 0,
    this.getResources().getString(R.string.menu_edit));
  menu.add(0, MENU_MSG_COPY, 0,
    this.getResources().getString(R.string.menu_copy));
  menu.add(0, MENU_MSG_DELETE, 0,
    this.getResources().getString(R.string.menu_delete));
  menu.add(0, MENU_MSG_MOVE, 0,
    this.getResources().getString(R.string.menu_intransit));

 }

 

4.写菜单里面的条目事件 :

 

1.单级菜单:

 @Override
  public boolean onContextItemSelected(MenuItem item) {
  Log.d(TAG, "clickContextMenu");
 
  setTitle("点击了长按菜单里面的第" + item.getItemId() + "个项目");
  return super.onContextItemSelected(item);
  }

 

2.多级菜单:

@Override
 public boolean onContextItemSelected(MenuItem item) {

  boolean flag = false;

  // TODO Auto-generated method stub
  
  ExpandableListContextMenuInfo menuInfo = (ExpandableListContextMenuInfo) item
    .getMenuInfo();

  /*
   *
   */
  String title = ((TextView) menuInfo.targetView).getText().toString();

  int type = ExpandableListView
    .getPackedPositionType(menuInfo.packedPosition);

  if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {

   int groupPos = ExpandableListView
     .getPackedPositionGroup(menuInfo.packedPosition);

   int childPos = ExpandableListView
     .getPackedPositionChild(menuInfo.packedPosition);

   CharSequence str = "您单击了" + title;

   Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

   Log.d(TAG, "Run Hereing...");

   flag = true;

  }

  else {

   if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {

    int groupPos = ExpandableListView
      .getPackedPositionGroup(menuInfo.packedPosition);

    CharSequence cs = "您单击了" + title;

    Toast.makeText(this, cs, Toast.LENGTH_SHORT).show();

    Log.d(TAG, "Run Here...");

    flag = true;

   }

  }

  return flag;

 }

 

5.若还要实现 上级 listview条目的长按

 

则要让长按事件.setOnItemLongClickListener 返回false 才能 让获焦事件 扔到setOnCreateContextMenuListener 菜单兼听事件这层

 

 

7.注销 菜单事件:

 

 1.unregisterForContextMenu(view);

2.view.setOnCreateContextMenuListener(null);

<1.2 相同 源代码中:

public void unregisterForContextMenu(View view) {
        view.setOnCreateContextMenuListener(null);
    }>

 

 

-----------------------------------注 :

 

在Android的菜单分两种:options menu 、context menu 。(也有分三种的:options menu 、context menu、sub menu)

 

其中options menu是和Activity相关联的,context menu 是和View关联。

 

 

 

---------------------------------------------

1.this.registerForContextMenu(getListView());   

getListView());

 

这个 用在  class AActivity extends ListActivity 

 

registerForContextMenu(getExpandableListView());

getExpandableListView();

 

这个 用在  class extends ExpandableListActivity

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值