时间:2020-10-08 13:26:31 | 栏目: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