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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android中okhttp3.4.1+retrofit2.1.0实现离线缓存
这篇文章主要介绍了Android中okhttp3.4.1结合retrofit2.1.0实现离线缓存,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-10-10解析Android中如何做到Service被关闭后又自动启动的实现方法
本篇文章是对在Android中如何做到Service被关闭后又自动启动的方法进行了详细的分析和介绍。需要的朋友参考下2013-05-05Android开发实现读取Assets下文件及文件写入存储卡的方法
这篇文章主要介绍了Android开发实现读取Assets下文件及文件写入存储卡的方法,涉及Android文件与目录的读取、写入、转换等相关操作技巧,需要的朋友可以参考下2017-10-10Android控件之CheckBox、RadioButton用法实例分析
这篇文章主要介绍了Android控件之CheckBox、RadioButton用法,以实例形式较为详细的分析了CheckBox和RadioButton实现复选按钮及单选按钮功能的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下2015-09-09
最新评论