siralam / LoopingViewPager

A ViewPager and PagerAdapter combination that support auto scroll, infinite loop and page indicators.
532 stars 64 forks source link

How to integrate pager indicator with auto scroll functionality in java. #42

Closed ThirupathiMukkera closed 3 years ago

ThirupathiMukkera commented 3 years ago

Could you write a code snippet on java to integrate this.

siralam commented 3 years ago

Do you mean you understand there is a Kotlin example on this, but you want a Java version?

ThirupathiMukkera commented 3 years ago

Do you mean you understand there is a Kotlin example on this, but you want a Java version?

Yes, I need Java example for this.

ThirupathiMukkera commented 3 years ago

I tried this, its working for me.

mPager = findViewById(R.id.viewPager);
pagerIndicator = findViewById(R.id.indicator);
mPager.setAdapter(new InfiniteSlideAdapter(this));
    pagerIndicator.setHighlighterViewDelegate(new Function1<FrameLayout, View>() {
        @Override
        public View invoke(FrameLayout frameLayout) {
            View selected = new View(HomeActivity.this);
            selected.setLayoutParams(new LinearLayout.LayoutParams(DisplayUtils.convertDpToPx(16), DisplayUtils.convertDpToPx(2)));
            selected.setBackgroundColor(Color.GRAY);
            return selected;
        }
    });

    pagerIndicator.setUnselectedViewDelegate(new Function1<LinearLayout, View>() {
        @Override
        public View invoke(LinearLayout linearLayout) {
            View unselected = new View(HomeActivity.this);
            unselected.setLayoutParams(new LinearLayout.LayoutParams(DisplayUtils.convertDpToPx(16), DisplayUtils.convertDpToPx(2)));
            unselected.setBackgroundColor(Color.GRAY);
            unselected.setAlpha(0.4F);
            return unselected;
        }
    });

    mPager.setOnIndicatorProgress(new Function2<Integer, Float, Unit>() {
        @Override
        public Unit invoke(Integer integer, Float aFloat) {
            this.invoke(((Number)integer).intValue(), ((Number)aFloat).floatValue());
            return Unit.INSTANCE;
        }
        public void invoke(int selectingPosition, float progress) {
            pagerIndicator.onPageScrolled(selectingPosition, progress);
        }
    });
    pagerIndicator.updateIndicatorCounts(mPager.getIndicatorCount());`