Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setCurrentItem on viewPager not working #58

Closed
JcMinarro opened this issue Jan 24, 2015 · 6 comments
Closed

setCurrentItem on viewPager not working #58

JcMinarro opened this issue Jan 24, 2015 · 6 comments
Assignees
Labels
Milestone

Comments

@JcMinarro
Copy link

When I create my view, I want that viewPager move to some pager (eg pager number 1).
ViewPager move to that pager and PSTS update the states of every tab, but doesn't move the scroll to the correct position.
I have changed your example to be like mine and it doesn't work.
The changes that I have done are the following:

MainActivity.java:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        setSupportActionBar(toolbar);
        // create our manager instance after the content view is set
        mTintManager = new SystemBarTintManager(this);
        // enable status bar tint
        mTintManager.setStatusBarTintEnabled(true);
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        tabs.setViewPager(pager);
        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                .getDisplayMetrics());
        pager.setPageMargin(pageMargin);
        changeColor(getResources().getColor(R.color.green));
     pager.setCurrentItem(1);
    }

activity_main.xml:

<com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:pstsShouldExpand="true" />

screenshot_2015-01-24-13-26-51

@jpardogo
Copy link
Owner

Change:

     pager.setCurrentItem(1);

For:

    pager.post(new Runnable() {
            @Override
            public void run() {
                pager.setCurrentItem(1);
            }
    });

But this just work until you start rotating the device because then It get lost....

When changing the default tab, there are still problems keeping the offset values so you may notice 'snapping' or wrong position on rotation.

I will try to spend some time on it when possible. Because it has been a problem for a while and I probably didn't spend enough time looking for a proper solution.
So far, that's all you can get changing the default starting tab.

I leave the issue open and It will be the first issue to fix for next release.

@jpardogo jpardogo modified the milestone: 1 Jan 24, 2015
@jpardogo jpardogo self-assigned this Jan 24, 2015
@jpardogo jpardogo added the bug label Jan 24, 2015
@jpardogo jpardogo modified the milestones: 1, 1.0.9 Jan 24, 2015
@JcMinarro
Copy link
Author

Ok, that works, but is a bit rare...
It starts on position 0 and then scroll to position x...

@jpardogo
Copy link
Owner

Yes I know. I want that fixed. If you find the solution PR are welcome. Otherwise I will try to fix it when I have a moment to study the problem in depth

@JcMinarro
Copy link
Author

I have seen that if you don't remove the OnGlobalLayoutListener that you added on notifyDataSetChanged() method, It works fine. But I don't know if it could made other bugs.

@jpardogo
Copy link
Owner

Fix in PR #65 and example updated to demonstrate fix in commit 5672cc8

@szuwest
Copy link

szuwest commented Mar 26, 2015

Is there any way to not snapping?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants