lsjwzh / RecyclerViewPager

Deprecated
Apache License 2.0
3.53k stars 667 forks source link

Can i use ViewPagerIndicator with this library? #39

Open TheLester opened 8 years ago

TheLester commented 8 years ago

Can i somehow use ViewPagerIndicator with this library? https://github.com/JakeWharton/ViewPagerIndicator

lsjwzh commented 8 years ago

I will support this

afaucogney commented 8 years ago

Based on CircleIndicator, you can use the following:

public class ECircleIndicator extends LinearLayout implements ERecyclerViewPager.OnPageChangedListener {
    private static final int DEFAULT_INDICATOR_WIDTH = 5;
    private ERecyclerViewPager mViewpager;
    private int mIndicatorMargin = -1;
    private int mIndicatorWidth = -1;
    private int mIndicatorHeight = -1;
    private int mAnimatorResId;
    private int mAnimatorReverseResId;
    private int mIndicatorBackgroundResId;
    private int mIndicatorUnselectedBackgroundResId;
    private int mCurrentPosition;
    private Animator mAnimationOut;
    private Animator mAnimationIn;

    public ECircleIndicator(Context context) {
        super(context);
        this.mAnimatorResId = me.relex.circleindicator.R.animator.scale_with_alpha;
        this.mAnimatorReverseResId = 0;
        this.mIndicatorBackgroundResId = me.relex.circleindicator.R.drawable.white_radius;
        this.mIndicatorUnselectedBackgroundResId = me.relex.circleindicator.R.drawable.white_radius;
        this.mCurrentPosition = 0;
        this.init(context, (AttributeSet) null);
    }

