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);
}
}
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.
Related
I have an activity that hosts tablayout with viewpager and fragments. I have successfully setup the tablayout and viewpager with user-populated values from a local database like below code(trying to be brief, showing only one tab here) -
public void setUpTabs() {
setUpViewPager(viewPager);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setupWithViewPager(viewPager);
}
private void setUpViewPager(ViewPager viewPager) {
Customize customize = new Select()
.from(Customize.class)
.where("tabName = ? ", Constants.NATURE)
.executeSingle();
if (customize != null) {
String nature = customize.tabName;
Log.d(TAG, "Nature Tab Name:\t" + nature);
}
pagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
pagerAdapter.addTab(new Top30Fragment(), Constants.Top30); // default tab
if (nature != null) {
pagerAdapter.addTab(new NatureFragment(), Constants.NATURE); // dynamic tab from db
}
pagerAdapter.addTabAtLastPosition(new PrefsFragment(), Constants.PREFS);
viewPager.setAdapter(pagerAdapter);
}
Customize is model class, I have used ActiveAndroid library for persistence. Here's my tabsadapter class code:
public class TabsPagerAdapter extends FragmentPagerAdapter {
private static final String TAG = TabsPagerAdapter.class.getSimpleName();
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> titlesList = new ArrayList<>();
private ViewPager viewPager;
private TabLayout tabLayout;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
public void addTab(Fragment fragment, String name){
fragmentList.add(fragment);
titlesList.add(name);
}
public void addTabAtLastPosition(Fragment fragment, String name){
fragmentList.add(fragmentList.size(), fragment);
titlesList.add(name);
}
public void removeTab(Fragment fragment, String name){ //Not working
for (int i = 0; i < fragmentList.size(); i++){
if (fragmentList.get(i).equals(fragment)){ // never enters/returns from this loop
Log.d(TAG, "Fragment at position:\t" + i);
fragmentList.remove(fragment);
titlesList.remove(name);
}
}
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return titlesList.get(position);
}
}
In my preferences fragment(also part of the tablayout), I have a recyclerview with checkboxes, case1:if checked, the tab is checked in db and only added to tablayout if not already there. case2:Otherwise if unchecked, means the tab is in layout and db and has to be removed from both.
I have started working on case 2 but I'm only able to delete values from db, the tab to be deleted is still present in tablayout. Here's my recyclerview adapter code to delete the tab:
public class PrefsAdapter extends RecyclerView.Adapter<PrefsCustomizeViewHolder> {
private static final String TAG = PrefsAdapter.class.getSimpleName();
private final Context context;
private List<Customize> itemsList;
private long id_nature, id_space, id_seasons, id_art, id_scifi, id_misc;
private Customize cust; // Model class for saving and retrieving tabs
private TabsPagerAdapter pagerAdapter;
private String nature, space, seasons, art, scifi, misc;
private String tabNature, tabSpace, tabSeasons, tabArt, tabScifi, tabMisc;
public PrefsAdapter(Context context, List<Customize> itemsList) {
this.context = context;
this.itemsList = itemsList;
pagerAdapter = new TabsPagerAdapter(((AppCompatActivity)context).getSupportFragmentManager());
}
#Override
public PrefsCustomizeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.prefs_customize_items_layout, parent, false);
return new PrefsCustomizeViewHolder(view);
}
#Override
public void onBindViewHolder(final PrefsCustomizeViewHolder viewholder, final int position) {
final Customize customize = itemsList.get(position);
Picasso.with(context)
.load(customize.getIcon())
.fit()
.into(viewholder.prefsSelImg);
// search db to find if checkbox is checked, i.e tab is present or not and update recyclerview
switch (position) {
case 0:
cust = new Select()
.from(Customize.class)
.where("tabName = ? ", Constants.SPACE)
.executeSingle();
if (cust != null) {
space = cust.tabName;
Log.d(TAG, "Space Tab From DB:\t" + space);
id_space = cust.getId();
Log.d(TAG, "Space Tab Id from DB:\t" + id_space);
viewholder.prefsCheckBox.setChecked(true);
} else {
viewholder.prefsCheckBox.setVisibility(View.INVISIBLE);
viewholder.prefsCheckBox.setChecked(false);
}
break;
}
viewholder.prefsSelImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (position) {
case 0:
if (viewholder.prefsCheckBox.isChecked()){
viewholder.prefsCheckBox.setChecked(false);
Customize.delete(Customize.class, id_space);
Log.d(TAG, "Deleted Space id:\t" + id_space); // prints right log detail
pagerAdapter.removeTab(new SpaceFragment(), Constants.SPACE);
} else {
viewholder.prefsCheckBox.setChecked(true);
customize.setTabId(UUID.randomUUID().toString());
customize.setTabName(Constants.NATURE);
customize.save();
pagerAdapter.addTab(new SpaceFragment(), Constants.SPACE);
viewholder.prefsCheckBox.setVisibility(View.VISIBLE);
}
break;
}
}
});
}
#Override
public int getItemCount() {
if (itemsList == null) {
return 0;
}
return itemsList.size();
}
}
The tabspagerAdapter.removeTab() never gets called even though I have initialized it in the constructor like below:
public PrefsAdapter(Context context, List<Customize> itemsList) {
this.context = context;
this.itemsList = itemsList;
pagerAdapter = new TabsPagerAdapter(((AppCompatActivity)context).getSupportFragmentManager());
}
Can someone help me out with removing the fragment and also why the pagerAdapter.removeTab(fragment, title) method never gets called and how to update the tab layout. Thanks.
I have used the following code to successfully change the number of tabs (both add and remove). I found I had to remove them from both the pager adapter and the tab layout.
int cnp = mPagerAdapter.getCount();
// remove all tabs (or just the ones you need to remove)
// from both the pager adapter and the tab layout
for(int i = cnp-1; i >= 0; --i) {
mPagerAdapter.destroyItem(mViewPager, i, mPagerAdapter.getItem(i));
mTabLayout.removeTabAt(i);
}
// add back in whatever tabs you need and update the pager adapter to be consistent
mTabLayout.addTab(mTabLayout.newTab().setText("Tab 0"),0);
mTabLayout.addTab(mTabLayout.newTab().setText("Tab 1"),1);
mPagerAdapter.setConfiguration(2); // my custom method to tell it which pages to show
mTabLayout.invalidate();
mPagerAdapter.notifyDataSetChanged();
Relevant class members:
ViewPager mViewPager;
TabLayout mTabLayout;
MyPagerAdapter mPagerAdapter; // extends FragmentPagerAdapter
In my project I have a tab layout and a fragments viewpager. When I load the fragments and tabs the tab index and the displayed fragment is out of sync, and also when I swipe back and forth the fragments are not in sync with the tabs. I checked the getItem(int position) and the getTitle(int position) methods both have the same position number and the corresponding entries in the ArrayList<Fragments> and the ArrayList<String> (for tab headings) are correct but some how the tabs display is out of sync with the view pager.
Here is my fragmentPagerAdapter.java
public class DetailsFragmentPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments;
private ArrayList<String> titles;
public DetailsFragmentPagerAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<>();
titles = new ArrayList<>();
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return titles.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
public void setFragment(Fragment fragment, String title) {
fragments.add(fragment);
titles.add(title);
notifyDataSetChanged();
}
}
this is activity on create.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
tabLayout = (TabLayout) findViewById(R.id.detailsTabs);
viewPager = (ViewPager) findViewById(R.id.viewPager);
new GetCategoriesAsync2().execute();
}
the async task's doInBackground is calling is preparing arraylist
and the onPostExecute
#Override
protected void onPostExecute(Void aVoid) {
int size = lists.size();
ArrayList<Fragment> fragments = new ArrayList<>(size);
ArrayList<String> titles = new ArrayList<>(size);
for (int i = 0; i < lists.size(); i++) {
ArrayList<String> x = lists.get(i);
titles.add(x.remove(0));
fragments.add(DetailsListFragment.newInstance(x));
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentPagerAdapter = new DetailsFragmentPagerAdapter(fragmentManager);
fragmentPagerAdapter.setFragments(fragments,titles);
viewPager.setAdapter(fragmentPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
progressDialog.dismiss();
}
I am using design support library 23.0.1 and the build target is 23.
how can I fix this tab layout out of sync?
if you use a TabLayout, you need to connect it to the adapter
If you're using a ViewPager together with this layout, you can use
setTabsFromPagerAdapter(PagerAdapter) which will populate the tabs
using the given PagerAdapter's page titles. You should also use a
TabLayout.TabLayoutOnPageChangeListener to forward the scroll and
selection changes to this layout like so:
ViewPager viewPager = ...;
TabLayout tabLayout = ...;
viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(tabLayout));
Try this, it worked for me
public class DetailsFragmentPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragments= new ArrayList<Fragment>();
private final List<String> titles= new ArrayList<String>();
public DetailsFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
public void setFragment(Fragment fragment, String title) {
fragments.add(fragment);
titles.add(title);
}
}
And Change
fragmentPagerAdapter.setFragments(fragments,titles); To
for (int i=0;i<fragments.size();i++)
{
adapter.setFragments(fragments.get(i),titles.get(i));
}
Check this out
it's a demo app that uses a TabLayout with a ViewPager
https://github.com/chrisbanes/cheesesquare
and if you don't know the number of the fragments that you will have in the ViewPager
your pageadapter should extends FragmentStatePagerAdapter
https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html
I am trying to implement a Gallery with a FragmentPagerAdapter on a ViewPager. Each fragment has a "delete" button which deletes the file.
Then I am trying to remove this fragment and scroll to next fragment when a callback of a AsyncTask is called.
In summary, when the delete button is touched first, delete the file in a remote FTP server and then remove the local file.
This is the Activity container of the ViewPager
public class MediaViewerActivity extends FragmentActivity implements OnFTPResult{
(...)
protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> multimedia;
viewPager = (ViewPager) findViewById(R.id.pager);
multimedia = getFiles();
MediaViewerAdapter adapter;
adapter = new MediaViewerAdapter(getSupportFragmentManager(), MediaViewerActivity.this, multimedia);
viewPager.setOffscreenPageLimit(2);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(position);
(...)
}
#Override
public void onActionPerformed(String result) {
switch (result) {
case "Done delete":
//remove the fragment and local file
break;
}
}
}
This is the code of the adapter for the ViewPager
public class MediaViewerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener{
(...)
public MediaViewerAdapter(FragmentManager fm, Context _ctx,ArrayList<String> imagePaths) {
super(fm);
manager = fm;
_imagePaths = imagePaths;
ctx = _ctx;
}
}
(...)
#Override
public Fragment getItem(int position) {
Fragment fragment;
String[] fileExtensionCaching = _imagePaths.get(position).split("\\.");
String currentExtension = fileExtensionCaching[fileExtensionCaching.length - 1];
Bundle args = new Bundle();
args.putString("path", _imagePaths.get(position));
if((currentExtension.equals("pdf")))
fragment = FragmentPDFViewer.newInstance(args);
else
if((currentExtension.equals("jpg") || currentExtension.equals("png") || currentExtension.equals("gif"))) {
fragment = FragmentImageViewer.newInstance(args);
}
else{
fragment = FragmentVideoPlayer.newInstance(args);
}
return fragment;
}
#Override
public int getCount() {
return _imagePaths.size();
}
(...)
}
And this is a slice of the AsyncTask class
#Override
protected void onPostExecute(String response) {
((OnFTPResult)ctx).onActionPerformed(response);
}
Which would be the best safety way to remove the current Fragment and then scroll to next (If there is)?
Thanks
I'm trying to start an animation when the fragment is showed on screen(I'm working with a ViewPager).
This a part of my code
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
private Map<Integer, String> mFragmentTags;
private FragmentManager mFragmentManager;
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
mFragmentTags = new HashMap<Integer, String>();
}
public ScreenSlidePageFragment getFragment(int position) {
String tag = mFragmentTags.get(position);
if (tag == null)
return null;
return (ScreenSlidePageFragment) mFragmentManager.findFragmentByTag(tag);
}
#Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Object obj = super.instantiateItem(container, position);
if (obj instanceof Fragment) {
// record the fragment tag here.
Fragment f = (Fragment) obj;
String tag = f.getTag();
mFragmentTags.put(position, tag);
System.out.println("holi");
}
return obj;
}
}
public class ScreenSlideActivity extends FragmentActivity {
(...)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
(...)
SimpleOnPageChangeListener mPageChangelistener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When changing pages, reset the action bar actions since they are dependent
// on which page is currently active. An alternative approach is to have each
// fragment expose actions itself (rather than the activity exposing actions),
// but for simplicity, the activity provides the actions in this sample.
ScreenSlidePageFragment currentFgm = mPagerAdapter.getFragment(position);
Animation animacion = AnimationUtils.loadAnimation(ScreenSlideActivity.this, R.anim.glasses);
currentFgm.getView().findViewById(R.id.image).startAnimation(animacion);
invalidateOptionsMenu();
}
(...)
The problem is when onPageSelected is called returns a NullPointer Exception in
currentFgm.getView().findViewById(R.id.image).startAnimation(animacion);
How could I start the animation when the fragment is showed on screen?
If you are trying to add animation to the ViewPager then you can implement the ViewPager.PageTransformer interface and supply it to the view pager to display a different animation than the default screen slide animation.
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
...
mPager.setPageTransformer(true, new CustomTransformer());
You will find here two examples of PagerTransformers.
I have implemented the ViewPagerIndicator with ActionBarSherlock.
Now i would like to open a specific fragment inside the viewpager once the viewpager is created. Normally you should use setCurrentItem(pageid) or setViewPager(mPager, pageid);
But this doesn't work :( If I would like to open fragment 2 inside the viewpager, the
viewpager indicator shows that fragment 2 is shown, but in reality fragment one (standard) is shown in the viewpager.
I have the following code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);
mAdapter = new ColofonFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
String page = "";
page = getIntent().getStringExtra("page");
int pageid = 0;
if(page.equals("contact")) { pageid = 0; }
else if(page.equals("colofon")) { pageid = 1; }
else if(page.equals("disclaimer")) { pageid = 2; }
mIndicator.setViewPager(mPager, pageid); // Doesn't work...
//mIndicator.setCurrentItem(pageid); // Doesn't work...
}
class ColofonFragmentAdapter extends FragmentPagerAdapter implements TitleProvider {
ArrayList<Fragment> fragments = new ArrayList<Fragment>();
ArrayList<String> titels = new ArrayList<String>();
public ColofonFragmentAdapter(FragmentManager fm) {
super(fm);
fragments.add(new LayoutFragment(R.layout.colofon_contactgegevens));
titels.add("Contact");
fragments.add(new LayoutFragment(R.layout.colofon_colofon));
titels.add("Colofon");
fragments.add(new LayoutFragment(R.layout.colofon_disclaimer));
titels.add("Disclaimer");
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public String getTitle(int position) {
return titels.get(position);
}
}
Can you tell me which method I should use to change the initial page?
Thank you!
I think it was a bug caused by setCurrentItem()
https://github.com/JakeWharton/Android-ViewPagerIndicator/pull/76
It's solved ^^