Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現簡單的分頁效果

Android實現簡單的分頁效果

編輯:關於Android編程

本文實例為大家分享了Android分頁效果的具體代碼,供大家參考,具體內容如下

1.實現分頁最主要的就是封裝分頁代碼,然後在按鈕裡實現相關的操作

/**
 * 分頁工具
 * 
 * @Project App_Page
 * @Package com.android.dividepage
 * @author chenlin
 * @version 1.0
 * @Date 2012年6月2日
 * @Note TODO
 * @param <T>
 */
public class PageHelper<T> {

 private List<T> allData; // 所有數據
 private int perPage = 10; // 每頁條目
 private int currentPage = 1;// 當前頁
 private int pageNum = 1; // 頁碼
 private List<T> childData;// 子數據
 private int allNum;// 總共條目

 public PageHelper(List<T> datas, int perPage) {
 this.allData = datas;
 if (perPage > 0)
  this.perPage = perPage;
 // 如果數據大於10條
 if (allData != null && allData.size() > perPage) {
  childData = allData.subList(0, perPage - 1);
 }
 allNum = allData.size();
 // 如果總數能除斷perPage,頁數就是余數,否則+1
 pageNum = allNum % perPage == 0 ? (allNum / perPage) : (allNum / perPage + 1);
 }

 public int getCount() {
 return this.allNum;
 }

 public int getCurrentPage() {
 return this.currentPage;
 }

 public int getPageNum() {
 return this.pageNum;
 }

 public int getPerPage() {
 return this.perPage;
 }

 public void gotoPage(int n) { // 頁面跳轉
 currentPage = n > pageNum ? pageNum : (n < 1 ? 1 : n);
 }

 public boolean hasNextPage() {// 是否有下一頁
 return currentPage < pageNum;
 }

 public boolean hasPrePage() {// 是否有前一頁
 return currentPage > 1;
 }

 public void headPage() {// 第一頁
 currentPage = 1;
 }

 public void lastPage() {// 最後一頁
 currentPage = pageNum;
 }

 public void nextPage() {// 下一頁
 currentPage = hasNextPage() ? currentPage + 1 : pageNum;
 }

 public void prePage() {// 前一頁
 currentPage = hasPrePage() ? currentPage - 1 : 1;
 }

 public void setPerPage(int perPage) {// 設置上一頁面
 this.perPage = perPage;
 }

 /**
 * 獲得當前數據
 * @return
 */
 public List<T> currentList() {
 if (currentPage == 1) {
  childData = allData.subList(0, perPage);
 } else if (currentPage == pageNum) {
  childData = allData.subList(perPage * (pageNum - 1), allNum);
 } else {
  childData = allData.subList(perPage * (currentPage - 1), perPage * currentPage);
 }
 return childData;
 }

 public void setCurrentPage(int currentPage) {
 this.currentPage = currentPage;
 }

}

2、主頁裡我根據tab標簽把按鈕設置進去,然後在click裡方法讀取,最後根據tag標簽就可判斷是哪個按鈕點擊了

/**
 * 分頁主頁
 * @Project App_Page
 * @Package com.android.dividepage
 * @author chenlin
 * @version 1.0
 * @Date 2012年6月2日
 * @Note TODO
 */
public class MainActivity extends Activity implements OnClickListener {

 private ListView mListView;
 //分頁按鈕
 private Button mBtnPrePage, mBtnNextPage, mBtnPreItem, mBtnNextItem;
 //顯示分頁信息
 private TextView mTvPageNo;

 //數據實現
 private PageHelper<String> mPageDaoImpl;
 private DataAdapter mAdapter;
 private List<String> mDatas;

 //被選著的索引
 private int selectIndex = 0;

