我已经看了很多关于这个主题的帖子,但没有解决我的问题。我正在使用FragmentStatePagerAdapter和ViewPagers。问题是我的一个片段中有一些编辑文本和文本视图。我有正在更新的数据,但它们不会显示,除非你滚动几个标签然后回到它。我现在有点生病和沮丧,但我会发布代码,希望有人能看出我的意思。
公共类MainActivity扩展FragmentActivity实现TabListener {
ViewPager ViewPager;
@Override
protected void onCreate(Bundle arg0)
{
super.onCreate(arg0);
setContentView(R.layout.activity_main);
ViewPager = (ViewPager) findViewById(R.id.pager);
ViewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
ViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
//This didn't do a damn thing for me
ViewPager.invalidate();
ViewPager.getAdapter().notifyDataSetChanged();
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft)
{
// TODO Auto-generated method stub
ViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft)
{
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft)
{
// TODO Auto-generated method stub
}
}
类MyAdapter扩展了FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int arg0)
{
Fragment fragment=null;
if(arg0==0)
{
fragment=new someFragment();
}
if(arg0==1)
{
fragment=new anotherFragment();
}
//等等......
return fragment;
}
@Override
public int getCount()
{
return 5; // however many fragments
}
}