我有一个带有片段的viewpager,我想在点击我的微调器的项目后用新片段更新我的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);
ArrayAdapter<CharSequence> stringArrayAdapter = ArrayAdapter.createFromResource(this,
R.array.language, android.R.layout.simple_spinner_item);
stringArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(stringArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(spinner.getSelectedItem().toString().equals("Frensh"))
{
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
}
else if(spinner.getSelectedItem().toString().equals("English"))
{
pager.setAdapter(new MyPagerAdapter2(getSupportFragmentManager()));
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
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);
}
这是包含我的Frensh片段的类:
public static 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;
}
}
这是包含我的英文片段的类:
public static class MyPagerAdapter2 extends FragmentPagerAdapter {
public MyPagerAdapter2(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return En_FirstFragment.newInstance("Page : 1");
case 1: return En_SecondFragment.newInstance("Page : 2");
case 2: return En_ThirdFragment.newInstance("Page : 3");
case 3: return En_FourthFragment.newInstance("Page : 4");
default: return null;
}
}
@Override
public int getCount() {
return 4;
}
}
答案 0 :(得分:1)
除了一件事,你的整体代码似乎很好。您选择使用Fragment Pager Adapter并非用于此目的。如果要动态更新寻呼机和片段内的数据,则应使用FragmentStatePagerAdapter
,其余部分应相同。