 private static final int PREPAGE = 0;
 private static final int NEXTPAGE = 1;
 private static final int PREITEM = 2;
 private static final int NEXTITEM = 3;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initViews();
 initDatas();
 }

 private void initDatas() {
 //從資源文件裡讀數據
 mDatas = Arrays.asList(getResources().getStringArray(R.array.channellist));
 //每次讀8條數據
 mPageDaoImpl = new PageHelper<String>(mDatas, 8);
 mAdapter = new DataAdapter(this, mPageDaoImpl.currentList());
 mListView.setAdapter(mAdapter);
 //設置當前頁碼與總頁碼
 mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum());
 }

 private void initViews() {
 mListView = (ListView) findViewById(R.id.page_list);
 mBtnPrePage = (Button) findViewById(R.id.pre_page);
 mBtnPrePage.setTag(PREPAGE);
 mBtnPrePage.setOnClickListener(this);
 mBtnNextPage = (Button) findViewById(R.id.next_page);
 mBtnNextPage.setTag(NEXTPAGE);
 mBtnNextPage.setOnClickListener(this);
 mBtnPreItem = (Button) findViewById(R.id.pre_item);
 mBtnPreItem.setTag(PREITEM);
 mBtnPreItem.setOnClickListener(this);
 mBtnNextItem = (Button) findViewById(R.id.next_item);
 mBtnNextItem.setTag(NEXTITEM);
 mBtnNextItem.setOnClickListener(this);

 mTvPageNo = (TextView) findViewById(R.id.pagenum);
 }

 @Override
 public void onClick(View v) {
 final int flag = (Integer) v.getTag();
 switch (flag) {
 case PREPAGE:// 首頁
  headPage();
  break;
 case NEXTPAGE:// 尾頁
  lastPage();
  break;
 case PREITEM:// 上一條
  prePage();
  break;
 case NEXTITEM:// 下一條
  nextPage();
  break;
 }
 }

 private void prePage() {
 if (selectIndex == 0) {
  if (mPageDaoImpl.getCurrentPage() >= 1) {
  mPageDaoImpl.prePage();
  } 
  mAdapter.setData(mPageDaoImpl.currentList());
  mListView.setSelection(mAdapter.getCount() - 1);
  mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum());
 } else {
  return;
 }
 }

 private void nextPage() {
 if (mPageDaoImpl.getCurrentPage() <= mPageDaoImpl.getPageNum()) {
  mPageDaoImpl.nextPage();
 } 
 mAdapter.setData(mPageDaoImpl.currentList());
 mListView.setSelection(0);
 mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum());
 }



 private void lastPage() {
 if (mPageDaoImpl.getCurrentPage() != mPageDaoImpl.getPageNum()) {
  mPageDaoImpl.lastPage();
 } 
 mAdapter.setData(mPageDaoImpl.currentList());
 mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum());
 }

 private void headPage() {
 if (mPageDaoImpl.getCurrentPage() != 1) {
  mPageDaoImpl.headPage();
 } 
 mAdapter.setData(mPageDaoImpl.currentList());
 mTvPageNo.setText(mPageDaoImpl.getCurrentPage() + " / " + mPageDaoImpl.getPageNum());
 }

}

3、適配器代碼,太簡單了,我就不說了

public class DataAdapter extends BaseAdapter {

 private Context mContext;
 private List<String> mDatas;

 public DataAdapter(Context context, List<String> datas) {
 this.mContext = context;
 this.mDatas = datas;
 }

 @Override
 public int getCount() {
 return mDatas == null ? 0 : mDatas.size();
 }

 @Override
 public Object getItem(int position) {
 return mDatas == null ? null : mDatas.get(position);
 }

 @Override
 public long getItemId(int position) {
 return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 ViewHolder mHolder;
 if (convertView == null) {
  convertView = LayoutInflater.from(mContext).inflate(R.layout.channel_item, parent, false);
  mHolder = new ViewHolder();
  mHolder.nameView = (TextView) convertView.findViewById(R.id.channel_name);
  convertView.setTag(mHolder);
 }
 mHolder = (ViewHolder) convertView.getTag();
 mHolder.nameView.setText(mDatas.get(position));
 return convertView;
 }

 static class ViewHolder {
 public TextView nameView;
 }

 public void setData(List<String> datas) {
 mDatas = datas;
 notifyDataSetChanged();
 }
}


4、代碼下載:http://xiazai.jb51.net/201611/yuanma/AndroidAppPage(jb51.net).rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved