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); }}
阅读全文
0 0
- andorid封装分页控件
- Andorid控件
- 简单的html数据分页控件封装
- andorid listView分页
- Andorid:ExpandableListActivity控件
- Andorid:ExpandableListActivity控件
- Andorid中的控件属性
- Andorid 自定义ProgressBar控件
- 封装分页
- 分页封装
- 分页封装
- 分页封装
- 封装分页
- 分页封装
- Andorid控件之Menu初见
- Andorid DrawerLayout控件的使用方法
- Andorid时间控件和日期控件
- 封装控件
- 如何在controller接受json类型
- JVM内存结构浅析
- python
- 使用YTKRequest避免控制器退出后不马上被释放的问题
- 用DataOutputStream表示输出流
- andorid封装分页控件
- Linux 无法进行域名解析
- PHP使用SMTP发送邮件
- 程序员基础算法
- linux正则表达式 Ingeneral
- 【C#学习】静态字段、常量字段、只读字段、只读属性的相同点和不同点
- 成为java高级工程师需要什么
- 用PrintStream表示输出流。
- Unity中的image effect和Post processing