错误@Override片段中的方法被调用(Android)

时间:2013-12-18 19:52:18

标签: android android-fragments override android-arrayadapter android-contextmenu

我使用片段时遇到问题 首先是一些基本的信息。我正在使用带有3个Tabs / Fragments的Actionbar(support.v4)。前两个碎片会导致问题。第一个片段扩展了ListFragment,Second扩展了Fragment。两者都有ListView和一个ArrayAdapter(不同的ArrayAdapters)。

  1. My Fragments:

    在两个片段中,我创建了一个ContextMenu(不同的菜单/菜单项):

    在这两个片段中,我覆盖了onCreateContextMenu(..)onContextItemSelected(..)

  2. 我的适配器:

    两个Adapters都会覆盖getView方法。

  3. 我的问题:

    • 如果我选择片段2的ContextItem,则会调用图片1的onContextItemSelected方法。

    • 如果我更新片段2,也会调用片段1的getView。

  4. 在你的帮助和StackOverflow我已经发现,如果一个片段处于活动状态,则它旁边的两个片段已经处于活动状态。因此,如果两个片段具有相同的可覆盖方法,则将选择第一个。 问题是,我没有找到解决方案; - )

    有人在这里,谁理解我的问题和/或曾经处理过这个问题?

    编辑:源代码

    public class ActionBarActivity extends FragmentActivity implements
    ActionBar.TabListener {
    
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    
        setContentView(R.layout.actionbarlayout);
    
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    
        android.support.v4.app.FragmentManager      fm = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction  ft = fm.beginTransaction();
        android.support.v4.app.Fragment myFirstFragment     = (Fragment) new Fragment_ONE();
        android.support.v4.app.Fragment mySecondFragment    = (Fragment) new Fragment_TWO();
        android.support.v4.app.Fragment myThirdFragment     = (Fragment) new Fragment_THREE();
        ft.add(R.id.pager, myFirstFragment, "FIRSTFRAGTAG");
        ft.add(R.id.pager, mySecondFragment, "SECONDFRAGTAG");
        ft.add(R.id.pager, myThirdFragment, "THIRDFRAGTAG");
        ft.commit();
        fm.executePendingTransactions();
    
        public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            switch (position){
            case 0:
                Fragment myFirstFragment = new Fragment_ONE();
                return myFirstFragment;
            case 1:
                Fragment mySecondFragment = new Fragment_TWO();
                return mySecondFragment;
            case 2:
                Fragment myThirdFragment = new Fragment_THREE();
                return myThirdFragment;
            default: 
                Fragment defaultFragment = new Fragment_ONE();
                return defaultFragment;
    
            }
    
        }
        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                 return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
         }
    
    
    
    
        @Override
            public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
            mViewPager.setCurrentItem(tab.getPosition());
        }
    
    
    
    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }
    
    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }
    
    }
    
    
                public class FirstAdapter extends ArrayAdapter<xxx> {
    
                    public BroadcastReceiver receiver = new BroadcastReceiver() {
    
                        @Override
                        public void onReceive(Context context, Intent intent) {
                            //SOME CODE
                        }
                    };
                    public FirstAdapter(Context context, ArrayList<xxx> items) {
                        super(context, 0, items);
                        // SOME CODE
                    }
    
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        // SOME CODE: codeONE
                        return convertView;
                    }
                }
    
                public class SecondAdapter extends ArrayAdapter<xxx> {
    
                    public BroadcastReceiver receiver = new BroadcastReceiver() {
    
                        @Override
                        public void onReceive(Context context, Intent intent) {
                            //SOME CODE
                        }
                    };
                    public SecondAdapter(Context context, ArrayList<xxx> items) {
                        super(context, 0, items);
                        // SOME CODE
                    }
    
                public  class Fragment_ONE extends ListFragment{
    
                    public Fragment_ONE() {
                    }
    
                    @Override
                    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
                        View rootView = inflater.inflate(
                                R.layout.firstfragment, container, false);
                        setRetainInstance(true);
    
                        return rootView;
                    }
    
                    @Override
                    public void onActivityCreated (Bundle savedInstanceState) {
                        super.onActivityCreated(savedInstanceState);
                        mFirstListView =(ListView) getView().findViewById(android.R.id.list);
                        registerForContextMenu(mFirstListView);
    
                        mFirstAdapter = new FirstAdapter(myInstance, mItemsONE);
                        mFirstListView.setAdapter(mFirstAdapter);
    
                        @Override
                        public void onCreateContextMenu(ContextMenu menu, View v,
                            ContextMenuInfo menuInfo) {
    
                        super.onCreateContextMenu(menu, v, menuInfo);
    
                            if (v.getId() == android.R.id.list) {
                                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                                menu.setHeaderTitle("CONTEXTMENU");
                                menu.add(FIRST_FRAGMENT_GROUP_ID, 0, 0, "1");
                                menu.add(FIRST_FRAGMENT_GROUP_ID, 1, 1, "2");
                                menu.add(FIRST_FRAGMENT_GROUP_ID, 2, 2, "3");
                                menu.add(FIRST_FRAGMENT_GROUP_ID, 3, 3, "4");
                                menu.add(FIRST_FRAGMENT_GROUP_ID, 4, 4, "5");
                                menu.add(FIRST_FRAGMENT_GROUP_ID, 5, 5, "6");
                            }
                        }
    
                        @Override
                        public boolean onContextItemSelected(MenuItem item) {
    
                            if (item.getGroupId()==FIRST_FRAGMENT_GROUP_ID){
                            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                                    .getMenuInfo();
    
                            int ListItemIndex = info.position;
    
                            Intent broadcast = new Intent(UPDATEREQUEST);
                            broadcast.putExtra(UPDATEREQUESTEXTRA, 1);
                            ActionBarActivity.getMyInstance().getApplicationContext().sendBroadcast(broadcast);
                            return true;
                            }
                        }
    
                        @Override
                        public void onListItemClick(ListView l, View v, int position, long id) {
                        }
                }
    
                public  class Fragment_TWO extends Fragment {
                    public Fragment_TWO() {
                    }
    
                    @Override
                    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
                        View rootView = inflater.inflate(
                                R.layout.secondfragment, container, false);
                                setRetainInstance(true);
    
                        return rootView;
                    }
    
                    @Override
                    public void onActivityCreated (Bundle savedInstanceState) {
                        super.onActivityCreated(savedInstanceState);
    
                        mySecondListView = (ListView) getView().findViewById(R.id.secondListView);
    
                        mySecondAdapter = new SecondAdapter(myInstance, myItemsTWO);
                        mySecondListView.setAdapter(mySecondAdapter);
                        registerForContextMenu(mySecondListView);   
                    }
    
                    @Override
                    public void onCreateContextMenu(ContextMenu menu, View v,
                            ContextMenuInfo menuInfo) {
                        super.onCreateContextMenu(menu, v, menuInfo);
                        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                        menu.setHeaderTitle("Options");
                        menu.add(SECOND_FRAGMENT_GROUP_ID, 1, 1, "1");
                        menu.add(SECOND_FRAGMENT_GROUP_ID, 2, 2, "2");
                    }
    
                    @Override
                    public boolean onContextItemSelected(MenuItem item) {
                        super.onContextItemSelected(item);
                        // SOME CODE
                        Intent broadcast = new Intent(UPDATEREQUEST);
                        broadcast.putExtra(UPDATEREQUESTEXTRA, 1);
                        ActionBarActivity.getMyInstance().getApplicationContext().sendBroadcast(broadcast);
                        return true;
                    }
    
                }
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        // SOME CODE: codeTWO
                        return convertView;
                    }
                }
    

1 个答案:

答案 0 :(得分:1)

Android FragmentManager通过遍历所有片段来处理contextMenuItem选择的覆盖,因此处理上下文菜单点击的每个片段都需要具有处理调用或是否应将调用传递给下一个片段的逻辑。 / p>

看到这个答案: Wrong fragment in ViewPager receives onContextItemSelected call