Marcin S.
Marcin S.

Reputation: 11191

How to display different text on each page in ViewPager, instantiateItem() confusion

In the CustomPagerAdapter of the ViewPager, in instantiateItem() method I'm trying to create an TextView and then for each page set a different text depending on certain condition. Text is read from a pages Cursor. Here is a code:

@Override
public Object instantiateItem(ViewGroup collection, int position) {

sc = new ScrollView(context);
sc.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
sc.setFillViewport(true);
tv = new TextView(context);


if(position < count) {
  tv.setText(pages.getString(1));   
  pages.moveToPosition(position);

}else {
  tv.setText("LOCKED");
}          

  tv.setTag(TAG_PAGE + position);

  tv.setGravity(Gravity.CENTER);
  tv.setTextColor(Color.BLACK);
  tv.setTextSize(30);
  sc.addView(tv);

  ((ViewPager) collection).addView(sc);

  return sc;            
}

However ViewPager behaves not as expected. The first and the second page have the same text, rest of the pages has a sign "LOCKED" as expected. When I swipe into the 4th page and come back to the first page then the first page consists of the text that suppose to be in the second page. I also tried to use myViewPager.setOffscreenPageLimit(numberOfPages) however it doesn't help.

I found this answer:

"Inside of instantiateItem, 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. "

It make sense to me but how then can I correctly display the pages content and then update it if desired? Please advise if there is different way to do it with skipping instantiateItem() method that introduce the mess and confusion into the problem. Thank you.

Upvotes: 1

Views: 1673

Answers (2)

Marcin S.
Marcin S.

Reputation: 11191

I have solved this problem by using a different implementation:

// Adapter class
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm)   {
    super(fm);
}

@Override
public Fragment getItem(int index) {
    return PageFragment.newInstance(pages[index]);  // Pages is an array of Strings
    }

@Override
public int getCount() {
    return numberOfPages;
    }
}


// PageFragment class
public class PageFragment extends Fragment {

TextView tv;

public static PageFragment newInstance(String page) {
    PageFragment pageFragment = new PageFragment();
    Bundle bundle = new Bundle();
    bundle.putString("pageContent", page);
    pageFragment.setArguments(bundle);

    return pageFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)   {
    View view = inflater.inflate(R.layout.fragment, container, false);
    tv = (TextView) view.findViewById(R.id.text_view);
    tv.setText(getArguments().getString("pageContent"));

    return view;
    }
}

Upvotes: 2

Mohd Saleem
Mohd Saleem

Reputation: 94

You can Create ViewPager Object and then set Listener onthis object.

               ViewPager myPager = (ViewPager) findViewById(R.id.yourPagerid);
            myPager.setAdapter(adapter);
                 myPager.setCurrentItem(0);

                    myPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
                      //You can change textview word according to current page

            switch (position) {
            case 0:

                break;
            case 1:

                break;
            case 2:

                break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // Log.d("check","onPageScrolled");

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // Log.d("check","onPageScrollStateChanged");

        }
    });

Upvotes: 0

Related Questions