Android 自定义分页组件

jopen 12年前

组件代码如下:

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;   }  }
分页监听器代码:
/**   * 分页监听事件   */  public interface OnPageChangeListener {   /**    * 点击分页按钮时触发此操作    * @param curPage 当前页    * @param numPerPage 每页显示个数    */   public void pageChanged(int curPage,int numPerPage);  }
使用示例:实现ListView分页效果
Activity布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical" >      <ListView          android:id="@+id/wordList"          android:layout_width="match_parent"          android:layout_height="0dp"          android:layout_weight="11" >      </ListView>      <org.chen.wordreview.control.PageControl          android:id="@+id/wordListPageControl"          android:layout_width="match_parent"          android:layout_height="0dp"          android:layout_weight="1"/>  </LinearLayout>
Activity实现:
//实现分页监听接口  public class MainActivity extends Activity implements OnPageChangeListener{   private WordDao wordDao;   private ListView wordListView;   private ArrayAdapter<Word> adapter;   private PageControl pageControl;      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_wordlist);          ...          //初始化分页组件          pageControl=(PageControl) findViewById(R.id.wordListPageControl);          pageControl.setPageChangeListener(this);          pageControl.initPageShow(wordDao.getCount());          ...      }      /**       * 点击分页按钮时触发该方法的执行       * @param curPage 当前页       * @param numPerPage 每页显示记录数       */   @Override   public void pageChanged(int curPage, int numPerPage) {    List<Word> words=wordDao.findOnePage((curPage-1)*numPerPage,numPerPage);    adapter.clear();    adapter.addAll(words);    adapter.notifyDataSetChanged();   }  }
分页效果图
Android 自定义分页组件