getItem() calling multiple time in FragmentPagerAdapter

Ask by : yokees November 03, 2012 23:24

I am developing application using viewpager with fragment.In my application i have a items which i showing in List.After that when i click i have calling SherlockFragmentActivity which calling ViewPager with adapter.Now the problem is getItem in FragmentPagerAdapter give multiple position at a single time.Also when i flip backword then also giveing wrong position.Here is my code

ViewPager Code:-

private void initialisePaging() {
            //Adapter Context;
    this.mPagerAdapter = new MyPagerAdapter(
            super.getSupportFragmentManager(), getApplicationContext(),
            title, link, description);

    ViewPager pager = (ViewPager) super.findViewById(;
           //Take position from ListView & set postion .

My Adapter Class which responsible for set fragment:-

Problem:- The problem is getItem Calling 3 time & suppose i click on 4 item its giving me 3,4,5 number.

public MyPagerAdapter(FragmentManager fm, Context cont,
            ArrayList<String> title, ArrayList<String> link,
            ArrayList<String> description) {

    this.context = cont;
    this.key_desc = description;
    this.key_link = link;
    this.key_title = title;
    viewPagerApplication = (RssItem) cont;


public Fragment getItem(int position) {
        return Fragment0.newInstance(position, this.context, key_title,
            key_link, key_desc);

public int getCount() {
     return key_title.size();
My Fragment Class:-

Problem:-Here if i show postion in textview its correct position but if i want take position in logcat for further parsing its giving me again 4,3,5 number which is also diffrent from getItem();

public static Fragment0 newInstance(int num,Context cont, ArrayList<String> key_title, ArrayList<String> key_link, ArrayList<String> key_desc) {

    Fragment0 f = new Fragment0();
    Bundle args = new Bundle();
    args.putInt("num", num);

    return f;

 public void onCreate(Bundle savedInstanceState) {



   public int getShownIndex() {
       return getArguments().getInt("num", 0);
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         View v = inflater.inflate(R.layout.newsdetailfragment_screen, container, false);
          tv = v.findViewById(;
           return v;

public void onActivityCreated(Bundle savedInstanceState) {

Now kindly let me know where i am wrong & how can i get only one exact position for further process.

View original question

Answer by : Marcin S.November 03, 2012 23:24

Inside of getItem(), the position parameter is the position that is in need of rendering. It is NOT the position of the currently focused item that the user would see. The pages to the left and right of the currently displayed view need to be pre rendered in memory so that the animations to those screens will be smooth. To get the item at current position use:


If you need an actual position of the page you can implement onPageChangeListener

pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        public void onPageSelected(int position) {

position in onPageSelected will give an accurate current position of the page.

View original answer