我有一个ExpandableListView,我想对其进行编辑。因此,当用户在项目上“ longClicks”时,我正在显示“编辑对话框”。但是,当我在组的convertView上实现onLongClickListener时,单击后便不再扩展。如果我注释掉设置onLongClickListener的代码,则该组将按预期扩展。如何阻止LongClickListner窃取该组的convertView中的click事件?

其他人似乎也有这个问题,可以接受的答案是在LongClickListener中返回false,但这对我不起作用。

这是我获取groupView并设置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;
}


对话框方法代码:

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();
}

最佳答案

而不是在getGroupView中,应该从您的活动中调用setOnItemLongListener。在定义可扩展列表视图的活动中使用以下代码。

expandableListView.setOnItemLongClickListener(new OnItemLongClickListener() {

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

07-24 09:19