自定义轮播图指示器BannerDotView

自定义轮播图指示器BannerDotView

直接上代码

public class BannerDotView extends LinearLayout {
    private LinearLayout mDotLl;
    private List mDots;
    private OnClickListener mOnClickListener;
    private int mSelectResId;
    private int mNormalResId;

    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);
        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 (mSelectResId != 0) {
                imageView.setImageResource(mSelectResId);
            } else {
                imageView.setImageResource(R.drawable.banner_indicator_selected);
            }
            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(2, 0, 2, 0);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            layoutParams.setLayoutDirection(LinearLayout.HORIZONTAL);
            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()));
                    if (mOnClickListener != null) {
                        mOnClickListener.onClick(((int) v.getTag()));
                    }
                }
            });
            mDotLl.addView(imageView);
            mDots.add(imageView);
        }
    }

    public void setSelectResId(int resId) {
        this.mSelectResId = resId;
    }

    public void setNormalResId(int resId) {
        this.mNormalResId = resId;
    }

    @SuppressLint({"UseCompatLoadingForDrawables", "ResourceType"})
    public void setSelectDot(int position) {
        if (getContext() == null) {
            return;
        }
        for (int i = 0; i < mDots.size(); i++) {
            if (mNormalResId == 0) {
                mDots.get(i).setImageResource(R.drawable.banner_indicator_normal);
            } else {
                mDots.get(i).setImageResource(mNormalResId);
            }
        }
        if (mSelectResId == 0) {
          mDots.get(position).setImageResource(R.drawable.banner_indicator_selected);
        } else {
            mDots.get(position).setImageResource(mSelectResId);
        }
    }

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

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

你可能感兴趣的:(java,android,banner,指示器,banner指示器)