时间:2021-05-20
本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Android基于ListView实现类似Market分页加载效果。分享给大家供大家参考,具体如下:最近几天研究ListView实现分页加载和滚动
本文实例为大家分享了vue.js2.0实现分页效果的具体代码,供大家参考,具体内容如下vue.js2.0实现的简单分页*{margin:0;padding:0;
先看看效果图:简单的分页js代码:1、效果描述:不用分页即可显示的jQuery插件jQuery分页插件——jQuery.page.js用法很简单,效果很棒2、调
分页大家都会用Android的TabHost和TabActivity的组合,今天我这里实现的是GridView和ActivityGroup实现的分页,这里需要将
本文实例讲述了Android滑动动态分页实现方法。分享给大家供大家参考,具体如下:实现Android.widget.AbsListView.OnScrollLi