gpt4 book ai didi

android - onContextItemSelected 为 fragment 调用了两次

转载 作者:太空狗 更新时间:2023-10-29 12:48:55 25 4
gpt4 key购买 nike

我的应用程序有一个 ListView,我正在为 SDK 11 以上的设备使用 Contextual Action Bar,为旧设备使用旧的弹出式上下文操作。我知道有一种方法可以将 CAB 与旧设备一起使用,但我尝试实现它,但发现对于最终会过时的设备来说,这样做是不值得的。我知道这是一些代码重复,但理论上,我将摆脱旧的弹出操作(强调“理论上”)。

无论如何,当我使用模拟器时,CAB 工作正常,但是当我在该事件中放置断点时,旧设备的旧弹出操作似乎会点击 onContextItemSelected 两次。我刚刚开始为我的应用程序实现一个 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;
}
}
}

最佳答案

这个问题的解决方案解决了我的问题:

How to handle onContextItemSelected in a multi fragment activity?

onContextItemSelected 中使用 getUserVisibleHint()

关于android - onContextItemSelected 为 fragment 调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14734586/

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