当前位置:操作系统 > 安卓/Android >>

android自定义分页组件

组件代码如下:
[java]
public class PageControl extends LinearLayout implements OnClickListener{ 
    private ImageButton firstImg; 
    private ImageButton preImg; 
    private ImageButton nextImg; 
    private ImageButton endImg; 
    private TextView totalPageText; 
    private TextView curPageText; 
    private int numPerPage=10; 
    private int curPage=1; 
    private int count=0; 
    private OnPageChangeListener pageChangeListener;  
    public PageControl(Context context) { 
        super(context); 
        initPageComposite(context); 
    } 
    public PageControl(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        initPageComposite(context); 
    } 
    public PageControl(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
        initPageComposite(context); 
    } 
    private void initPageComposite(Context context){ 
        this.setPadding(5,5,5,5); 
        firstImg=new ImageButton(context); 
        firstImg.setId(1); 
        firstImg.setImageResource(R.drawable.firstpage); 
        firstImg.setPadding(0,0,0,0); 
        LayoutParams layoutParam=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
        layoutParam.setMargins(0,0,5,0); 
        firstImg.setLayoutParams(layoutParam); 
        firstImg.setOnClickListener(this); 
        this.addView(firstImg); 
        preImg=new ImageButton(context); 
        preImg.setId(2); 
        preImg.setImageResource(R.drawable.prepage); 
        preImg.setPadding(0,0,0,0); 
        layoutParam=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
        layoutParam.setMargins(0,0,5,0); 
        preImg.setLayoutParams(layoutParam); 
        preImg.setOnClickListener(this); 
        this.addView(preImg); 
        nextImg=new ImageButton(context); 
        nextImg.setId(3); 
        nextImg.setImageResource(R.drawable.nextpage); 
        nextImg.setPadding(0,0,0,0); 
        layoutParam=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
        layoutParam.setMargins(0,0,5,0); 
        nextImg.setLayoutParams(layoutParam); 
        nextImg.setOnClickListener(this); 
        this.addView(nextImg); 
        endImg=new ImageButton(context); 
        endImg.setId(4); 
        endImg.setImageResource(R.drawable.lastpage); 
        endImg.setPadding(0,0,0,0); 
        layoutParam=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
        layoutParam.setMargins(0,0,5,0); 
        endImg.setLayoutParams(layoutParam); 
        endImg.setOnClickListener(this); 
        this.addView(endImg); 
        totalPageText=new TextView(context); 
        layoutParam=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT); 
        layoutParam.setMargins(5,0,5,0); 
        totalPageText.setLayoutParams(layoutParam); 
        totalPageText.setText("总页数"); 
        this.addView(totalPageText); 
        curPageText=new TextView(context); 
        layoutParam=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT); 
        layoutParam.setMargins(5,0,5,0); 
        curPageText.setLayoutParams(layoutParam); 
        curPageText.setText("当前页"); 
        this.addView(curPageText); 
    } 
    /**
     * 初始化分页组件的显示状态
     * @param newCount
     */ 
    public void initPageShow(int newCount){ 
        count=newCount; 
        int totalPage=count%numPerPage==0?count/numPerPage:count/numPerPage+1; 
        curPage=1; 
        firstImg.setEnabled(false); 
        preImg.setEnabled(false); 
        if(totalPage<=1){ 
            endImg.setEnabled(false); 
            nextImg.setEnabled(false); 
        }else{ 
            endImg.setEnabled(true); 
            nextImg.setEnabled(true); 
        } 
        totalPageText.setText("总页数 "+totalPage); 
        curPageText.setText("当前页 "+curPage); 
    } 
    /**
     * 分页按钮被点击时更新状态,该方法要在initPageShow后调用
     */ 
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,