分页加载(类似网页)

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

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值