    public ECircleIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mAnimatorResId = me.relex.circleindicator.R.animator.scale_with_alpha;
        this.mAnimatorReverseResId = 0;
        this.mIndicatorBackgroundResId = me.relex.circleindicator.R.drawable.white_radius;
        this.mIndicatorUnselectedBackgroundResId = me.relex.circleindicator.R.drawable.white_radius;
        this.mCurrentPosition = 0;
        this.init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.setOrientation(LinearLayout.HORIZONTAL);
        this.setGravity(17);
        this.handleTypedArray(context, attrs);
        this.checkIndicatorConfig(context);
    }

    private void handleTypedArray(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, me.relex.circleindicator.R.styleable.CircleIndicator);
            this.mIndicatorWidth = typedArray.getDimensionPixelSize(me.relex.circleindicator.R.styleable.CircleIndicator_ci_width, -1);
            this.mIndicatorHeight = typedArray.getDimensionPixelSize(me.relex.circleindicator.R.styleable.CircleIndicator_ci_height, -1);
            this.mIndicatorMargin = typedArray.getDimensionPixelSize(me.relex.circleindicator.R.styleable.CircleIndicator_ci_margin, -1);
            this.mAnimatorResId = typedArray.getResourceId(me.relex.circleindicator.R.styleable.CircleIndicator_ci_animator, me.relex.circleindicator.R.animator.scale_with_alpha);
            this.mAnimatorReverseResId = typedArray.getResourceId(me.relex.circleindicator.R.styleable.CircleIndicator_ci_animator_reverse, 0);
            this.mIndicatorBackgroundResId = typedArray.getResourceId(me.relex.circleindicator.R.styleable.CircleIndicator_ci_drawable, me.relex.circleindicator.R.drawable.white_radius);
            this.mIndicatorUnselectedBackgroundResId = typedArray.getResourceId(me.relex.circleindicator.R.styleable.CircleIndicator_ci_drawable_unselected, this.mIndicatorBackgroundResId);
            typedArray.recycle();
        }
    }

    public void configureIndicator(int indicatorWidth, int indicatorHeight, int indicatorMargin) {
        this.configureIndicator(indicatorWidth, indicatorHeight, indicatorMargin, me.relex.circleindicator.R.animator.scale_with_alpha, 0, me.relex.circleindicator.R.drawable.white_radius, me.relex.circleindicator.R.drawable.white_radius);
    }

    public void configureIndicator(int indicatorWidth, int indicatorHeight, int indicatorMargin, @AnimatorRes int animatorId, @AnimatorRes int animatorReverseId, @DrawableRes int indicatorBackgroundId, @DrawableRes int indicatorUnselectedBackgroundId) {
        this.mIndicatorWidth = indicatorWidth;
        this.mIndicatorHeight = indicatorHeight;
        this.mIndicatorMargin = indicatorMargin;
        this.mAnimatorResId = animatorId;
        this.mAnimatorReverseResId = animatorReverseId;
        this.mIndicatorBackgroundResId = indicatorBackgroundId;
        this.mIndicatorUnselectedBackgroundResId = indicatorUnselectedBackgroundId;
        this.checkIndicatorConfig(this.getContext());
    }

    private void checkIndicatorConfig(Context context) {
        this.mIndicatorWidth = this.mIndicatorWidth < 0 ? this.dip2px(5.0F) : this.mIndicatorWidth;
        this.mIndicatorHeight = this.mIndicatorHeight < 0 ? this.dip2px(5.0F) : this.mIndicatorHeight;
        this.mIndicatorMargin = this.mIndicatorMargin < 0 ? this.dip2px(5.0F) : this.mIndicatorMargin;
        this.mAnimatorResId = this.mAnimatorResId == 0 ? me.relex.circleindicator.R.animator.scale_with_alpha : this.mAnimatorResId;
        this.mAnimationOut = AnimatorInflater.loadAnimator(context, this.mAnimatorResId);
        if (this.mAnimatorReverseResId == 0) {
            this.mAnimationIn = AnimatorInflater.loadAnimator(context, this.mAnimatorResId);
            this.mAnimationIn.setInterpolator(new ECircleIndicator.ReverseInterpolator());
        } else {
            this.mAnimationIn = AnimatorInflater.loadAnimator(context, this.mAnimatorReverseResId);
        }

        this.mIndicatorBackgroundResId = this.mIndicatorBackgroundResId == 0 ? me.relex.circleindicator.R.drawable.white_radius : this.mIndicatorBackgroundResId;
        this.mIndicatorUnselectedBackgroundResId = this.mIndicatorUnselectedBackgroundResId == 0 ? this.mIndicatorBackgroundResId : this.mIndicatorUnselectedBackgroundResId;
    }

    public void setViewPager(ERecyclerViewPager viewPager) {
        this.mViewpager = viewPager;
        this.mCurrentPosition = this.mViewpager.getCurrentPosition();
        this.createIndicators(viewPager);
        this.mViewpager.removeOnPageChangedListener(this);
        this.mViewpager.addOnPageChangedListener(this);
        //this.onPageSelected(this.mCurrentPosition);
        this.OnPageChanged(1, this.mCurrentPosition);
    }

    public void onPageSelected(int position) {
        if (mViewpager.getAdapter() == null || mViewpager.getAdapter().getItemCount() <= 0) {
            return;
        }

        if (this.mAnimationIn.isRunning()) {
            this.mAnimationIn.end();
        }

        if (this.mAnimationOut.isRunning()) {
            this.mAnimationOut.end();
        }

        View currentIndicator = this.getChildAt(this.mCurrentPosition);
        currentIndicator.setBackgroundResource(this.mIndicatorUnselectedBackgroundResId);
        this.mAnimationIn.setTarget(currentIndicator);
        this.mAnimationIn.start();
        View selectedIndicator = this.getChildAt(position);
        selectedIndicator.setBackgroundResource(this.mIndicatorBackgroundResId);
        this.mAnimationOut.setTarget(selectedIndicator);
        this.mAnimationOut.start();
        this.mCurrentPosition = position;

    }

    private void createIndicators(ERecyclerViewPager viewPager) {
        this.removeAllViews();
        if (viewPager.getAdapter() == null) {
            return;
        }

        int count = viewPager.getAdapter().getItemCount();
        if (count > 0) {
            this.addIndicator(this.mIndicatorBackgroundResId, this.mAnimationOut);

            for (int i = 1; i < count; ++i) {
                this.addIndicator(this.mIndicatorUnselectedBackgroundResId, this.mAnimationIn);
            }

        }
    }

    private void addIndicator(@DrawableRes int backgroundDrawableId, Animator animator) {
        if (animator.isRunning()) {
            animator.end();
        }

        View Indicator = new View(this.getContext());
        Indicator.setBackgroundResource(backgroundDrawableId);
        this.addView(Indicator, this.mIndicatorWidth, this.mIndicatorHeight);
        LayoutParams lp = (LayoutParams) Indicator.getLayoutParams();
        lp.leftMargin = this.mIndicatorMargin;
        lp.rightMargin = this.mIndicatorMargin;
        Indicator.setLayoutParams(lp);
        animator.setTarget(Indicator);
        animator.start();
    }

    public int dip2px(float dpValue) {
        float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5F);
    }

    @Override
    public void OnPageChanged(int i, int i1) {
        onPageSelected(i1);
    }

private class ReverseInterpolator implements Interpolator {
    private ReverseInterpolator() {
    }

    public float getInterpolation(float value) {
        return Math.abs(1.0F - value);
    }
}
CJEEP commented 7 years ago

@lsjwzh @afaucogney doesn't call OnPageChangedListener sometimes,why?