Search code examples
androidexpandablelistviewonlongclicklistener

ExpandableListView not expanding when OnLongClickListener is implemented


I have an ExpandableListView which items I want to be editable. So I was showing an "edit dialog" when the user 'longClicks' on an item. But after I implemented the onLongClickListener on the convertView for the group, it no longer expands on a click. If I comment out the code where I set the onLongClickListener, the group expands as expected. How do I stop the LongClickListner from stealing the click event from the group's convertView?

Others seemed to have this problem as well and the accepted answer was to return false in the LongClickListener, but that does not work for me.

Here is where I get the groupView and set the LongClickListener:

...
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    final Crew crew = (Crew) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.crew_item_row, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.crewTxtView);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(crew.getName());

    //set ya onlclick listener here too for ya button to add crew member
    RelativeLayout addMemberBttn = (RelativeLayout) convertView.findViewById(R.id.addCrewMemberBttn);
    addMemberBttn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showNewMemberDialog(crew.getId());
        }
    });

    convertView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showEditCrewDialog(crew);
            return false;
        }
    });

    return convertView;
}

The dialog method code:

private void showEditCrewDialog(final Crew crew){
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.edit_crew_dialog);

    final EditText crewTextBox = (EditText) dialog.findViewById(R.id.crewEditTxt);
    crewTextBox.setText(crew.getName());
    final ImageView editButton = (ImageView) dialog.findViewById(R.id.editCrewBttn);
    editButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            crewTextBox.setEnabled(true);
            crewTextBox.setBackgroundResource(R.drawable.textbox);
            editButton.setVisibility(View.GONE);
        }
    });

    Button cancelBttn = (Button) dialog.findViewById(R.id.crewCancelBttn);
    cancelBttn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    Button updateBttn = (Button) dialog.findViewById(R.id.updateCrewDialogBttn);
    updateBttn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!crewTextBox.isEnabled()) {
                dialog.dismiss();
                return;
            }
            String crewName = crewTextBox.getText().toString();
            if(crewName.isEmpty()){
                Toast.makeText(context, "Crew must have a name...", Toast.LENGTH_SHORT).show();
                return;
            }

            crew.setName(crewName);
            CrewService crewService = new CrewService(context);
            if(crewService.updateCrew(crew)==0){
                Toast.makeText(context, "Error updating crew...", Toast.LENGTH_SHORT).show();
                return;
            };

            notifyDataSetChanged();
            Toast.makeText(context, "Crew updated!", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });

    dialog.show();
}

Solution

  • Instead of in getGroupView, you should call setOnItemLongListener from your activity. Use below code in your activity where expandable listview defined.

    expandableListView.setOnItemLongClickListener(new OnItemLongClickListener() {
    
            Override   
            public boolean onItemLongClick( AdapterView<?> parent, View view, int position, long id) {
                showEditCrewDialog(crew);
                return false;
            }
        });