自定义轮播图指示器,banner dot

自定义轮播图指示器,banner dot

直接上代码。

public class BannerDotView extends LinearLayout {
    private LinearLayout mDotLl;
    private List mDots;
    private OnClickListener mOnClickListener;
    private int mSelectDrawable;
    private int mUnSelectDrawable;

    public BannerDotView(Context context) {
        this(context, null);
    }

    public BannerDotView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BannerDotView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public BannerDotView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setOrientation(HORIZONTAL);
        initView(context);
    }

    private void initView(Context context) {
        if (context == null) {
            return;
        }
        mDotLl = (LinearLayout) this.getRootView();
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    public void setDataSize(int size) {
        if (getContext() == null) {
            return;
        }
        mDots = new ArrayList<>();
        mDotLl.removeAllViews();
        for (int i = 0; i < size; i++) {
            ImageView imageView = new ImageView(getContext());
            if (mSelectDrawable != 0) {
                imageView.setImageResource(mSelectDrawable);
            } else {
                mDots.get(position).setImageResource(Color.RED);
            }
            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(2, 0, 2, 0);
            imageView.setLayoutParams(layoutParams);
            imageView.setPadding(10, 10, 10, 8);
            imageView.setTag(i);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    viewPager.setCurrentItem(((int) v.getTag()) + 1);
                    if (mOnClickListener != null) {
                        mOnClickListener.onClick(((int) v.getTag()) + 1);
                    }
                }
            });
            mDotLl.addView(imageView);
            mDots.add(imageView);
        }
    }

    public void setSelectDrawable(int resIdDrawable) {
        this.mSelectDrawable = resIdDrawable;
    }

    public void setUnSelectDrawable(int resIdDrawable) {
        this.mUnSelectDrawable = resIdDrawable;
    }

    @SuppressLint({"UseCompatLoadingForDrawables", "ResourceType"})
    public void setSelectDot(int position) {
        if (getContext() == null) {
            return;
        }
        for (int i = 0; i < mDots.size(); i++) {
            if (mUnSelectDrawable == 0) {
                mDots.get(i).setImageResource(Color.GRAY);
            } else {
                mDots.get(i).setImageResource(mUnSelectDrawable);
            }
        }
        if (mSelectDrawable == 0) {
            mDots.get(position).setImageResource(Color.RED);
        } else {
            mDots.get(position).setImageResource(mSelectDrawable);
        }
    }

    public void setOnClickListener(OnClickListener mOnClickListener) {
        this.mOnClickListener = mOnClickListener;
    }

    public interface OnClickListener {
        void onClick(int position);
    }
}

你可能感兴趣的:(android,轮播图指示器,轮播图小圆点)