andorid封装分页控件

来源:互联网 发布:python黑客攻防入门 编辑:程序博客网 时间:2024/06/04 00:22

主要代码:

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);    }}



原创粉丝点击