天天看点

RecyclerView 滑动时,添加indicator,并实现联动

RecyclerView 滑动时,添加indicator,并实现联动

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    //获取第一个可见Item的位置, 4 表示一行最多显示4个Item;indexNum 表示共有多少页数据
    int position = manager.findFirstVisibleItemPosition();
    if (indexNum > ) {
        indicatorIndex = (position / ) % indexNum;
        if (position %  > ) {
            indicatorIndex = indicatorIndex == indexNum ?  : indicatorIndex + ;
        }
        mIndicator.onPageScrolled(indicatorIndex, f, dx);
    }

    // firstVisiableChildView 获取第一个Item的View
    //View firstVisiableChildView = manager.findViewByPosition(position);
    //int itemWidth = firstVisiableChildView.getWidth();

    //活动时的位移
    //int result = (position) * itemWidth - firstVisiableChildView.getLeft();
});
           

制作圆形的通用Indicator,此Indicator既可以应用于RecyclerView,也可以应用于ViewPager:

public class SimpleCircleIndicator extends View {
    Paint circlePaint;

    private int pageNum;
    private float scrollPercent = f;
    private int currentPosition;
    private int gapSize;


    private float radius;
    private int colorOn;
    private int colorOff;

    public SimpleCircleIndicator(Context context) {
        super(context);
        init();
    }

    public SimpleCircleIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SimpleCircleIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        radius = CommonUtil.dip2px(, getContext());
        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        colorOn = Color.WHITE;
        colorOff = Color.parseColor("#888888");
        gapSize = CommonUtil.dip2px(, getContext());
    }

    public void setSelectDotColor(int colorOn) {
        this.colorOn = colorOn;
    }

    public void setUnSelectDotColor(int colorOff) {
        this.colorOff = colorOff;
    }


    public void onPageScrolled(int position, float percent, int pixels) {
        scrollPercent = percent;
        currentPosition = position;
        invalidate();
    }

    private ViewPager viewPager;

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        if (null != viewPager) {
            pageNum = viewPager.getAdapter().getCount();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (pageNum <= ) {
            return;
        }
        float left = (getWidth() - (pageNum - ) * gapSize) * f;
        float height = getHeight() * f;
        circlePaint.setColor(colorOff);
        for (int i = ; i < pageNum; i++) {
            canvas.drawCircle(left + i * gapSize, height, radius, circlePaint);
        }
        circlePaint.setColor(colorOn);
        canvas.drawCircle(left + currentPosition * gapSize + gapSize * scrollPercent, height, radius, circlePaint);
    }

    public void setPageNum(int nums){
        pageNum = nums;
    }

}
           
RecyclerView 滑动时,添加indicator,并实现联动