WEB开发网
开发学院手机开发Android 开发 android自定义分页组件 阅读

android自定义分页组件

 2012-10-17 13:25:36 来源:WEB开发网   
核心提示: 组件代码如下:public class PageControl extends LinearLayout implements OnClickListener{private ImageButton firstImg;private ImageButton preImg;private ImageButton nex

 组件代码如下:

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后调用
	 */
	@Override
	public void onClick(View view) {
		if(pageChangeListener==null){
			return;
		}
		int totalPage=count%numPerPage==0?count/numPerPage:count/numPerPage+1;
		switch(view.getId()){
		case 1:
			curPage=1;
			firstImg.setEnabled(false);
			preImg.setEnabled(false);
			if(totalPage>1){
				nextImg.setEnabled(true);
				endImg.setEnabled(true);
			}
			break;
		case 2:
			curPage--;
			if(curPage==1){
				firstImg.setEnabled(false);
				preImg.setEnabled(false);
			}
			if(totalPage>1){
				nextImg.setEnabled(true);
				endImg.setEnabled(true);
			}
			break;
		case 3:
			curPage++;
			if(curPage==totalPage){
				nextImg.setEnabled(false);
				endImg.setEnabled(false);
			}
			firstImg.setEnabled(true);
			preImg.setEnabled(true);
			break;
		case 4:
			curPage=totalPage;
			nextImg.setEnabled(false);
			endImg.setEnabled(false);
			firstImg.setEnabled(true);
			preImg.setEnabled(true);
			break;
		default:
			break;
		}
		totalPageText.setText("总页数 "+totalPage);
		curPageText.setText("当前页 "+curPage);
		pageChangeListener.pageChanged(curPage,numPerPage);
	}
	public OnPageChangeListener getPageChangeListener() {
		return pageChangeListener;
	}
	/**
	 * 设置分页监听事件
	 * @param pageChangeListener
	 */
	public void setPageChangeListener(OnPageChangeListener pageChangeListener) {
		this.pageChangeListener = pageChangeListener;
	}
}

1 2  下一页

Tags:android 定义 组件

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接