选择Spinner项时,在viewpager中更新片段

时间:2015-04-25 01:24:18

标签: android android-fragments android-viewpager spinner

我的目标是创建一个简单的多语言应用程序,使用ViewPager来显示Fragments。我创建了:

- " Frensh_Frag1,Frensh_Frag2,Frensh_Frag3"作为法国碎片

- " English_Frag1,English_Frag3,English_Frag3"作为英文片段

我现在要做的是添加一个显示语言(fr,en)的微调器,当我选择" en" ViewPager应显示英文片段。 如何将微调器实现到我的代码中?! 到目前为止,这是我的代码:

public class MainActivity extends FragmentActivity {
private ViewPager pager;

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

     String[] language={"Fr", "En", "Sp"};
    ArrayAdapter<String> stringArrayAdapter=new ArrayAdapter<String>
    (this,android.R.layout.simple_spinner_dropdown_item,language);
    Spinner spinner = (Spinner)  findViewById(R.id.spinner);
    spinner.setAdapter(stringArrayAdapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
        });  

    pager = (ViewPager) findViewById(R.id.viewPager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            invalidateOptionsMenu();
        }
    });
}

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.activity_screen_slide, menu);

    menu.findItem(R.id.action_previous).setEnabled(pager.getCurrentItem() > 0);

    // Add either a "next" or "finish" button to the action bar, depending on which page
    // is currently selected.
    MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE,
            (pager.getCurrentItem() == pager.getAdapter().getCount() - 1)
                    ? R.string.action_finish
                    : R.string.action_next);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // Navigate "up" the demo structure to the launchpad activity.
            // See http://developer.android.com/design/patterns/navigation.html for more.
            NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
            return true;

        case R.id.action_previous:
            // Go to the previous step in the wizard. If there is no previous step,
            // setCurrentItem will do nothing.
            pager.setCurrentItem(pager.getCurrentItem() - 1);
            return true;

        case R.id.action_next:
            // Advance to the next step in the wizard. If there is no next step, setCurrentItem
            // will do nothing.
            pager.setCurrentItem(pager.getCurrentItem() + 1);
            return true;
    }

    return super.onOptionsItemSelected(item);
}

private class MyPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int pos) {
        switch(pos) {

        case 0: return Fr_FirstFragment.newInstance("Page : 1");
        case 1: return Fr_SecondFragment.newInstance("Page : 2");
        case 2: return Fr_ThirdFragment.newInstance("Page : 3");
        case 3: return Fr_FourthFragment.newInstance("Page : 4");
        default: return null;
        }
    }

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

0 个答案:

没有答案