安卓:长点击一个ExpandableListView的孩子的意见? [英] Android: long click on the child views of a ExpandableListView?
问题描述
ExpandableListView有setOnChildClickListener方法,但缺乏 setOnChild 龙 ClickListener 方式。
ExpandableListView has a setOnChildClickListener method, but lacks of setOnChildLongClickListener method.
当我在 getChildView()
,整个子列表变得完全不可点击添加 setOnLongClickListener()
的子视图(尽管对 parentView.setOnChildClickListener()
present和工作之前)。
When I added setOnLongClickListener()
on child view in getChildView()
, whole sublist became completely unclickable (despite of parentView.setOnChildClickListener()
present and working before).
我怎样才能使长时间点击孩子的意见?
How can I enable long clicks on child views?
推荐答案
我发现史蒂夫奥利弗在这里的博客答案:<一href="http://steveoliverc.word$p$pss.com/2009/10/16/context-menus-for-expandable-lists/">http://steveoliverc.word$p$pss.com/2009/10/16/context-menus-for-expandable-lists/
I found an answer on Steve Oliver's blog here: http://steveoliverc.wordpress.com/2009/10/16/context-menus-for-expandable-lists/
您应该使用 onCreateContextMenu()
而不是寻找 setOnChildLongClickListener()
。这是史蒂夫的信息:
You should use onCreateContextMenu()
instead of looking for setOnChildLongClickListener()
. Here is Steve's info:
的可扩展列表支持上下文菜单中的pretty的大致相同的方式,一个标准的清单上:增加对上下文菜单的监听器(当用户有长期pressed一个列表项) 。不同于标准的列表视图,但是,您可能想知道用户是否已经选择了一组(扩展项目)还是一个孩子(分项)列表项。的
此外,你可能不希望当用户试图打开一个上下文菜单一组项目做任何事情。可能有这样的情况,您都需要做下的一组东西向所有的孩子,但在我Librarium的应用程序,我想忽略组项目和present只针对儿童的上下文菜单。的
首先,你需要知道什么时候该上下文菜单将被创建,这样就可以识别是否pssed一组或一个子用户$ P $。如果他们$ pssed在群P $,则取消该上下文菜单。这也给了我们一个机会,让孩子项目的文本,这样我们就可以把它放到上下文菜单的标题。的
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info =
(ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type =
ExpandableListView.getPackedPositionType(info.packedPosition);
int group =
ExpandableListView.getPackedPositionGroup(info.packedPosition);
int child =
ExpandableListView.getPackedPositionChild(info.packedPosition);
// Only create a context menu for child items
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
{
// Array created earlier when we built the expandable list
String page =mListStringArray[group][child];
menu.setHeaderTitle(page);
menu.add(0, MENU_READ, 0, "Read page");
menu.add(0, MENU_EDIT, 0, "Edit page");
menu.add(0, MENU_FAVORITE, 0, "Add page to favorites");
menu.add(0, MENU_EXPORT, 0, "Export page to file");
menu.add(0, MENU_DELETE, 1, "Delete page");
}
}
其次,创建上下文菜单:
Secondly, create the Context Menu:
public boolean onContextItemSelected(MenuItem menuItem)
{
ExpandableListContextMenuInfo info =
(ExpandableListContextMenuInfo) menuItem.getMenuInfo();
int groupPos = 0, childPos = 0;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
{
groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
}
// Pull values from the array we built when we created the list
String author = mListStringArray[groupPos][0];
String page = mListStringArray[groupPos][childPos * 3 + 1];
rowId = Integer.parseInt(mListStringArray[groupPos][childPos * 3 + 3]);
switch (menuItem.getItemId())
{
case MENU_READ:
readNote(rowId);
return true;
case MENU_EDIT:
editNote(rowId);
return true;
// etc..
default:
return super.onContextItemSelected(menuItem);
}
}
就是这样。现在,用户可以长期preSS在项目上的可扩展列表,并获得在上下文菜单中,如果它是一个子项。的
这篇关于安卓:长点击一个ExpandableListView的孩子的意见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!