读书人

ListView的长按菜单_源码分析

发布时间: 2012-09-28 00:03:35 作者: rapoo

ListView的长按菜单___源码分析
ListView的长按菜单___源码分析

Android的listview可以长按弹出来一个菜单。
今天就跟了下代码大概看了下弹出菜单的流程。
我们实现一个菜单长按步骤通常如下:

1.弹出菜单的生成
如果控制listview长按应该生成什么样的菜单。
a、生成一个OnCreateContextMenuListener的接口对象
该接口定义如下:在view.java中

private boolean performLongPress(final View child,final int longPressPosition, final long longPressId) {      boolean handled = false;      if (mOnItemLongClickListener != null) {          handled = mOnItemLongClickListener.onItemLongClick(AbsListView.this, child,                  longPressPosition, longPressId);      }       if (!handled) {          mContextMenuInfo = createContextMenuInfo(child, longPressPosition, longPressId);          handled = super.showContextMenuForChild(AbsListView.this);      }      if (handled) {          performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);      }      return handled;  }  ContextMenuInfo createContextMenuInfo(View view, int position, long id) {      return new AdapterContextMenuInfo(view, position, id);  }  ContextMenuInfo createContextMenuInfo(View view, int position, long id) {      return new AdapterContextMenuInfo(view, position, id);  }


就是我们在构造菜单时候所用到的ContextMenuInfo.

如果我们要给其它自己定义的View,响应长按菜单。这时候我们就需要重写该View的getContextMenuInfo()。

根据View当前状态生成不同的ContextMenuInfo,进而决定应该弹出什么样的菜单。


1 楼 mm4409092 2012-05-05 android listview综合使用示例_结合数据库操作和listitem单击长按等事件处理
http://blog.csdn.net/lk_blog/article/details/7537200

读书人网 >移动开发

热点推荐