ContextMenuFragment使用分析menu dialog
2014-03-28 17:01
267 查看
public class FragmentContextMenu extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create the list fragment and add it as our sole content. ContextMenuFragment content = new ContextMenuFragment(); getFragmentManager().beginTransaction().add(android.R.id.content, content).commit(); } public static class ContextMenuFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_context_menu, container, false); registerForContextMenu(root.findViewById(R.id.long_press)); return root; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "Menu A"); menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "Menu B"); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.a_item: Toast.makeText(getActivity(), "Item 1a was chosen", Toast.LENGTH_SHORT).show(); return true; case R.id.b_item: Toast.makeText(getActivity(), "Item 1b was chosen", Toast.LENGTH_SHORT).show(); return true; } return super.onContextItemSelected(item); } } }
这个activity里面直接添加了一个fragment。fragment中的longpress响应,在oncreateview里面的这句话registerForContextMenu(root.findViewById(R.id.long_press));
为button注册了一个长按响应回调,响应回调的方法是onCreateContextMenu()。在回调里面添加了2个menu,而这2个的menu的点击响应方法是onContextItemSelected(),这个方法的监听注册应该是在父类中完成的
相关文章推荐
- ContextMenuFragment的使用
- slidingmenu+fragment实现经常使用的側滑效果(包含Fragment状态的保存)
- FragmentPagerAdapter和FragmentStatePagerAdapter 使用场景及源码分析
- C#winform中ListView及ContextMenuStrip的使用
- Fragment的进一步使用(三)--- 关于DialogFragment对话框,设备旋转与fragment,fragment间的通讯 , fragment的Menu
- 开源中国源码学习UI篇(一)之FragmentTabHost的使用分析
- Context Menu的使用+为ListView绑定监听器——开源项目Standup Timer学习总结(二)
- springboot源码分析10-ApplicationContextInitializer使用
- 深入分析:Fragment与Activity交互的几种方式(二,使用Bundle)
- ContextMenuStrip 菜单的使用技巧
- Android Fragment项目中使用及生命周期分析
- 使用context:property-placeholder载不进属性原因分析与解决方案
- c#中contextMenuStrip与datagridview使用CellMouseDown事件及treeview使用mousedown事件
- Android应用开发:LoaderManager在Activity/Fragment中的使用分析
- 深入分析:Fragment与Activity交互的几种方式(二,使用Bundle)
- Android编程 - Option Menu选项菜单&& Context Menu上下文菜单基本使用
- Android编程心得-使用ActionBar+Fragment+ViewPager实现动态切换Menu效果
- Context-Menu.Android库的使用(修改显示位置)
- APIDemo分析:fragment的使用方法
- iOS 绘图时使用 UIGraphicsGetCurrentContext() 报错的原因分析