Java 如何在ExpandableListView[Android]中设置长点击组

Java 如何在ExpandableListView[Android]中设置长点击组,java,android,android-listview,expandablelistview,Java,Android,Android Listview,Expandablelistview,我有一个ExanpdableLisView,我必须为孩子实现setOnChildClickListener方法,为组/父级实现“LongClick” 我已经做了一个孩子的,但我不知道如何实现长点击 这是setOnChildClickListener expListView.setOnChildClickListener(new OnChildClickListener() { public boolean onChildClick(ExpandableListView

我有一个ExanpdableLisView,我必须为孩子实现setOnChildClickListener方法,为组/父级实现“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;
            }
        });

但我不知道如何长时间单击组/父视图。

长时间单击组或子视图

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;
    }
});
getExpandableListView().setOnItemLongClickListener(新的OnItemLongClickListener()){
推翻
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
long packedPosition=m_expandableListView.getExpandableListPosition(位置);
int itemType=ExpandableListView.getPackedPositionType(packedPosition);
int groupPosition=ExpandableListView.getPackedPositionGroup(packedPosition);
int childPosition=ExpandableListView.getPackedPositionChild(packedPosition);
/*如果单击组项目*/
if(itemType==ExpandableListView.PACKED\u POSITION\u TYPE\u GROUP){
//  ...
onGroupLongClick(groupPosition);
}
/*如果单击子项*/
else if(itemType==ExpandableListView.PACKED\u POSITION\u TYPE\u CHILD){
//  ...
onChildLongClick(groupPosition,childPosition);
}
返回false;
}
});
定义
onGroupLongClick()
onChildLongClick()
方法来执行任何您想要的操作


试试这个。这将起作用。

这并不是最干净的解决方案,但在适配器中,您可以在创建每个视图时,在getGroupView方法中为它们注册一个onLongClick侦听器

public override View GetGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
     if(converView == null)
     {
        View view = new View(context);
        view.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v)
            {     
                //enter code here
            }
        });
     }

}

希望有帮助

可能重复的工作完美。谢谢您知道这里的
id
参数代表什么吗?它的值与您从
OnChildClickListener.onChildClick()
中的
id
参数中得到的值不同。谢谢@jonte,这可能会在将来帮助我,在单击的组上有不同的“反应”,并使主代码更干净。Zygoltelnit的答案非常完美