public class PageControl extends LinearLayout implements View.OnClickListener {
private int maxPage = 1;//最大页
private int curPage = 1;//当前页
private int TotalCount = 0;//总数
private int buttonCount = 5;//按钮数
private int lastButton = 0;//最后一个按钮对应的页码
private int firstButton = 0;//第一个按钮对应的页码
private OnPageChangeListener pageChangeListener;
Context context;
private int height = 0;
//分页监听事件
public interface OnPageChangeListener {
//点击分页按钮时触发此操作
// curPage 当前页 numPerPage 每页显示个数
void pageChanged(int numPerPage);
}
public PageControl(Context context) {
this(context, null);
}
public PageControl(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public PageControl(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
height = DisplayUtil.getInstance().dip2px(context, 44);
initPageComposite();
}
private void createView(int page) {
TextView first = createView();
first.setText(page + "");
first.setTag(page);
first.setOnClickListener(this);
this.addView(first);
}
private TextView createView() {
TextView first = new TextView(context);
first.setBackgroundResource(R.drawable.page_sel);
first.setPadding(20, 0, 20, 0);
first.setGravity(Gravity.CENTER);
LayoutParams layoutParam = new LayoutParams(LayoutParams.WRAP_CONTENT, height);
first.setMinWidth(height);
//first.setTextColor(context.getResources().getColor(R.drawable.selector_textcolor,null));
layoutParam.setMargins(0, 0, 15, 0);
first.setLayoutParams(layoutParam);
return first;
}
private void createPrevious() {
TextView first = createView();
first.setText("上一页");
first.setOnClickListener(this);
this.addView(first, 0);
}
private void createNext() {
TextView first = createView();
first.setText("下一页");
first.setOnClickListener(this);
this.addView(first);
}
public void initPageComposite() {
int temp = maxPage;
maxPage = TotalCount % Constant.PageSize == 0 ? TotalCount / Constant.PageSize : TotalCount / Constant.PageSize + 1;
if (temp != maxPage) {
createAllView();
}
if (maxPage == 0) {
removeAllViews();
return;
}
if (curPage < firstButton || curPage > lastButton || lastButton == 0) {
createAllView();
}
setSelectView(curPage);
}
private void setBtnGroup() {
int n = maxPage / buttonCount;
if (n == 0) {
//只有一组
firstButton = 1;
lastButton = maxPage;
} else {
int i = curPage / buttonCount;
//有n组
firstButton = i * buttonCount + 1;
if (firstButton > curPage) {
firstButton = (i - 1) * buttonCount + 1;
}
lastButton = (firstButton - 1) + buttonCount;
}
if (lastButton > maxPage && lastButton > 1) {
lastButton = maxPage;
}
}
private void createAllView() {
setBtnGroup();
this.removeAllViews();
this.setPadding(10, 10, 10, 10);
int n = firstButton;
while (n <= lastButton && n <= TotalCount) {
createView(n);
n++;
}
createPrevious();
createNext();
}
public void setTotalCount(int totalCount) {
if (totalCount != TotalCount) {
TotalCount = totalCount;
initPageComposite();
}
}
public int getTotalCount() {
return TotalCount;
}
//分页按钮被点击时更新状态,该方法要在initPageShow后调用
@Override
public void onClick(View view) {
if (pageChangeListener == null) return;
if (view instanceof TextView) {
TextView tv = (TextView) view;
String txt = tv.getText().toString();
if (txt.equalsIgnoreCase("上一页")) {
curPage -= 1;
curPage = curPage >= 1 ? curPage : 1;
setSelectView(curPage);
pageChangeListener.pageChanged(curPage);
initPageComposite();
return;
}
if (txt.equalsIgnoreCase("下一页")) {
curPage += 1;
curPage = curPage <= maxPage ? curPage : maxPage;
setSelectView(curPage);
pageChangeListener.pageChanged(curPage);
initPageComposite();
return;
}
}
if (view.getTag() != null) {
Object tag = view.getTag();
curPage = Integer.parseInt(tag.toString());
pageChangeListener.pageChanged(curPage);
setSelectView(curPage);
}
}
public void setCurrentPage(int page) {
curPage = page;
setSelectView(curPage);
}
private void setSelectView(int n) {
//initPageComposite();
for (int i = 0; i < this.getChildCount(); i++) {
View v = this.getChildAt(i);
if (v.getTag() != null) {
int tag = Integer.parseInt(v.getTag().toString());
if (tag == n) {
v.setSelected(true);
} else {
v.setSelected(false);
}
}
}
}
/**
* 设置分页监听事件
*/
public void setPageChangeListener(OnPageChangeListener pageChangeListener) {
this.pageChangeListener = pageChangeListener;
}
}