Reputation: 77
I ran into some strange issue, whenever I'm trying to assign position to my textview, it goes very strange with the first and second page, it returns 0 for the first one and 0 for the second one, page three says 1, and whenever I swipe back to page 0 it says 1.
Here is my activity (theabcactivity):
public class theabcactivity extends FragmentActivity {
String[] heabc;
int backColor;
int currentnumber = 0;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_theabcactivity);
heabc = getResources().getStringArray(R.array.heabc);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setPageTransformer(true, new ZoomOutPageTransformer());
mPagerAdapter = new
ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(0);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int CurPage) {
currentnumber = CurPage;
Log.e("CurPage", " " + CurPage);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Random rnd = new Random();
backColor = Color.argb(40, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
return new theabc();
}
@Override
public int getCount() {
return heabc.length;
}
}
}
Here I'm assigning the currentnumber = CurPage;, that I reuse later on at my fragment.
Fragment code (theabc):
public class theabc extends Fragment {
TextView itemscount;
String [] heabc;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.theabc, container, false);
LinearLayout contentll = (LinearLayout)rootView.findViewById(R.id.content);
heabc = getResources().getStringArray(R.array.heabc);
itemscount = (TextView)rootView.findViewById(R.id.itemcount);
theabcactivity abcact = (theabcactivity) getActivity();
contentll.setBackgroundColor(abcact.backColor);
itemscount.setText(String.valueOf(abcact.currentnumber));
Log.e("www", "current number value" + abcact.currentnumber);
return rootView;
}
}
One other thing i've noticed that whenever this activity opens up, it loads the fragment two times as I'm receiving the following Log:
E/www: current number value0
E/www: current number value0
It leads me into conclusion that when Viewpager opens up it loads the first two items, the first one and the second one. How can I solve that it would load the first item as 0, the next one 1 and so on?
Thanks in advance.
Upvotes: 1
Views: 1455
Reputation: 37
This is the normal behaviour of the viewPager widget, it first instantiates the first element (which is the current element) then it loads the next element of the list/array to give the scroll activity a smooth animation. I have faced the same issue and solved it by adding a listener to my view pager
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// This is called a lot of times when the user is scrolling
}
@Override
public void onPageSelected(int position) {
// Check position here to see which page was selected
Log.i("current pos_page", "\n" + "\t\t" + "current pos of selected page after scroll = " + position );
display(position);
}
@Override
public void onPageScrollStateChanged(int state) {
// Called when the scroll state changes (scroll started - ended)
if ( state == ViewPager.SCROLL_STATE_IDLE) {
currentPosition = mViewPager.getCurrentItem();
Log.i("current pos_scrolled", "\n" + "\t\t" + "current pos SCROLLED FINISH = " + currentPosition );
}
}
});
Then I have used the overrided methods of the page change listener to set the current position to the one given by the onPageScrollStateChanged
once the animation of scroll is finished.
Finally, i just call the function i created to set the views i'd like to update inside of onPageSelected
.
Upvotes: 1