1. 程式人生 > >android自定義分頁元件

android自定義分頁元件

元件程式碼如下:
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();
	}
}
分頁效果圖