FragmentPagerAdapter Fragment getItem wrong position? - java

So for some reason i'm getting a wrong position when i switch tabs on my tabbed activity.
At the bottom of my post you can see my logcat when i start my app, tab 1 and tab 2 their oncreateview is called.
And position 0 switches to 1 even though i'm on tab 1 which must be position 0.
So my problem is that when i change a tab, the wrong data is shown and wrong logcat info.
for example i change to tab 2, logcat shows oncreateview i called from tab3 ?
How can i fix this?
Thanks,
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int numOfTabs;
SectionsPagerAdapter(FragmentManager fm, int numOfTabs) {
super(fm);
this.numOfTabs = numOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Log.e(TAG, "Pos: "+ position);
return new Tab1();
case 1:
Log.e(TAG, "Pos: "+ position);
return new Tab2();
case 2:
Log.e(TAG, "Pos: "+ position);
return new Tab3();
case 3:
Log.e(TAG, "Pos: "+ position);
return new Tab4();
case 4:
Log.e(TAG, "Pos: "+ position);
return new Tab5();
default:
return null;
}
}
#Override
public int getCount() {
// Get total pages
return numOfTabs;
}
}
Logcat
.activities.MainActivity: Pos: 0
.activities.MainActivity: Pos: 1
.fragments.Tab1: onCreateView called!
.fragments.Tab2: onCreateView called!

ViewPager by default loads 2 tabs. So getItem gets called twice when you first open the app. When you go to tab2, ViewPager loads tab3 in advance so that the scroll will be smooth. See offscreenPageLimit.

Related

How to refresh Fragment and pass info based on its position?

