Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:08:34+00:00 2026-06-09T13:08:34+00:00

i have an activity with actionbarsherlock TabsNavigation (3 tabs) and when I press a

  • 0

i have an activity with actionbarsherlock TabsNavigation (3 tabs) and when I press a tab i change the viewpager adapter of the corresponding fragment. it is working fine, the thing is that when I click on another tab besides the first created, the first page is always the one of that first tab. I tried to put invalidate() before changing the adapter, but it isnt working. anyone has any idea? here is the code:

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:08:36+00:00Added an answer on June 9, 2026 at 1:08 pm

    To solve your case, I used:

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction transaction) {   
        this.mPager.setCurrentItem(tab.getPosition());
    }
    

    When a click on a tab, the view of the corresponding tab is automatically changed.

    Edit: I must admit that I have some difficulties to really understand your code and what you are trying to do, so I add some more code to explain what i think you need.

    In my optinion, only one adapter is necessary for the ViewPager, and then if I’m right, you would do so:

    // I took some personal code for my example
    private ViewPager mPager;
    private PageIndicator mIndicator;
    private TabsExampleSectionsAdapter mAdapter;
    
    // Inside the onCreate method
    this.mPager = (ViewPager) findViewById(R.id.pager);
    this.mIndicator = new TabPageIndicator(this);
    this.mAdapter = new TabsExampleSectionsAdapter(this.getSupportFragmentManager());
    
    this.mPager.setAdapter(this.mAdapter);
    this.mIndicator.setViewPager(this.mPager);
    

    When everything is initialized, this is how to build tabs and pager views instructions (the two are related). Also, don’t mind the Section class, it’s a custom data model object which contains the tag data you need but it has nothing to do with actionbarsherlock.

    private void buildTabs(Section[] sections) {
    
        if (sections != null) {
    
            for (Section section : sections) {
    
                ActionBar.Tab sectionTab = getSupportActionBar().newTab();
                sectionTab.setText(section.name);
                sectionTab.setTabListener(this);
                getSupportActionBar().addTab(sectionTab);
    
                // The tag ("op" or "dd" in your case for example) is contained somewhere in the section object
                this.mAdapter.getSections().add(section);
            }
        }
    }
    

    And finally, this is the view pager adapter. It will choose what type of fragment to return following the tags you defined for each tab position:

    public class TabsExampleSectionsAdapter extends FragmentPagerAdapter {
    
        private ArrayList<Section> mSectionsList = new ArrayList<Section>();
    
        public TabsExampleSectionsAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }
    
        @Override
        public Fragment getItem(int position) {
    
                // Retrieving the cooresponding tag of position
                Section section = this.mSectionsList.get(position % getCount());
    
                // Here, you check the tag to know what type of fragment you must return
                if (section.getTag().equals("dd")) {
                    return FragmentDados.newInstance(position);
                } else if (section.getTag.equals("op")) {
                    return FragmentOp.newInstance(position);
                }
        }
    
        @Override
        public int getCount() {
            return this.mSectionsList.size();
        }
    
        @Override
            public CharSequence getPageTitle(int position) {
            return this.mSectionsList.get(position % getCount()).name.toUpperCase();
        }
    
        public ArrayList<Section> getSections() {
            return this.mSectionsList;
        }
    }
    

    In conclusion, when everything is set up, changing views doesn’t have to be done manually by changing adapters and calling invalidate(). You can return different type of fragments from your adapter with a simple condition. Then, by calling:

    this.mPager.setCurrentItem(position);
    

    it changes automatically the current view by passing in the adapter’s getItem(position) method. Basically, you just have to coordinate your tab positions and your tags in order to get the right type of fragment.

    Feel free to ask for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity that when users press share on a file it would
Its possible to retain a Fragment between Activities? Lets say I have Activity A
I have a one activity and one layout application. I am implementing ActionBarSherlock, but
I have activity with android:configChanges=orientation|keyboardHidden I am trying to change image in ImageView when
In my main activity, I have an ActionBar with navigation tabs and action items.
I have a activity displaying call logs in a ListView. The adapter used here
I have an activity using ActionBarSherlock with ActionBar.NAVIGATION_MODE_LIST . When entering the page I
I have an Android app where i'm using tabs (with ActionBarSherlock). So my main
I have Activity A with android:launchMode=singleTop in the manifest. If I go to Activity
I have Activity with ListView inside it and in the onCreate method of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.