我的应用程序有一个ListView
,我正在Contextual Action Bar
为 SDK 11 以上的设备使用一个,旧设备的旧弹出上下文操作。我知道有一种方法可以将 CAB 与旧设备一起使用,但我尝试实施它,发现对于最终将过时的设备来说,这是不值得的。我知道这是一些代码重复,但从理论上讲,我将摆脱旧的弹出操作(强调“理论上”)。
onContextItemSelected
无论如何,当我使用模拟器时,CAB 工作正常,但是当我在该事件中设置断点时,旧设备的旧弹出操作似乎会命中两次。我刚刚开始ViewPager
为我的应用程序实现一个,这在之前没有发生,ViewPager
所以不确定这是否是导致问题的原因。
这是我正在使用的代码:
public class MyFragment extends SherlockListFragment
{
private ListView mListView;
private android.view.ActionMode mActionMode;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
if (this.mActionMode != null) return;
menu.add(1, 0, 0, "Delete");
menu.add(1, 1, 0, "Save");
}
@Override
public void onActivityCreated(final Bundle icicle)
{
mListView = getListView();
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB)
{
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onCreateActionMode(android.view.ActionMode mode, android.view.Menu menu) {
// Inflate the menu for the CAB
menu.clear();
menu.add(1, 1, 2, "Delete").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(1, 3, 1, "Save").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
@Override
public boolean onActionItemClicked(android.view.ActionMode mode, android.view.MenuItem item) {
mActionMode = mode;
if (item.getGroupId() == 1)
{
switch(itemId)
{
case 0:
DeleteItem();
break;
case 1:
SaveItem();
break;
}
}
}
}
}
}
@Override
public boolean onContextItemSelected(final android.view.MenuItem item) {
if (item.getGroupId() == 1) {
final AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
final Integer position = info.position;
final int itemId = item.getItemId();
switch(itemId)
{
case 0:
DeleteItem();
break;
case 1:
SaveItem();
break;
}
}
return super.onContextItemSelected(item);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu (menu);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
ViewPager 代码
public class Main extends SherlockFragmentActivity
{
private static List<Integer> mIds;
@Override
public void onCreate(final Bundle icicle)
{
super.onCreate(null);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager); //view pager exists, so we are using the portait layout
if (mViewPager != null)
{
mIds = new ArrayList<Integer>();
mIds.add(0);
mIds.add(1);
mIds.add(2);
}
else //in landscape
{
ListFragment lf = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentList);
if (lf == null)
lf = new ListFragment();
DetailFragment df = (DetailFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentDetail);
if (df == null)
{
df = new DetailFragment();
df.setArguments(getIntent().getExtras());
}
getSupportFragmentManager().beginTransaction().add(R.id.fragmentList, lf).commit();
getSupportFragmentManager().beginTransaction().add(R.id.fragmentDetail, df).commit();
}
}
private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
//can't use getSupportFragmentManager().findFragmentById() here because I get a "Cannot make a static reference to the non-static method" error
if (index == 0)
return ListFragment.newInstance();
else
return DetailFragment.newInstance(mIds.get(index-1));
}
@Override
public int getCount() {
return 4;
}
}
}