简体   繁体   中英

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:

在此输入图像描述 在此输入图像描述

 @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): 在此输入图像描述

And if I let it slide it looks like this:

在此输入图像描述

use textView.setTranslationX(positionOffsetPixels); instead of margins

 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.

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);
                    }
        }
    }

Have a look at Slowing speed of Viewpager controller in android

You can set custom duration for Viewpager scrolling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM