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; } }
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
赞助商链接