I`ve got an activity with ViewPager. There is 1 Fragment class - ScheduleFragment and a BroadcastReceiver in it. What I need is to get info and fill up the list in the fragment, based on its position.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ScheduleFragment scheduleFragment = new ScheduleFragment();
return scheduleFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 5 total pages.
return 5;
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.descr_monday);
case 1:
return getString(R.string.descr_tuesday);
case 2:
return getString(R.string.descr_wednesday);
case 3:
return getString(R.string.descr_thursday);
case 4:
return getString(R.string.descr_friday);
}
return null;
}
And here the ScheduleFragment`s methods:
public ScheduleFragment newInstance(Integer day) {
ScheduleFragment fragment = new ScheduleFragment();
Bundle args = new Bundle();
args.putInt("day", day);
fragment.setArguments(args);
return fragment;
}
private class ScheduleBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ArrayList<Lesson> result = (ArrayList<Lesson>) intent.getSerializableExtra("schedule");
ArrayList<Lesson> dataToInsert = new ArrayList<>();
Log.i("ASSSSSSSSSSSS", day.toString());
for (Lesson lesson : result) {
if (Objects.equals(lesson.getDay(), day)) {
dataToInsert.add(lesson);
}
}
adapter = new ScheduleListAdapter(context, result);
SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(adapter);
swingBottomInAnimationAdapter.setAbsListView(schedule_listView);
schedule_listView.setAdapter(swingBottomInAnimationAdapter);
adapter.notifyDataSetChanged();
}
}
I also have a Spinner in Toolbar with weeks. So also I want to update the fragment when I pick another week.
Please, guys. Help me. I`ve tried some solutions, but none of them helped me!
Hope for you assistance
I'm probably not an expert on this, but I just completed a project full of fragments. The 'dirty' way I passed data between them was accomplished by using a singleton class in my project. This probably isn't the best solution, though.
For instance, my project included a fragment which I updated based on options in a settings activity. The FamilyMap (familyMap) class is my singleton, and I kept a settings model containing maps of needing information within that object. The code shown here is updating that settings model based on user interaction.
public void mapTypeSelection(String selection)
{
Log.d("Map Selection", selection);
switch(selection)
{
case "Normal":
familyMap.getMapFrag().getmMap().setMapType(GoogleMap.MAP_TYPE_NORMAL);
familyMap.getSettings().setMapType(new Pair<String, Integer>("Normal", GoogleMap.MAP_TYPE_NORMAL));
break;
case "Satellite":
familyMap.getMapFrag().getmMap().setMapType(GoogleMap.MAP_TYPE_SATELLITE);
familyMap.getSettings().setMapType(new Pair<String, Integer>("Satellite", GoogleMap.MAP_TYPE_SATELLITE));
break;
case "Hybrid":
familyMap.getMapFrag().getmMap().setMapType(GoogleMap.MAP_TYPE_HYBRID);
familyMap.getSettings().setMapType(new Pair<String, Integer>("Hybrid", GoogleMap.MAP_TYPE_HYBRID));
break;
case "Terrain":
familyMap.getMapFrag().getmMap().setMapType(GoogleMap.MAP_TYPE_TERRAIN);
familyMap.getSettings().setMapType(new Pair<String, Integer>("Terrain", GoogleMap.MAP_TYPE_TERRAIN));
break;
default:
break;
}
}
//private method of your class
private int getIndex(Spinner spinner, String myString)
{
int index = 0;
for (int i=0;i<spinner.getCount();i++){
if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(myString)){
index = i;
break;
}
}
return index;
}
public Pair<String, Integer> colorLineSelection(String selection)
{
Log.d("Map Selection", selection);
switch(selection)
{
case "Red":
return new Pair<String, Integer>("Red", Color.RED);
case "Blue":
return new Pair<String, Integer>("Blue", Color.BLUE);
case "Green":
return new Pair<String, Integer>("Green", Color.GREEN);
default:
return null;
}
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.getId()){
case R.id.mapTypeSpinner:
mapTypeSelection((String)parent.getItemAtPosition(position));
break;
case R.id.lifeLineSpinner:
FamilyMap.getInstance().getSettings().setLifeColor(colorLineSelection((String)parent.getItemAtPosition(position)));
familyMap.getMapFrag().drawLines();
break;
case R.id.spouseLineSpinner:
FamilyMap.getInstance().getSettings().setSpouseColor(colorLineSelection((String) parent.getItemAtPosition(position)));
familyMap.getMapFrag().drawLines();
break;
case R.id.familyLineSpinner:
FamilyMap.getInstance().getSettings().setFamilyColor(colorLineSelection((String) parent.getItemAtPosition(position)));
familyMap.getMapFrag().drawLines();
break;
default:
break;
}
}
The other thing I did was I retained the instance of my fragment - it was the main portion of my app, so it made sense for my situation. That allowed me to be able to update the original fragment when an option was selected. I also set the fragment to update when a certain result was reached by using startActivityForResult().
I think a better (cleaner, more acceptable) option for sending the data to your fragment would be object serialization, but at least in my case, that proved to be more work than I could afford at the time.
Hopefully this at least helps to point you in the right direction!
So, after trying, trying and a lot of trying I figured it out!
In newInstance method, where I create new instance of Fragment I`ve added a bundle with day, according to fragments position
public ScheduleFragment newInstance(Integer day) {
ScheduleFragment fragment = new ScheduleFragment();
Bundle args = new Bundle();
args.putInt("day", day);
fragment.setArguments(args);
return fragment;
}
Then, in fragment's onCreateView I get this argument with getArguments().getInt("day"); and put it into private Integer dayglob; field, which means, that its different for every fragment.
Now, when I have 'day' variable in my fragment I can put info into list according to provided day:
private class ScheduleBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ArrayList<Lesson> result = (ArrayList<Lesson>) intent.getSerializableExtra("schedule");
ArrayList<Lesson> dataToInsert = new ArrayList<>();
for (Lesson lesson : result) {
if (Objects.equals(lesson.getDay(), dayglob)) {
dataToInsert.add(lesson);
}
}
adapter = new ScheduleListAdapter(context, dataToInsert);
SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(adapter);
swingBottomInAnimationAdapter.setAbsListView(schedule_listView);
schedule_listView.setAdapter(swingBottomInAnimationAdapter);
try {
getActivity().unregisterReceiver(mScheduleBroadcastReceiver);
} catch (IllegalArgumentException e) {
Log.i("Unregister", e.toString());
}
}
}
Every fragment got its own "onReceive" and wor on data from service for specific fragment.
Also, about the week spinner and updating the lists according to its value: I've made a static field public static Spinner week_spinner;, initialized it it onCreate method of my Activity and then, in fragment's "onCreateView" I made a request to my database like this:
requestQueue.add(connectionManager.getSchedule(this, savedUser.getGroup(), MainActivity.week_spinner.getSelectedItemPosition() + 1));
The only matter thing here is getting value by accessing spinner via static reference.
And, also, I've set Listener which says to fragments something like
Hey, guys! You are not up to date! Update yourself!
week_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mViewPager.getAdapter().notifyDataSetChanged();
}
});
And a little trick which make it possible is to change getItemPositionin FragmentPagerAdapter class (SectionsPagerAdapter in my case)
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
It`s kinda tricky, I guess, but its the only way i can figure it out
I hope, this will help someone!

Android ViewPager: Destroy Fragment on Slide?

I am basically playing an animation on each fragment of the view pager. The animation plays when the user slides to the specific fragment. However, certain fragments don't play the animation the second time I visit them. That's because the view pager keeps them in memory.
I need to destroy each fragment after the user slides to another fragment. This way, the animations play every time I revisit those fragments.
Main View:
pager = (ViewPager) findViewById(R.id.guidepager);
mAdapter = new NewUserGuideAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
pager.setOffscreenPageLimit(0); //Tried this too. Didnt work
Fragment:
public class NewUserPage_Two extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_new_user_page__two, container, false);
//Play animation, etc
Animation animation_1 = AnimationUtils.loadAnimation(NewUserPage_Two.this.getActivity(), R.anim.abc_slide_in_bottom);
person1.setAnimation(animation_1);
return rootView;
}
Adapter:
public class NewUserGuideAdapter extends FragmentPagerAdapter {
public NewUserGuideAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new NewUserPage_One();
case 1:
return new NewUserPage_Two();
case 2:
return new NewUserPage_Three();
case 3:
return new NewUserPage_One();
case 4:
return new NewUserPage_One();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 5;
}
}
How can I amend my code guys?
ViewPager provide a method mViewPager.setOffscreenPageLimit(0);
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.
Try this inside the fragment:
((BaseAdapter) *YourContainer*.getAdapter()).notifyDataSetChanged();
You can refer to: Refresh Current Fragment (ListView Data) remaining in the same activity
public void setOffscreenPageLimit(int limit) {
if (limit < DEFAULT_OFFSCREEN_PAGES) {
Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " +
DEFAULT_OFFSCREEN_PAGES);
limit = DEFAULT_OFFSCREEN_PAGES;
}
if (limit != mOffscreenPageLimit) {
mOffscreenPageLimit = limit;
populate();
}
}
That's the method body,DEFAULT_OFFSCREEN_PAGES=1by the way.I think google add this limits cause you need at least the 2 views between current item while you are sliding.
You can try using addOnPageChangeListener() and start your animation on onPageSelected.

How to update fragments under navigation tabs

I have four tabs in navigation tab, and each tab has a fragment:
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 PostJob();
case 1:{
return new PostedJobHistory();
}
case 2:{
return new SearchCandidates();
}
case 3:{
return new EmployerSettings();
}
default:
break;
}
return PlaceholderFragment.newInstance(position + 1);
}
I enter job details in first tab - PostJob fragment. and want to get it updated in the second tab when I go to Posted Job history fragment.
How can I achieve this.
In tab 1 I enter details in database
and in tab 2 i read database to show posted job history, but posted job history is not updated.
use this
List<Fragment> fragmentsList = getSupportFragmentManager().getFragments();
if (fragmentsList == null || fragmentsList.size() == 0) {
return;
}
for (Fragment fragment : fragmentsList) {
if (fragment instanceof PostedJobHistory) {
((PostedJobHistory) fragment).yourUpdateMethod();
}
}

ViewPager only displays 2 out of 6 child Fragments using FragmentPagerAdapter

My app uses 2 identical ViewPagers that contain 6 images in each of their 6 Fragments.
The leftViewPager's OnPageChangeListener swipe is used to control the rightViewPager.
As I swipe through the 6 leftViewpager fragments the rightViewpager just rotate between the first 2 Fragments back and forth!
I understand this is due to Android loading the current and next Fragment in the viewPager, but when I swipe the rightViewpager manually (Just to check) all 6 Fragments are there!!
So the Question: How can I get all 6 Fragments in Vp2 by swiping Vp1?? As the Hierachy Viewer shows just 2 Fragments. Or Do I need some special trickery to re-use/recycle the two fragments?
Click link to animated gif illustrating issue
// Set View pager on page changed listner
first_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
//second_pager.setCurrentItem(position); //Jerky
Toast.makeText(MainActivity.this,
"Selected page position: " + position, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageScrolled(int position , float positionOffset , int positionOffsetPixels) {
if(positionOffsetPixels>0)
{
second_pager.scrollTo(positionOffsetPixels-second_width ,0); // 2/3 vers
//second_pager.scrollTo(positionOffsetPixels-second_width + positionOffsetPixels ,0);
}
}
#Override
public void onPageScrollStateChanged(int position) {
}
});
MyAdapter uses
public class SecondPageAdapter extends FragmentPagerAdapter {
public SecondPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// Return how many Fragment's attached on View Pager
return 6;
}
#Override
public Fragment getItem(int position) {
Log.i("First", "postion="+position);
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return Fragment1.init(position);
case 1: // Fragment # 1 - This will show SecondFragment
return Fragment2.init(position);
case 2: // Fragment # 0 - This will show FirstFragment
return Fragment3.init(position);
case 3: // Fragment # 1 - This will show SecondFragment
return Fragment4.init(position);
case 4: // Fragment # 0 - This will show FirstFragment
return Fragment5.init(position);
case 5: // Fragment # 1 - This will show SecondFragment
return Fragment6.init(position);
default:// Fragment # default - Will show FirstFragment
return null;
}
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return super.saveState();
}
#Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
Log.d("DataSetChanged", "notifyDataSetChanged=");
}
}
#Override
public int getCount() {
return 6;
}
In your FragmentPagerAdapter and return number of pages you want to display

Android : FragmentPagerAdapter don't saved the state of Fragment?

I have a problem with the FragmentPagerAdapter .
I can not save the state of the Fragment and then there is the view that within the Fragment . Whenever I use the swipe left and right , the Fragment is recreated by overriding the method getItem ( int position ) in the static class that extends the FragmentPagerAdapter .
public static class GraphicsCollectionPagerAdapter extends FragmentPagerAdapter {
final int NUM_ITEMS = 3; // number of tabs
public GraphicsCollectionPagerAdapter(FragmentManager fm) {
super(fm);
fragmentList = new AnalyzeFragmentPageListWithDate();
fragment1 = new AnalyzeFragmentPage1();
fragment2 = new AnalyzeFragmentPage2();
}
#Override
public Fragment getItem(int position) {
//Log.i(TAG, "getItem() -> New fragment at position " + position);
switch (position) {
case 0:
return fragmentList;
case 1:
return fragment1;
case 2:
return fragment2;
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Fragm1";
case 1:
return "Fragm2";
case 2:
return "Fragm3";
}
return "OBJECT " + (position + 1);
}
}
Within the method OnCreateView of each instance of the Fragment there are several steps to the SQLite database and this causes a saturation of the Database Connection Pool.
The warning found whenever change dynamically fragment is: "W / SQLiteConnectionPool (1111 ) : A SQLiteConnection object for database ' / data / data / com.myapp / databases / mydb ' was leaked ! Please fix your application to end transactions in progress properly and to close the database When it is no longer needed . "
I already tried to use the FragmentStatePagerAdapter without success.
Could you kindly tell me how to proceed ? I do not want the Fragment is regenerated each time, causing problems to the database. Have you got an example for save Fragment/View sate?
I have not found any suggestion for now .
thank you very much
use setOffscreenPageLimit property for your ViewPager object.
ViewPager pager = (ViewPager) findViewById(R.id.viewPagerId);
pager.setOffscreenPageLimit(2);
First of all create a new instance of Fragment every time the getItem() is called
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentList();
case 1:
return new AnalyzeFragmentPage1();
case 2:
return new AnalyzeFragmentPage2();
}
return null;
}
Note that FragmentPagerAdapter does not work exactly like normal Adapter meaning it does NOT use an empty vessel Fragment and then refills it with different data.
Instead it creates a new Fragment whenever it is needed. So you should pass data (if any) when the Fragment is created. Please study the example in the Android docs.
In a real time projects instead of passing current position to a new Fragment you could pass the actual ID of the entry that a Fragment should refer to.

Categories

Resources