我有一个包含ExpandableListView
的片段。我希望能够通过组选择和删除项目。我还希望能够通过上下文操作栏选择多个组项目进行删除。
到目前为止,我可以点击群组查看孩子,我可以点击孩子去另一个活动。我将ExpandableListView
的选择模式设置为CHOICE_MODE_MULTIPLE_MODAL
,如果我长按对某个组进行选择,则会显示一个上下文操作栏。如果我选择删除按钮,则删除该项目。都好。
但是,当我尝试在CAB模式下选择多个组时,会出现问题。它只是不起作用。如果我单击第二个组,则该组将展开(未选中)。我希望能够突出显示多个组项而不进行任何扩展。
有很多代码可以让它工作,但我会尝试展示一些相关的部分。主要问题是获取所选组项的列表。其次,我不希望这些组在被选中时被扩展(虽然CAB是可见的 - 这是我试图通过保持mActionMode
中的ActionMode)。
非常感谢任何帮助。
ex.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked) {
// Ignore long clicks on children
long pos = ex.getExpandableListPosition(position);
if (checked && (ExpandableListView.getPackedPositionType(pos) == ExpandableListView.PACKED_POSITION_TYPE_CHILD)) {
ex.setItemChecked(position, false);
return;
}
}
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
inflater.inflate(R.menu.context_delete_items, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
mActionMode = actionMode;
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem item) {
// If delete is clicked, delete the measurement set
switch(item.getItemId()) {
case(R.id.context_menu_delete_item):
Set<mySet> setsToDelete = new HashSet<mySet>();
if (ex != null) {
if (ex.getCheckedItemCount() > 0) {
SparseBooleanArray checkedPositions = ex.getCheckedItemPositions();
for (int i = 0; i < checkedPositions.size(); i++) {
int position = checkedPositions.keyAt(i);
/* Ignore selected children */
long pos = ex.getExpandableListPosition(position);
int groupPosition = ExpandableListView.getPackedPositionGroup(pos);
if (checkedPositions.valueAt(i)) {
Log.d(TAG, "Adding MeasurementSet at position Key = " + position + " to deletion list");
setsToDelete.add(mSets.get(groupPosition));
}
}
}
}
try {
if (ex != null && setsToDelete.size() > 0) {
ArrayList setsToDeleteList = new ArrayList(setsToDelete);
deleteSets(setsToDeleteList);
for (mySet s : setsToDelete) {
mSets.remove(s);
}
Toast.makeText(getActivity(), "Set deleted successfully", Toast.LENGTH_LONG).show();
}
} catch (SQLiteException sqlex) {
Log.e(TAG, "Delete operation failed");
Log.e(TAG, "Error was: " + sqlex.getMessage());
Log.e(TAG, "Stack trace: " + sqlex.getStackTrace());
Toast.makeText(getActivity(), "There was an error whilst deleting Set(s): " + sqlex.getMessage(), Toast.LENGTH_LONG).show();
}
actionMode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
mActionMode = null;
}
});
ex.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Long mId = mSets.get(groupPosition).getMyList().get(childPosition).getId();
Intent intent = new Intent(getActivity(), ViewSingleActivity.class);
intent.putExtra(getResources().getString(R.string.EXTRA_MID_KEY), measurementId);
startActivityForResult(intent, Constants.REQ_VIEW_M);
return true;
}
});
// Here I was trying to intercept group clicks and force the clicked group to collapse. Although this doesn't seem to solve the issue of having the group "selected"
ex.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (mActionMode != null) {
Log.d(TAG, "mActionMode is not null. Setting View: " + parent.toString() + " to be selected");
v.setSelected(true);
Log.d(TAG, "Collapsing group " + groupPosition);
parent.collapseGroup(groupPosition);
}
return false;
}
});
}
答案 0 :(得分:1)
虽然这是一篇很老的帖子,但如果有人偶然发现,我会发布一个答案。问题是ExpandableListView
不支持ChoiceMode。虽然它似乎有效,你可以手动调用ExpandableListView
来设置检查项目...它永远不会完全正常工作。为了更深入的原因,为什么;请参阅this和this SO回答。
OP没有说明他们是否编写了自己的自定义适配器。所以我将解决这两种方法。
如果您想编写自己的自定义ExpandableListAdapter
,建议您使用PatchedExpandabeListAdapter。你可以阅读它here。基本上它补丁并改进了一些东西......其中一个是由OP提出的ChoiceMode问题。
如果你不想推出自己的解决方案,那么找到两个很好的完全成熟的Rolodex适配器here来实现PatchedExpandabeListAdapter
以及除视图生成之外你需要的大部分内容。