点击更改片段

时间:2015-01-26 09:18:10

标签: android android-fragments swipe android-fragmentactivity

我必须实现一个应用程序,用户可以在该应用程序中点击5页之间的bottom_bar(虽然谷歌不推荐在最近的文档中,但客户总是正确的)。 所以我用自动工具android学习创建了一个滑动活动。 问题:如何在单击按钮后开始转换,并更改片段??

以下是代码(使用滑动更改活动时)

public class Home extends ActionBarActivity {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ImageView home, ospitalita,multimedia,territoro, prodotti;

    ViewPager mViewPager;

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

        home = (ImageView) findViewById(R.id.ibHome);
        ospitalita = (ImageView) findViewById(R.id.ibOspitalita);
        multimedia = (ImageView) findViewById(R.id.ibMultimedia);
        territoro = (ImageView) findViewById(R.id.ibTerritorio);
        prodotti = (ImageView) findViewById(R.id.ibProdotti);

       mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        View.OnClickListener gestore = new View.OnClickListener() {
            @Override
            public void onClick(View v) {


//this is the bottom bar, on click, change icon color

               switch (v.getId()){
                   case R.id.ibHome:
                       changeBottomIcon("home");
                       mSectionsPagerAdapter.getItem(0);  //do nothing
                        break;

                   case R.id.ibOspitalita:
                       changeBottomIcon("ospitalita");
                       mSectionsPagerAdapter.getItem(1);
                       break;

                   case R.id.ibMultimedia:
                       changeBottomIcon("multimedia");
                       mSectionsPagerAdapter.getItem(2);
                       break;

                   case R.id.ibTerritorio:
                       changeBottomIcon("territorio");
                       mSectionsPagerAdapter.getItem(3);
                       break;

                   case R.id.ibProdotti:
                       changeBottomIcon("prodotti");
                       mSectionsPagerAdapter.getItem(4);
                       break;

                }
            }
        };

        home.setOnClickListener(gestore);
        ospitalita.setOnClickListener(gestore);
        multimedia.setOnClickListener(gestore);
        territoro.setOnClickListener(gestore);
        prodotti.setOnClickListener(gestore);

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }


    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.

            switch (position) {

// change bottom doesn't work here
                case 0:
                   // changeBottomIcon("home");  
                    return new fragment_home();
                case 1:
                    //changeBottomIcon("territorio");
                    return new fragment_territorio();
                case 2:
                  //changeBottomIcon("ospitalita");
                    return new fragment_ospitalita();
                case 3:
                //changeBottomIcon("multimedia");
                    return new fragment_multimedia();
                case 4:
                  //changeBottomIcon("prodotti");
                    return new fragment_prodotti();
            }
            return null;
        }

        @Override
        public int getCount() {
           //num pagine
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            //Not visible, no nav bar
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.home);
                case 1:
                    return getString(R.string.territorio);
                case 2:
                    return getString(R.string.ospitalita);
                case 3:
                    return getString(R.string.multimedia);
                case 4:
                    return getString(R.string.prodotti);
            }
            return null;
        }
    }

//simple method to change icon color
    public void changeBottomIcon(String tipo){
      .
      .
      .
//lo ometto perchè inutile

    }

}

1 个答案:

答案 0 :(得分:1)

您可以致电:

来实现这一目标
mViewPager.setCurrentItem(index);

(有关详细信息,请参阅android dev docs