daimajia / AndroidImageSlider

An amazing and convenient Android image slider.
MIT License
5.62k stars 1.66k forks source link

Incorrect position in OnPageScrolled #409

Open wildvoice13 opened 5 years ago

wildvoice13 commented 5 years ago

Greetings!

I've tried to move a button on 1 slide with it in my Slider with 3 slides and got some strange values of position in function OnPageScrolled. Everything works pretty nice except one thing - instead of slide position value like 0, 1, 2, 0, 1... int position gives 120, 121, 122, 123, 124... etc, on other device - 720, 721, 722, 723... etc.

Here's the code:

        slider.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {

            //movement of button
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.d("TAG","Position "+position);
                DisplayMetrics metr = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metr);
                int width = metr.widthPixels;
                if (position % file_maps.size() == (position+slider.getCurrentPosition())% file_maps.size() && positionOffset < 0.75f) {
                    tv_app.setVisibility(View.VISIBLE);
                    tv_app.setTranslationX(-positionOffsetPixels);
                } else if (position % file_maps.size() == 0 && positionOffset >= 0.6f) {
                    tv_app.setVisibility(View.INVISIBLE);
                }
                if (position % file_maps.size() == file_maps.size()-1 && positionOffset>0.5f) {
                    tv_app.setTranslationX(width - positionOffsetPixels);
                    tv_app.setVisibility(View.VISIBLE);
                } else if (position % file_maps.size()-1 == file_maps.size()-1 && positionOffset <= 0.4f) {
                    tv_app.setVisibility(View.INVISIBLE);
                }
            }

And here's log's fragment:

2019-02-28 09:57:33.618 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.360 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.385 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.401 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.427 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.444 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.460 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.477 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.494 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.510 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.527 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.544 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.561 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.577 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.594 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.611 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.627 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.644 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.661 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.678 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.694 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.711 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.728 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.744 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.761 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.778 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.794 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.811 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.828 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:44.844 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.061 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.078 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.095 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.111 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.128 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.145 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.162 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.178 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.195 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.212 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.228 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.245 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.262 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.278 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.295 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.312 16773-16773/test.sliderapp D/TAG: Position 120
2019-02-28 09:57:45.329 16773-16773/test.sliderapp D/TAG: Position 121

It.s pretty strange behavior, because even with division by whole I can't be sure that slider's position is correct. I've looked over github and stackoverflow yet I haven't seen anything about it. Workspace: Android Studio 3.3.1, gradle implementation - com.daimajia.slider:library:1.1.5@aar, tried on API 25.