长按一下,阻止扩展可扩展列表视图

时间:2016-04-14 10:06:18

标签: java android android-studio expandablelistview onitemlongclicklistener

我正在开发一个Android应用程序,我在其中使用Expandablelistview来显示一些数据。目前,列表视图将在单击组和长时间单击后释放时进行扩展。但是,我需要阻止在长按一下扩展Expandablelistview

我的代码段如下所示:

elvItemList = (ExpandableListView) root.findViewById(R.id.elv_item_list);
elvItemList.setOnGroupClickListener(this);
elvItemList.setAdapter(smListAdapter);
elvItemList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            Utils.logit("SMLOG", "Long button pressed");
            //
        }
        return false;
    }
});

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

ExpandableListView.PACKED_POSITION_TYPE_GROUP是组的ID,将其更改为 ExpandableListView.PACKED_POSITION_TYPE_CHILD您可以使用longclicks对组童子进行操作。

类似的东西:

elvItemList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
// Your code with group long click
return true;
}
return false;
}
});

答案 1 :(得分:0)

public void stopExpandOnLongClick()
{
    expListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            boolean checkClick=false;
            long packedPosition = expListView.getExpandableListPosition(position);

            int itemType = ExpandableListView.getPackedPositionType(packedPosition);
            int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);

             /*  if group item clicked */
            if (itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                //  ...
                if(expListView.isGroupExpanded(groupPosition))
                {
                    Toast.makeText(MainActivity.this, "It's normal group collaspe", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this, "This Grouo is not going to expand on long click", Toast.LENGTH_LONG).show();
                    //you can put your logic when you long press on Group header
                    checkClick=true;
                }
            }
            return checkClick;
        }
    });

}

答案 2 :(得分:0)

这适合我。

expandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            return true; // Change return false; to return true;
        }
    });