0

我是新手Android programming,所以请放轻松。:-)

我有一个项目列表,当用户单击一个项目时,它会将他们带到包含该项目详细信息的屏幕。用户可以左右滑动查看列表中其他项目的详细信息。
我有一些 ActionBar 图标,例如共享,增加字体大小...
等等共享而不是 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);
                }
            });     
        }
    }

ViewPager 类:

    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;

        }
    }

简而言之:滑动后菜单/操作栏图标不会更新以响应当前页面。我希望我的问题很清楚。

4

1 回答 1

0

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

我使用了“setOnPageChangeListener”并将其放在“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: //.....
于 2013-01-26T16:58:17.257 回答