andorid封装分页控件

主要代码:

package com.sun.framework.CustomizeVC;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.sun.framework.Utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 2017/4/20.
 */

public class PagerVC extends RelativeLayout {

    public int mDotContentWidth;
    public int mDotContentHeight;
    public int mSelectDotBgColor;//选中的点背景颜色
    public int mUnSelectDotBgColor;//没选中的点背景颜色
    public int mDotStrokeWidth;//点边框宽度
    public int mDotStrokeColor;//点边框夜色

    Context context;
    int numberOfPages;
    int currentPage;
    ScreenUtils screenUtils;
    List<View> Dots;
    int mDotWidthHeight;
    int mDotCornerRadius;
    private OnDotClickListener onDotClickListener;

    public PagerVC(Context context) {
        super(context);
    }

    public PagerVC(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

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

    private void init(Context context){
        this.context = context;
        screenUtils = new ScreenUtils(context);
        mDotContentWidth = screenUtils.dp2px(15);
        mDotContentHeight = screenUtils.dp2px(20);
        mDotStrokeWidth = 0;
        mDotStrokeColor = Color.BLACK;
        mSelectDotBgColor = Color.WHITE;
        mUnSelectDotBgColor = Color.GRAY;
    }

    public void setNumberOfPages(int numberOfPages){
        this.numberOfPages = numberOfPages;
        refreshPagerVC();
    }

    public int getNumberOfPages() {
        return numberOfPages;
    }

    public void setCurrentPage(int currentPage){
        this.currentPage = currentPage;
        switchDot();
    }

    public int getCurrentPage() {
        return currentPage;
    }

    private void refreshPagerVC() {
        if (this.getChildCount() > 0){
            this.removeAllViews();
        }
        int min;
        if (mDotContentHeight < mDotContentWidth){
            min = mDotContentHeight;
        }else  {
            min = mDotContentWidth;
        }
        mDotWidthHeight = min/2;
        mDotCornerRadius = mDotWidthHeight/2;

        LinearLayout content = new LinearLayout(context);
        content.setOrientation(LinearLayout.HORIZONTAL);
        this.addView(content);
        RelativeLayout.LayoutParams contentParams = (LayoutParams) content.getLayoutParams();
        contentParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        contentParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        contentParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        Dots = new ArrayList<>();
        for (int i=0;i<numberOfPages;i++){
            RelativeLayout DotContent = new RelativeLayout(context);
            content.addView(DotContent);
            LinearLayout.LayoutParams dotContentParams = (LinearLayout.LayoutParams) DotContent.getLayoutParams();
            dotContentParams.width = mDotContentWidth;
            dotContentParams.height = mDotContentHeight;

            View Dot = new View(context);
            Dot.setTag(i);
            DotContent.addView(Dot);
            RelativeLayout.LayoutParams DotsParams = (LayoutParams) Dot.getLayoutParams();
            DotsParams.width = mDotWidthHeight;
            DotsParams.height = mDotWidthHeight;
            DotsParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
            DotsParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

            GradientDrawable drawable = new GradientDrawable();
            drawable.setColor(mUnSelectDotBgColor);
            drawable.setCornerRadius(mDotCornerRadius);
            drawable.setStroke(mDotStrokeWidth,mDotStrokeColor);
            Dot.setBackgroundDrawable(drawable);

            Dots.add(Dot);
        }
        for (View item : Dots){
            item.setClickable(true);
            item.setOnClickListener(On_Dot_Click);
        }

    }

    private void switchDot(){
        if (null == Dots){
            return;
        }
        int size = Dots.size();
        if (currentPage >= size){
            currentPage = size-1;
        }
        for (View view : Dots){
            GradientDrawable drawable = (GradientDrawable) view.getBackground();
            drawable.setColor(mUnSelectDotBgColor);
        }
        View view = Dots.get(currentPage);
        GradientDrawable drawable = (GradientDrawable) view.getBackground();
        drawable.setColor(mSelectDotBgColor);
    }

    OnClickListener On_Dot_Click = new OnClickListener() {
        @Override
        public void onClick(View view) {
            int tag = (int) view.getTag();
            if (null != onDotClickListener){
                onDotClickListener.OnDotClickListener(tag);
            }
        }
    };

    public void setOnDotClickListener(OnDotClickListener onDotClickListener) {
        this.onDotClickListener = onDotClickListener;
    }

    public interface OnDotClickListener {
        boolean OnDotClickListener(int index);
    }

}


我的业余技术微信公众号:YKJGZH,欢迎大家进入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值