我有一个带有 actionbarsherlock TabsNavigation(3 个标签)的活动,当我按下一个标签时,我更改了相应片段的 viewpager 适配器。它工作正常,问题是当我单击除第一个创建的选项卡之外的另一个选项卡时,第一页始终是第一个选项卡中的一个。我试图在更改适配器之前放置 invalidate() ,但它不起作用。有人知道吗?这是代码:
public class Tabsteste2 extends SherlockFragmentActivity implements TabListener {
static AdapterOpiniao mOdapter;
static AdapterDados mDdapter;
static AdapterFoto mFdapter;
Bundle extras;
JSONParser jsonParser = new JSONParser();
SharedPreferences mPrefs;
static ViewPager mPager;
static int countopiniao;
static int countdados;
static int countfoto;
JSONArray perguntas = null;
PageIndicator mIndicator;
static ArrayList<HashMap<String, String>> opiniaolist;
static ArrayList<HashMap<String, String>> dadoslist;
static ArrayList<HashMap<String, String>> fotolist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabsteste2);
opiniaolist = new ArrayList<HashMap<String, String>>();
dadoslist = new ArrayList<HashMap<String, String>>();
fotolist = new ArrayList<HashMap<String, String>>();
mPager = (ViewPager)findViewById(R.id.pager);
extras = getIntent().getExtras();
Boolean opiniaoflag = extras.getBoolean("opiniaoflag");
Boolean dadosflag = extras.getBoolean("dadosflag");
Boolean fotoflag = extras.getBoolean("fotoflag");
countdados= extras.getInt("countdados");
countopiniao=extras.getInt("countopiniao");
countfoto=extras.getInt("countfoto");
mPrefs = getSharedPreferences("mPrefs1",MODE_PRIVATE);
Log.d("countdados",""+countdados);
Log.d("countfoto",""+countfoto);
Log.d("countopiniao",""+countopiniao);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
if(opiniaoflag==true){
ActionBar.Tab opiniaotab = getSupportActionBar().newTab();
opiniaotab.setText("Opinião");
opiniaotab.setTag("op");
opiniaotab.setTabListener(this);
mOdapter = new AdapterOpiniao(getSupportFragmentManager());
Log.d("Opiniao",""+opiniaotab.getTag());
getSupportActionBar().addTab(opiniaotab);
}if(dadosflag == true){
ActionBar.Tab dadostab = getSupportActionBar().newTab();
dadostab.setText("Dados");
dadostab.setTag("dd");
mDdapter = new AdapterDados(getSupportFragmentManager());
dadostab.setTabListener(this);
Log.d("Dados",""+dadostab.getTag());
getSupportActionBar().addTab(dadostab);
}
// mDdapter = new AdapterDados(getSupportFragmentManager());
if(fotoflag==true){
ActionBar.Tab fotostab = getSupportActionBar().newTab();
fotostab.setText("Fotos");
fotostab.setTag("ft");
mFdapter = new AdapterFoto(getSupportFragmentManager());
fotostab.setTabListener(this);
Log.d("Foto",""+fotostab.getTag());
getSupportActionBar().addTab(fotostab);
}
new getpergunta().execute();
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
if(tab.getTag().equals("op")){
mPager.invalidate();
mPager.setAdapter(mOdapter);
mIndicator = (UnderlinePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}else if (tab.getTag().equals("dd")){
mPager.invalidate();
mPager.setAdapter(mDdapter);
mIndicator = (UnderlinePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}else if(tab.getTag().equals("ft")){
mPager.invalidate();
mPager.setAdapter(mFdapter);
mIndicator = (UnderlinePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
public static class AdapterOpiniao extends FragmentPagerAdapter {
public AdapterOpiniao(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return countopiniao;
}
@Override
public Fragment getItem(int position) {
return FragmentOpinioes.newInstance(position);
}
}
public static class AdapterDados extends FragmentPagerAdapter {
public AdapterDados(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return countdados;
}
@Override
public Fragment getItem(int position) {
return FragmentDados.newInstance(position);
}
}
public static class AdapterFoto extends FragmentPagerAdapter {
public AdapterFoto(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return countfoto;
}
@Override
public Fragment getItem(int position) {
return FragmentFotos.newInstance(position);
}
}