Obsolete
Status Update
Comments
da...@gmail.com <da...@gmail.com> #2
This is a very desirable feature. We apply some transformations to the views once a new page is selected and the fact this event is not called for the first page is making us look for other places to hook up the event.
Trying to track the first invocation of getItem(int position) on the page adapter did not yield any good results as the first time this method is called is on onMeasure() of the ViewPager, which is not suitable for this kind of checks.
Trying to track the first invocation of getItem(int position) on the page adapter did not yield any good results as the first time this method is called is on onMeasure() of the ViewPager, which is not suitable for this kind of checks.
ma...@gmail.com <ma...@gmail.com> #3
I have this problem too, is it a bug?
ma...@gmail.com <ma...@gmail.com> #4
On a side note for anyone stuck on this issue, you can do the following below to still get the same effect , extending the SimpleOnPageChangeListener seems to work for me :
public class MainActivity extends SherlockFragmentActivity implements
ActionBar.TabListener,
HeadlinesFragment.OnHeadlineSelectedListener {
private class PageListener extends ViewPager.SimpleOnPageChangeListener {
public void onPageSelected(int position) {
getSupportActionBar().setSelectedNavigationItem(position);
}
}
private ViewPager m_ViewPager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// For the paging swipes
MyPagerAdapter adapter = new MyPagerAdapter();
m_ViewPager = (ViewPager) findViewById(R.id.myfivepanelpager);
m_ViewPager.setAdapter(adapter);
m_ViewPager.setCurrentItem(0);
PageListener pageListener = new PageListener();
m_ViewPager.setOnPageChangeListener(pageListener);
if (m_ViewPager != null) {
........
public class MainActivity extends SherlockFragmentActivity implements
ActionBar.TabListener,
HeadlinesFragment.OnHeadlineSelectedListener {
private class PageListener extends ViewPager.SimpleOnPageChangeListener {
public void onPageSelected(int position) {
getSupportActionBar().setSelectedNavigationItem(position);
}
}
private ViewPager m_ViewPager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// For the paging swipes
MyPagerAdapter adapter = new MyPagerAdapter();
m_ViewPager = (ViewPager) findViewById(R.id.myfivepanelpager);
m_ViewPager.setAdapter(adapter);
m_ViewPager.setCurrentItem(0);
PageListener pageListener = new PageListener();
m_ViewPager.setOnPageChangeListener(pageListener);
if (m_ViewPager != null) {
........
nk...@gmail.com <nk...@gmail.com> #5
There is very easy work around:
In your adapter write:
public static interface FirstShot
{
public void onFirstShot();
}
private FirstShot firstShot;
private boolean shot = false;
@Override
public Fragment getItem(int arg0)
{
if (!shot && firstShot != null)
{
shot = true;
firstShot.onFirstShot();
}
// your code here
}
Now implement FirstShot in activity:
@Override
public void onFirstShot()
{
viewPager.post(new Runnable()
{
@Override
public void run()
{
viewPager.setCurrentItem(position, false);
}
});
}
onPageSelected() now calls!
In your adapter write:
public static interface FirstShot
{
public void onFirstShot();
}
private FirstShot firstShot;
private boolean shot = false;
@Override
public Fragment getItem(int arg0)
{
if (!shot && firstShot != null)
{
shot = true;
firstShot.onFirstShot();
}
// your code here
}
Now implement FirstShot in activity:
@Override
public void onFirstShot()
{
viewPager.post(new Runnable()
{
@Override
public void run()
{
viewPager.setCurrentItem(position, false);
}
});
}
onPageSelected() now calls!
nk...@gmail.com <nk...@gmail.com> #6
And small work around of work around:
@Override
public void onFirstShot()
{
viewPager.post(new Runnable()
{
@Override
public void run()
{
boolean needNotify = false;
if (pager.getCurrentItem() == position) needNotify = true; // if you set page which is set already (usually 0 page), you must call onPageSelected manually.
viewPager.setCurrentItem(position, false);
if (needNotify) onPageSelected(position);
}
});
}
@Override
public void onFirstShot()
{
viewPager.post(new Runnable()
{
@Override
public void run()
{
boolean needNotify = false;
if (pager.getCurrentItem() == position) needNotify = true; // if you set page which is set already (usually 0 page), you must call onPageSelected manually.
viewPager.setCurrentItem(position, false);
if (needNotify) onPageSelected(position);
}
});
}
sa...@gmail.com <sa...@gmail.com> #7
I think that best workaround is to maintain a reference to PageChangeListener and call onPageSelected manually after setting the adapter. I.e:
onCreate/onActivityCreated:
mPageChangeListener = new PageChangeListener();
Whenever you change/set your adapter:
mViewPager.setAdapter(new YourPagerAdapter());
mPageChangeListener.onPageSelected(0);
onCreate/onActivityCreated:
mPageChangeListener = new PageChangeListener();
Whenever you change/set your adapter:
mViewPager.setAdapter(new YourPagerAdapter());
mPageChangeListener.onPageSelected(0);
ta...@gmail.com <ta...@gmail.com> #8
Very annoying, it should be fixed to avoid ugly workarounds.
Link to a short analysis of this bug, should be enough of a hint for a fix:
http://stackoverflow.com/a/17694619/183575
Link to a short analysis of this bug, should be enough of a hint for a fix:
ba...@gmail.com <ba...@gmail.com> #9
hi every one ,i upload a patch to fix this problem, but no guy add review or verifier it , you can add your feedbacks to help us to fix it forever.
related link:
https://android-review.googlesource.com/#/c/68333/
related link:
en...@google.com <en...@google.com>
[Deleted User] <[Deleted User]> #10
Issue is not obsolete .
please use Bangbang patch for viewpager of support package
please use Bangbang patch for viewpager of support package
[Deleted User] <[Deleted User]> #11
Is this fixed with the viewPager2??
ta...@gmail.com <ta...@gmail.com> #12
Just let the bug die, they won't fix it. It's hardly relevant 9 years after anyway.
Description
Use case: I have an activity that has a five page ViewPager.
In the onResume() method of that activity, I check for an intent parameter specifying which page to display and call pager.setCurrentItem(n, false) to select that page.
The ViewPager also has an OnPageChangeListener to watch for page changes and will tweak the page content (it highlights a field on the page).
The trouble is, if we have just created the ViewPager, and if the page selected is page 0, the OnPageChangeListener won't be called. Obviously this is because the ViewPager defaults to page 0 and doesn't think the page has changed.
IMO the listener ought to be called immediately after creation (with page set to 0).
At the moment, the work around is that I have to check which page is currently displayed and call the listener myself if necessary.
public class FooActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager);
ViewPager pager = (ViewPager) findViewById(R.id.view_pager);
pager.setAdapter(new FooPagerAdapter(this));
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position)
{
/**/Log.d("foo", "**** onPageSelected = "+position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageScrollStateChanged(int arg0) {}
});
}
@Override
protected void onResume()
{
super.onResume();
int page = getIntent().getIntExtra("page", 0))
ViewPager pager = (ViewPager) findViewById(R.id.view_pager);
pager.setCurrentItem(page, false);
}
}
Android: 4.03