The getItem () method is called twice when using the Fragment Pager Adapter

I use the fragment pager adapter to instantiate the fragment class. I can do this, but my problem is that my getItem () method is called twice, which creates a problem even more. Please tell me why this is happening.

package com.creatiosoft.rssfeed.adaptor; import android.content.Context; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.util.Log; import com.creatiosoft.rssfeed.utils.RssItem; import com.viewpagerindicator.IconPagerAdapter; public class NewsFeedsAdapter extends FragmentPagerAdapter implements IconPagerAdapter { int[] icon = null; String[] content = null; String[] URLs = null; Context cont; public NewsFeedsAdapter(FragmentManager fm, Context context) { super(fm); Log.i("jk", "constructor"); this.cont = context; RssItem newsFeedAppliaction = (RssItem) cont; /* * Retrieving the values of the Icons and contents from the application * class in utils package */ icon = newsFeedAppliaction.getICONS(); content = newsFeedAppliaction.getCONTENT(); URLs = newsFeedAppliaction.getURL(); } /** instantiate a new fragment class */ @Override public Fragment getItem(int position) { Log.i("yt", "hello" + position); return TestFragment.newInstance(position % content.length, cont); } @Override public CharSequence getPageTitle(int position) { return content[position % content.length].toUpperCase(); } public int getIconResId(int index) { return icon[index]; } /** return the no of views on the basis of array items */ @Override public int getCount() { Log.i("hi", "length" + content.length); return content.length; } } 

I am calling Adapter with this code:

 NewsFeedsAdapter adapter = new NewsFeedsAdapter( getSupportFragmentManager(), getApplicationContext()); /** * get the id of the view pager declared in the xml resource and set the * adaptor on the view pager */ ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(adapter); //pager.setCurrentItem(0); /** * Tab page indicator class is used to indicate the tabs and is accessed * from the library class */ TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator); indicator.setViewPager(pager); 
+6
source share
3 answers

Remember that the pager supports at least one page in front . This means that when creating a pager, it creates at least two pages - the one that it shows and the next one to enable paging.

+11
source

In your getItem method, you create a new instance of the fragment. This is terrible. This puts you in the situation you are currently in. What you do is create a new object that will reside in it, as you indicated. But when the evil Android system decides that you need to talk to your snippet again and request it from your adapter, you tell it "No, I don't want to!" and instead giving him a new piece EXACTLY the same as his older roter.

To fix this, you inflate the entire fragment in advance and then return the correct fragment. Or, if you want, do not do this. But keep a record of the instances you created so that you can return fragments already created.

I don’t know exactly why it is called twice - I didn’t look at the v4 source, but this is probably done to ensure that the element was actually extracted, or the system entered a state in which the previous element should be referenced,

Finally, save the created instances - particularly for an object as heavy as fragments, views and actions.

 class Adapter extends MyWhateverAdapter { Fragment[] myLovelyFragments; public Adapter() { // Instantiate your fragments and place them into myLovelyFragments } public int getFragmentCount() { return myLovelyFragments.length; } public Fragment getItem(int position) { return myLovelyFragments[position]; } } 
+2
source

The problem you probably have is not multiple calls to the getItem() method. After a little research, I found that the isViewFromObject() method has a huge role, and its documentation says that "This method is necessary to use the PagerAdapter function properly."

I also found that the correct way to implement the method is as follows:

 @Override public boolean isViewFromObject(View view, Object object) { if(object != null){ return ((Fragment)object).getView() == view; }else{ return false; } } 

You can look here .

0
source

Source: https://habr.com/ru/post/927371/


All Articles