我的目标是创建一个使用ViewPager显示片段的简单多语言应用程序。我创建了:
- " Frensh_Frag1,Frensh_Frag2,Frensh_Frag3"作为法国碎片
- " English_Frag1,English_Frag3,English_Frag3"作为英文片段
我现在要做的是添加一个显示语言(fr,en)的微调器,当我选择" en" ViewPager应显示英文片段。如何将微调器实现到我的代码中?!到目前为止,这是我的代码:
public class MainActivity extends FragmentActivity {
private ViewPager pager;
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.viewPager);
spinner = (Spinner) findViewById(R.id.spinner);
String[] language={"Fr", "En", "Sp"};
ArrayAdapter<String> stringArrayAdapter=new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_dropdown_item,language);
spinner.setAdapter(stringArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
if(spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString().equals("Fr"))
{
}
else if(spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString().equals("En"))
{
}
else if(spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString().equals("Sp"))
{
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
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);
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:
NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
return true;
case R.id.action_previous:
pager.setCurrentItem(pager.getCurrentItem() - 1);
return true;
case R.id.action_next:
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;
}
}
}
如果有人帮助我,我将不胜感激。现在已经超过一周,仍然没有结果