如何在 ExpandableListView [Android] 组中设置 LongClick

How can I set LongClick on group in ExpandableListView [Android]

我有一个 ExanpdableLisView,我必须为孩子实现 setOnChildClickListener 方法,为 group/parent 实现 "LongClick"。

我有孩子的,但我不知道如何实现 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。

长按群组或子视图,

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() 方法来做任何你想做的事情。

试试这个。这会起作用。

这并不是最干净的解决方案,但在您的适配器中,您可以在 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
            }
        });
     }

}

希望对您有所帮助!