我有一个 ExanpdableLisView,我必须为子项实现 setOnChildClickListener 方法,为组/父项实现“LongClick”方法。
我已经给 child 下了,但我不知道如何实现LongClick。
这是setOnChildClickListener
的代码
expListView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
RosterEntry rentry = (RosterEntry) exListAdapter.getChild(groupPosition, childPosition);
final String selected = rentry.getName();
//Change Toast to make the new functionality
Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG).show();
return true;
}
});
但我不知道如何在 Group/parent 上执行 LongClick。
最佳答案
长按组 View 或 subview ,
getExpandableListView().setOnItemLongClickListener(new OnItemLongClickListener() {
Override
public boolean onItemLongClick( AdapterView<?> parent, View view, int position, long id) {
long packedPosition = m_expandableListView.getExpandableListPosition(position);
int itemType = ExpandableListView.getPackedPositionType(packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
/* if group item clicked */
if (itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// ...
onGroupLongClick(groupPosition);
}
/* if child item clicked */
else if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
// ...
onChildLongClick(groupPosition, childPosition);
}
return false;
}
});
定义 onGroupLongClick()
和 onChildLongClick()
方法来执行您想要的任何操作。
试试这个。这会起作用。
关于java - 如何在 ExpandableListView 中的组上设置 LongClick [Android],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28561551/