ListView配合popmenu的使用
来源:互联网 发布:淘宝网玩具批发市场 编辑:程序博客网 时间:2024/05/29 03:09
项目中需要在一个Listview不仅有OnItemClicked还需要item中某一个按钮具有单独的点击事件。在安卓官网中 已经有了类似的实现
官网地址https://developer.android.com/samples/ActionBarCompat-ListPopupMenu/index.html
代码主要是在ListView的apdater中的getView中
/** * A simple array adapter that creates a list of cheeses. */class PopupAdapter extends ArrayAdapter<String> { PopupAdapter(ArrayList<String> items) { super(getActivity(), R.layout.list_item, android.R.id.text1, items); } @Override public View getView(int position, View convertView, ViewGroup container) { // Let ArrayAdapter inflate the layout and set the text View view = super.getView(position, convertView, container); // BEGIN_INCLUDE(button_popup) // Retrieve the popup button from the inflated view View popupButton = view.findViewById(R.id.button_popup); // Set the item as the button's tag so it can be retrieved later popupButton.setTag(getItem(position)); // Set the fragment instance as the OnClickListener popupButton.setOnClickListener(PopupListFragment.this); // END_INCLUDE(button_popup) // Finally return the view to be displayed return view; }}而后是点击事件的代码
@Overridepublic void onClick(final View view) { // We need to post a Runnable to show the popup to make sure that the PopupMenu is // correctly positioned. The reason being that the view may change position before the // PopupMenu is shown. view.post(new Runnable() { @Override public void run() { showPopupMenu(view); } });}
// BEGIN_INCLUDE(show_popup)private void showPopupMenu(View view) { final PopupAdapter adapter = (PopupAdapter) getListAdapter(); // Retrieve the clicked item from view's tag final String item = (String) view.getTag(); // Create a PopupMenu, giving it the clicked view for an anchor PopupMenu popup = new PopupMenu(getActivity(), view); // Inflate our menu resource into the PopupMenu's Menu popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu()); // Set a listener so we are notified if a menu item is clicked popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.menu_remove: // Remove the item from the adapter adapter.remove(item); return true; } return false; } }); // Finally show the PopupMenu popup.show();}// END_INCLUDE(show_popup)这样就实现了一个listview的一个条目不同点击事件
0 0
- ListView配合popmenu的使用
- PopMenu的使用
- LIstView与Adapter的配合使用
- 加载listview配合适配器的使用
- ListView+CursorAdapter+LoaderManger的配合使用
- RadToolTipManager, usercontrol, ListView配合使用
- Android ListView配合ListViewHolder使用
- PopMenu效果的学习
- PopMenu菜单的实现
- popmenu的用法
- 对话框样式的PopMenu
- PopMenu
- C#中ListView和ImageList配合使用
- Android中ListView与ArrayAdapter配合使用
- Android中ListView与ArrayAdapter配合使用
- Android ContextMenu和Listview配合使用
- Android ListView-各种Adapter配合使用
- Android中ListView与ArrayAdapter配合使用
- 架构衍变过程----58同城沈剑:好的架构源于不停地衍变,而非设计
- iOS 自定义返回按钮,仍实现右滑返回的功能.
- 通知和kvo以及代理的区别
- Populating Next Right Pointers in Each Node
- 从java内存分配角度分析android内存泄漏问题
- ListView配合popmenu的使用
- 啊哦!还是语言关
- 关于分包后的apk运行发生java.lang.NoClassDefFoundError的问题
- java日期类
- android的日志工具log
- RxVelloy 简介
- ubuntu13.04 安装jdk6
- 安卓四种启动模式使用场景
- 在vs中用C++编程的方式生成.dll文件和以动态、静态调用dll中函数