gpt4 book ai didi

android - 实现 OnLongClickListener 时 ExpandableListView 不扩展

转载 作者:行者123 更新时间:2023-11-30 01:40:35 26 4
gpt4 key购买 nike

我有一个 ExpandableListView,我希望其中的项目是可编辑的。因此,当用户在某个项目上“长按”时,我会显示一个“编辑对话框”。但是在我为该组的 convertView 实现 onLongClickListener 之后,它不再在单击时展开。如果我注释掉我设置 onLongClickListener 的代码,该组将按预期展开。如何阻止 LongClickListner 从组的 convertView 窃取点击事件?

其他人似乎也有这个问题,接受的答案是在 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();
}

最佳答案

您应该从您的 Activity 中调用 setOnItemLongListener,而不是在 getGroupView 中。在定义了可扩展 ListView 的 Activity 中使用以下代码。

expandableListView.setOnItemLongClickListener(new OnItemLongClickListener() {

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

关于android - 实现 OnLongClickListener 时 ExpandableListView 不扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com