user6586661
user6586661

Reputation: 452

Android ViewPager "slide lag"

I have a ViewPager Fragment with 3 fragments. Each of it has an image as background and a TextView. The TextView is always absolute on the left side of the screen (not the Fragment itself). When I move the fragments with my finger, it stays at the correct position. Also when I move it fast. But when I let it slide into the other Fragment, the TextView doesn't stays at the correct position. It looks like it lags around.

It should stay like in these 2 pictures:

enter image description here enter image description here

 @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        SingleTabFragment singleTabFragment = (SingleTabFragment) mSectionsPagerAdapter.getRegisteredFragment(position);
        TextView textView = singleTabFragment.getNonResizingTextView();
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams)textView.getLayoutParams();
        marginLayoutParams.leftMargin = positionOffsetPixels;
        textView.setLayoutParams(marginLayoutParams);

    }

I really don't know what the problem is. I tried it multiple ways, i added Thread.sleep(50); to see if it also happens there. And yes, also there it doesn't work correct. How can I fix this?

EDIT:

Now I have put in the middle. But the problem is the same.

Here is how it should look like (I know, not that much frames. But I think you will get the point): enter image description here

And if I let it slide it looks like this:

enter image description here

Upvotes: 3

Views: 2098

Answers (4)

Tin Tran
Tin Tran

Reputation: 2285

 TextView textView = singleTabFragment.getNonResizingTextView();
 textView.translationX = positionOffsetPixels;

You should not do marginLayoutParams.leftMargin = positionOffsetPixels; because changing the margin may change the position of the View, which will cause the View to be re-layout on every frame. That is why it is lagging. Changing the translationX will only change how the view is drawn instead of it's position.

Upvotes: 1

Vamsi
Vamsi

Reputation: 878

Have a look at Slowing speed of Viewpager controller in android

You can set custom duration for Viewpager scrolling.

Upvotes: -1

Antonis Lat
Antonis Lat

Reputation: 545

use textView.setTranslationX(positionOffsetPixels); instead of margins

Upvotes: 1

Divy Soni
Divy Soni

Reputation: 836

You can use following method to do the same instead of doing it in onPageScrolled:

mPager.setPageTransformer(false, new FadePageTransformer());

public class FadePageTransformer implements ViewPager.PageTransformer {
        public void transformPage(View view, float position) {
            view.setAlpha(1 - Math.abs(position));
                    if (position < 0) {
                        view.setScrollX(-(int) ((float) (view.getWidth()) * -position));
                    } else if (position > 0) {
                        view.setScrollX((int) ((float) (view.getWidth()) * position));
                    } else {
                        view.setScrollX(0);
                    }
        }
    }

Upvotes: 1

Related Questions