实现OnLongClickListener时,ExpandableListView不会展开

时间:2016-01-02 05:20:55

标签: android expandablelistview onlongclicklistener

我有一个ExpandableListView,我想要编辑哪些项目。所以我正在显示一个"编辑对话框"当用户长时间点击'在一个项目上。但是我在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();
}

2 个答案:

答案 0 :(得分:1)

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

expandableListView.setOnItemLongClickListener(new OnItemLongClickListener() {

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

答案 1 :(得分:0)

将OnChildClickListener用于可扩展列表视图