3

I have created 6 fragments. First 3 fragments will load in viewPager at morning time and other 3 fragment will come in evening time. How to dynamically update the viewPager with the desired set of fragments according to time ? Here is my MainActivity code. I have implemented the first 3 fragments in the viewPager. But how to load the other 3 fragments in the viewPager ?

MainActivity:

mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); // adding bottom dots addBottomDots(0); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { MyFragmentInterface fragmentInterface = (MyFragmentInterface) mSectionsPagerAdapter.instantiateItem(mViewPager, position); if (fragmentInterface != null) { fragmentInterface.fragmentBecomeVisible(); Log.e("PageSelected", position + ""); } addBottomDots(position); } @Override public void onPageScrollStateChanged(int state) { } }); private class SectionsPagerAdapter extends FragmentStatePagerAdapter { SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). switch (position) { case 0: { return new Fragment1(); } case 1: { return new Fragment2(); } case 2: { return new Fragment3(); } } return null; } @Override public int getItemPosition(Object object) { return POSITION_NONE; } @Override public int getCount() { // Show 3 total pages. return totalTabs; } } 

How to load the other 3 fragments at evening time ?

UPDATE: Looking after the solutions one issue I think will arise. What if the user is using the application since morning. Will the fragments get updated at 12:05PM at realtime ?

4 Answers 4

3

You Can do something like this given that the next continuous number are the evening fragments.

switch (position) { case 0: { if (time != Calendar.AM){ return new Fragment4(); } else { return new Fragment1(); } } case 1: { if (time != Calendar.AM){ return new Fragment5(); } else { return new Fragment2(); } } case 2: { if (time != Calendar.AM){ return new Fragment3(); } else { return new Fragment6(); } } } 
Sign up to request clarification or add additional context in comments.

5 Comments

This one looks more neat. But what if the user is using the application since morning and at 12:01pm what will happen ? will the fragment change realtime ?
@SagarSuri it won't. You need a thread that actually that drastically will change the view if it's PM.
@SagarSuri If you get time just inside switch then it will detect and reflect when user change page. Or if user is stay on same page for that condition use alarm manager and in receiver of that try to exit application or just reload fragment.
can you provide some more detail on it. I am confused on how to reload the fragments using broadcast receiver.
@SagarSuri you might want to read the docs developer.android.com/reference/android/content/… or if you want some codes to follow through take a look at this, vogella.com/tutorials/AndroidBroadcastReceiver/article.html.
2

Update your code

switch (position) { case 0: { if(condition){ return new FragmentOther1(); } return new Fragment1(); } case 1: { if(condition){ return new FragmentOther2(); } return new Fragment2(); } case 2: { if(condition){ return new FragmentOther3(); } return new Fragment3(); } 

And calculate your EVENING condition as your time slot.

For Real time add Receiver

 private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(Intent.Intent.ACTION_TIME_TICK)){ //notify adapter data from here. and also check time logic. this method will call for every minute. } } }; 

In onCreate

IntentFilter filter = new IntentFilter(); filter.addAction(Intent.Intent.ACTION_TIME_TICK); registerReceiver(receiver, filter); 

and in onDestory

 if (receiver != null) { unregisterReceiver(receiver); receiver = null; } 

4 Comments

Even I feel the same. Will that be the efficient solution ?
This one looks more neat. But what if the user is using the application since morning and at 12:01pm what will happen ? will the fragment change realtime ?
For the real time implement BroadcastReceiver for Intent.ACTION_TIME_CHANGED and notifyDataSetChanged of adapter.
This one is neat and I have got a good idea related to your answer. Thanks a lot !!
1

add dynamic fragment count set

@Override public int getCount() { // check calendar session. if(morning == true) { // Show 3 total pages morning. return 3; }else{ // Show 3 + 3 6 total pages evening. return 6; } } 

then add fragments

@Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). switch (position) { case 0: { return new Fragment1 (); } case 1: { return new Fragment2 (); } case 2: { return new Fragment3 (); } case 3: { return new Fragment4 (); } case 4: { return new Fragment5 (); } case 5: { return new Fragment6 (); } } return null; } 

Comments

1

Why you have to make it so complicated? Create 2 lists: a list for day and a list for night. Check the time and setAdapter or notifyDataSetChanged() base on your condition

private class SectionsPagerAdapter extends FragmentStatePagerAdapter { private List<Fragment> fragmentList; public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) { super(fm); this.fragmentList = fragmentList; } SectionsPagerAdapter(FragmentManager fm) { super(fm); } public void setFragmentList(List<Fragment> fragmentList) { this.fragmentList = fragmentList; notifyDataSetChanged(); } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getItemPosition(Object object) { return POSITION_NONE; } @Override public int getCount() { // Show 3 total pages. return fragmentList.size(); } } 

Here is how to implement that

 List<Fragment> mDayFragments = new ArrayList<>(); mDayFragments.add(Fragment1); mDayFragments.add(Fragment2); mDayFragments.add(Fragment3); List<Fragment> mNightFragments = new ArrayList<>(); mNightFragments.add(Fragment4); mNightFragments.add(Fragment5); mNightFragments.add(Fragment6); SectionsPagerAdapter mAdapter; if (yourCondition) { mAdapter = new SectionsPagerAdapter(mDayAdapter); } else { mAdapter = new SectionsPagerAdapter(mNightAdapter); } yourViewPager.setAdapter(mAdapter); // Time moving... The night comes... mDayAdapter.setFragmentList(mNightFragments); 

3 Comments

mDayAdapter.setFragmentList(mNightFragments); I didnt understand this line
I create a function in SectionPagerAdapter named setFragmentList to asign List<Fragment> with new value and notify adapter to re-update view inside ViewPager. @SagarSuri
Great Answer bro. But for some reasons i think broadcast Receiver works well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.