如何在ExpandableListView [Android]中设置LongClick on group

时间:2015-02-17 12:30:42

标签: java android android-listview expandablelistview

我有一个ExanpdableLisView,我必须为子项和" LongClick"实现方法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;
            }
        });

但我不知道如何对Group / parent进行LongClick。

2 个答案:

答案 0 :(得分:12)

要长时间点击群组或子视图,

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()方法以执行任何操作。

试试这个。这将有效。

答案 1 :(得分:0)

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

}

希望它有所帮助!