由于 android UI 指南,我尝试ActionBar
为ExpandableListView
在SherlockFragment
. 我找不到解决方案来让它运行。
我使用的最后一个教程是这个,但它不适用于ListView
.
有没有办法让这种组合发挥作用?
这是 SherlockFragment 的代码:
public class ResultFragment extends SherlockFragment implements OnGroupClickListener {
private ExpandableListView mExpandableListView;
private ResultExpandableAdapter mResultExpandableAdapter;
private ActionMode mActionMode;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.fragment_result, container, false);
mResultExpandableAdapter = new ResultExpandableAdapter(getSherlockActivity());
mExpandableListView = (ExpandableListView) fragmentView.findViewById(R.id.fragment_result_expandableListView);
mExpandableListView.setAdapter(mResultExpandableAdapter);
mExpandableListView.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
mExpandableListView.setOnGroupClickListener(this);
return fragmentView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.result_fragment, menu);
}
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
SparseBooleanArray checked = mExpandableListView.getCheckedItemPositions();
boolean hasCheckedElement = false;
for (int i = 0 ; i < checked.size() && ! hasCheckedElement; ++i) {
hasCheckedElement = checked.valueAt(i);
}
if (hasCheckedElement) {
if (mActionMode == null) {
mActionMode = getSherlockActivity().startActionMode(new ModeCallback());
}
} else {
if (mActionMode != null) {
mActionMode.finish();
}
}
return false;
}
private final class ModeCallback implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.result_fragment_context, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
for (int i = 0; i < mResultExpandableAdapter.getGroupCount(); ++i) {
mExpandableListView.setItemChecked(i, false);
}
if (mode == mActionMode) {
mActionMode = null;
}
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
long[] selected = mExpandableListView.getCheckedItemIds();
if (selected.length > 0) {
for (long id: selected) {
// Do something with the selected item
}
}
mode.finish();
return true;
}
}
}