reclyview底部的滑动指示器

1.ScrollBarView

public class ScrollBarView extends View {

    private Paint mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private RectF mBgRect = new RectF();
    private Float mRadius = 0f;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private RectF mRect = new RectF();
    private int viewWidth = 0;
    private int mBgColor = getResources().getColor(R.color.scrollbar_color);
    private int mIndicatorColor = getResources().getColor(R.color.scroll_color);
    Float ratio = 0.5f;        //长度比例

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

    public ScrollBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public void init(AttributeSet attrs){
        @SuppressLint("CustomViewStyleable") TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.HIndicator);
        mBgColor = typedArray.getColor(R.styleable.HIndicator_hi_bgColor, mBgColor);
        mIndicatorColor = typedArray.getColor(R.styleable.HIndicator_hi_indicatorColor, mIndicatorColor);
        typedArray.recycle();
        mBgPaint.setColor(mBgColor);
        mBgPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mIndicatorColor);
        mPaint.setStyle(Paint.Style.FILL);
    }

    public void set(Float value) {
        ratio = value;
        invalidate();
    }

    Float progress = 0f;    //滑动进度比例
    public void setProgress(Float value) {
        progress = value;
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        mBgRect.set(0f, 0f, w * 1f, h * 1f);
        mRadius = h / 2f;
    }

    /**
     * 设置指示器背景进度条的颜色
     * @param color 背景色
     */
    public void setBgColor(int color) {
        mBgPaint.setColor(color);
        invalidate();
    }

    /**
     * 设置指示器的颜色
     * @param color 指示器颜色
     */
    public void  setIndicatorColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    /**
     * 绑定recyclerView
     */
    public void  bindRecyclerView(RecyclerView recyclerView) {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                float offsetX = recyclerView.computeHorizontalScrollOffset();
                float range = recyclerView.computeHorizontalScrollRange();
                float extend = recyclerView.computeHorizontalScrollExtent();
                float progres = offsetX * 1.0f / (range - extend);
                progress = progres;     //设置滚动距离所占比例
                setProgress(progress);
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制背景
        canvas.drawRoundRect(mBgRect, mRadius, mRadius, mBgPaint);
        //计算指示器的长度和位置
        float leftOffset = viewWidth * (1f - ratio) * progress;
        float left = mBgRect.left + leftOffset;
        float right = left + viewWidth * ratio;
        mRect.set(left, mBgRect.top, right, mBgRect.bottom);
        //绘制指示器
        canvas.drawRoundRect(mRect, mRadius, mRadius, mPaint);
    }
}

2.定义资源文件

     <declare-styleable name="HIndicator">
         <attr name="hi_bgColor" format="color"></attr>
         <attr name="hi_indicatorColor" format="color"></attr>
     </declare-styleable>

3.ScreenWidthHelper

/**
 * RecycleView 占 屏 幕 的 宽 度
 */
	
public class ScreenWidthHelper {
	//reclyview左间距
    private static final int left = 85;
    //每个item所占宽度
    private static final int itemWidth = 230;
    Context context;
    
    public ScreenWidthHelper(Context context) {
        this.context = context;
    }
	
    public int getwidth(){
        WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        int widthPixels = displayMetrics.widthPixels;
        float density = displayMetrics.density;
        int screenWidth = (int) (widthPixels / density);    //屏幕的宽度
        int i = left * 2;        //recycleview距离右侧和左侧的距离
        int recywidth = screenWidth - i;    //recycleview在屏幕的大小
        int entry = recywidth / itemWidth;        // 一屏能显示几个条目
        return entry;
    }
}

4.activity引用

private ScrollBarView mHindicator;

mHindicator = findViewById(R.id.hIndicator);
//绑定reclyview
mHindicator.bindRecyclerView(mReclyview);
screenWidth(appList);

//指 示 器 显 示 或 隐 藏
private void screenWidth(List<AppInfo> appInfoList) {
ScreenWidthHelper screenWidthMapper = new ScreenWidthHelper(this);
  if (appInfoList.size() > screenWidthMapper.getwidth()){
         mHindicator.setVisibility(View.VISIBLE);
     }else {
        mHindicator.setVisibility(View.GONE);
     }
   }

5.布局文件

                <com.my.demo.ui.view.ScrollBarView
                    android:id="@+id/hIndicator"
                    android:layout_width="108dp"
                    android:layout_height="10dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="534dp"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值