菜单&使用ViewPager时,ActionBar图标在刷卡后响应错误的页面

时间:2013-01-17 12:22:17

标签: android eclipse android-actionbar android-viewpager

我是Android programming的新手,所以对我很轻松。 : - )

我有一个项目列表,当用户点击某个项目时,它会将他们带到具有该项目详细信息的屏幕。用户可以向右和向左滑动以查看列表中其他项目的详细信息 我有一些ActionBar图标,如分享,增加字体大小等等 我的问题是:如果我在列表中选择item1并转到具有该项目详细信息的屏幕,如果我滑动到item2然后单击共享,则共享item1的内容而不是item2。 增加或减少字体也会发生同样的事情。如果我选择item2然后滑动到item3并增加字体大小,则item2的字体大小增加而不是item3 我尝试了很多东西,比如使用SimpleOnPageChangeListener但没有用,我不知道如何正确实现它。

这是我的代码:

包含列表视图的主要活动:

    public class Main extends SherlockListActivity implements OnItemClickListener {

        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);  

            ListView mylist = (ListView) findViewById(android.R.id.list);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_reda_1, R.id.list_content, getResources().getStringArray(R.array.items_list_array) );
            mylist.setAdapter(adapter);

            mylist.setOnItemClickListener(new OnItemClickListener() 
            {
                public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3) 
                {
                    Intent n = null; 
                switch (position){
                    case 0: 
                        n = new Intent(getApplicationContext(), ViewPagerClass.class);
                        n.putExtra("POSITION_KEY", position);
                        break;
                    case 1: 
                        n = new Intent(getApplicationContext(), ViewPagerClass.class);
                        n.putExtra("POSITION_KEY", position);
                        break;
                    case 2: 
                        n = new Intent(getApplicationContext(), ViewPagerClass.class);
                        n.putExtra("POSITION_KEY", position);
                        break;
                    case 3: 
                        n = new Intent(getApplicationContext(), ViewPagerClass.class);
                        n.putExtra("POSITION_KEY", position);
                        break;
                    }

                    if(null!=n)
                        startActivity(n);
                }
            });     
        }
    }

ViewPagerClass:

    public class ViewPagerClass extends SherlockFragmentActivity{

        static final int NUM_ITEMS = 4;
        MyAdapter mAdapter;
        ViewPager mPager;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.setContentView(R.layout.viewpager_layout);

            mAdapter = new MyAdapter(getSupportFragmentManager());
            mPager = (ViewPager) findViewById(R.id.viewpager);
            mPager.setAdapter(mAdapter);
            //mPager.setCurrentItem(2);

                final ActionBar ab = getSupportActionBar();
                ab.setDisplayHomeAsUpEnabled(true);
                ab.setDisplayUseLogoEnabled(false);
                ab.setDisplayShowHomeEnabled(false);
    }

 public boolean  onCreateOptionsMenu(Menu menu) {

        mainMenu = menu;
        subMenu1 = menu.addSubMenu(0, 1, 7, "");
        subMenu2 = menu.addSubMenu(0, 2, 6, "");
        subMenu3 = menu.addSubMenu(0, 3, 5, "");

        //some code here...

        MenuItem share = menu.findItem(R.id.menu_share);
        ShareActionProvider provider = (ShareActionProvider) share.getActionProvider();
        provider.setShareHistoryFileName(null);
        provider.setShareIntent(createShareIntent());

        getSupportMenuInflater().inflate(R.menu.textsize_subm, subMenu3);
        MenuItem subMenu3Item = subMenu3.getItem();
        subMenu3Item.setIcon(R.drawable.ic_font_size);
        subMenu3Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        return true;


    }


    private Intent createShareIntent() {

        TextView tv = (TextView) findViewById(R.id.textView1);
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, tv.getText());
        shareIntent.setType("text/plain");      
        return shareIntent;
    }


           @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            //some code here...

            default:
                return super.onOptionsItemSelected(item);
        }
    }



    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {

            switch(position){
            case 0: return FirstPageFragment.newInstance();

            case 1: return SecondPageFragment.newInstance();

            case 2: return ThirdPageFragment.newInstance();

            case 3: return FourthPageFragment.newInstance();

            }
            return null;
        }
    }



    public static class FirstPageFragment extends Fragment {

        public static FirstPageFragment newInstance() {
            FirstPageFragment f = new FirstPageFragment();
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.fragment1, container, false);
            return V;

        }
    }

    public static class SecondPageFragment extends Fragment {

        public static SecondPageFragment newInstance() {
            SecondPageFragment f = new SecondPageFragment();
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.fragment2, container, false);
            return V;

        }
    }

    public static class ThirdPageFragment extends Fragment {

        public static ThirdPageFragment newInstance() {
            ThirdPageFragment f = new ThirdPageFragment();
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.fragment3, container, false);
            return V;

        }
    }


    public static class FourthPageFragment extends Fragment {

        public static FourthPageFragment newInstance() {
            FourthPageFragment f = new FourthPageFragment();
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.fragment4, container, false);
            return V;

        }
    }

简而言之:菜单/操作栏图标不会更新以在刷卡后响应当前页面。 我希望我的问题很明确。

1 个答案:

答案 0 :(得分:0)

我想通了,所以我想我会在这里发帖以防万一有人遇到同样的问题。 实际上解决方案非常简单,我不知道为什么我没有立刻解决它。

我使用'setOnPageChangeListener'&amp;把它放在'onCreate'

这是我的代码:

    int pageNum;
    myPager.setOnPageChangeListener(
                    new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {
                            //Here I put the code I want to call/update when the page changes (i.e swipe)
                                pageNum = myPager.getCurrentItem();

                            //update actionprovider & share intent after swiping
                                ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider();
                                mShareActionProvider.setShareIntent(createShareIntent());        
                            //I can also put a switch statement here, depending on which page. 
                                switch (pageNum) {
                                case 0: //.....